0

I have a model Item which stores user items. This model has a field endDate at which the instances have to be marked for deletion by setting expired field to true. Is there some way I can do this in loopback/express automatically? I am using postgresql as database.

I can think of following 2 ways but I don't think they are the best solutions for this problem:

1) Using setTimeout I can call a function which would mark the items for deletion. This solution is not at all scalable as it is not stateless and if there are multiple instances of node running, this would cause issues.

2) Use redis database and for each item create a key in redis with expire time. redis will automatically call a function when the timer expires and I can delete the item then. This I think is scalable as this is stateless.

Is there a more efficient solution to this?

Edit

I want to notify the user also as soon as the item expires. So, I would need a function call to happen as soon as expiration happens. So setting expireAt key and running a cleanup utility at intervals, will not be of much help here.

Noober
  • 1,516
  • 4
  • 22
  • 48
  • 1
    Why not set an expired field in the database for each item. They, you can periodically run a cleanup algorithm that queries for any items that have an expired value beyond some threshold and you can then remove them. The state is entirely stored in the database so it will work just fine with clustering as any server in the cluster can run the cleanup code. – jfriend00 Sep 28 '19 at 05:12
  • Thanks. That is an option but actually I want to notify the user also as soon as it expires (I am using websockets for that) .That is why I need a function call to happen when it expired. – Noober Sep 28 '19 at 05:43
  • 1
    Then you should add that requirement to your question. – jfriend00 Sep 28 '19 at 05:50

2 Answers2

0

setTimeout is scalable provided the scalability/performance advise is followed. The issue here is that setTimeout approach is not durable e.g. the functionality will be broken for some users if the server/container that hosts Express reboots. The other issue is that it will not work if you have multiple Express instances.

In case there is one Express instance using Redis will change neither scalability nor durability unless you enable Redis persistence to survive restarts/reboots or unless you use more than one Redis node.

In case there are multiple Express instances using Redis is the way to go.

winwiz1
  • 2,906
  • 11
  • 24
0

I would use an operation hook like access to launch a function that would query the model to find the newly expired items.