0

I need to design a node.js app in this scenario:

  1. User requests to /page with a cookie which has a unique token
  2. Node.js creates a record with this token on redis(?) with a 5 mins TTL. tokens has n type. So redis(?) should store tokens with types.
  3. If user comes back again before record expire, record's TTL reset to 5 mins again.
  4. If user doesn't come back again and record expires, a function is triggered.
  5. And at last, I also need the count of records belongs to a specific type (i.e. type 27).

What would be the best way to achieve this problem? Is redis right choice for it? How can I count tokens and trigger 5. step if I use redis? Using https://github.com/Automattic/kue or something like that would be the nice choice?

Edit: It seems possible duplicate with Event on key expire . In general, I ask, is redis suitable for this problem? If you think it is, could you please give an example pattern I should follow. If not, what are any other solutions maybe even without redis or 3rd party tool.

Also as I said at the 5. item in the list above, redis seems not suitable at querying keys with a pattern as described here:https://redis.io/commands/keys . Because there seems no way to put expiration on an individual member of a set (TTL for a set member) , I cannot put tokens in a type set or hash . Thus I need to query keys in order to get count of a group (type) and this seems not efficient as described at the link above.

Even if https://redis.io/topics/notifications solves the problem at the 4. item in the list above, I also ask for "Which pattern/algorithm should I go and how" . I don't want you to code for me, but just need a spark. A tutorial, guide, video etc.. would be great. Thanks!

iedmrc
  • 742
  • 2
  • 8
  • 21
  • what have you tried so far? – Magd Kudama Oct 16 '18 at 18:43
  • I've just created redis strings but I saw I don't know how to "trigger a function when the key expired" and also getting count of record (keys) which has type, say 27, is also seems not efficient with redis. I just found this links: https://redis.io/commands/keys https://quickleft.com/blog/how-to-create-and-expire-list-items-in-redis/ https://stackoverflow.com/questions/33166812/redis-scan-count-how-to-force-scan-to-return-all-keys-matching-a-pattern – iedmrc Oct 16 '18 at 18:48
  • Since there is really nothing else except **get notified when redis key expires**. Possible duplicate of [Event on key expire](https://stackoverflow.com/questions/14647494/event-on-key-expire) – Shaharyar Oct 16 '18 at 19:09

1 Answers1

1

You have many options in order to achieve what you want, but I'm going to provide one here, which is a very efficient one in terms of retrieving speed, but it requires some more space. It all depends on your use-case, obviously.

Say token is 12345, type is A

When adding a token (we'll do it with a transaction):

MULTI
SETEX tokens:12345 300 ""
SET type:12345 "A"
INCR types:A
EXEC

When the key expires (after 300 seconds, or whenever Redis sees it as expired) we get notified using Keyspace notifications (https://redis.io/topics/notifications) listening for the EXPIRED event:

PSUBSCRIBE __keyspace@0__:expired

When that subscription receives a message, in your code you'd need to:

MULTI
GET type:12345 # Returns A
DEL type:12345
DECR types:A
EXEC

In order to get the elements of a specific type:

GET types:A

Any NodeJS Redis client would work just fine for this.

I want to make clear this is only one of the multiple options you have; one of the Redis advantages is it's flexibility.

Magd Kudama
  • 3,229
  • 2
  • 21
  • 25