Using Redis and Node-js to generate Temporary-Invite Codes at Scale.

Gaurav Agrawal
2 min readApr 23, 2022
Credits: https://unsplash.com/photos/cixohzDpNIo

Process

Step 1: (Connect Redis)

Connect the Redis client to your node application.

Step 2: (Generate Invite-code)

Generate a random string of alphanumeric characters.

Step 3: (Making sure the Invite-code is unused)

Check if generated invite code already exists in the key-value store, if exists then retry till you manage to find a new one.

Note: This retrial will almost never run infinite and is not a computationally heavy process. Reason being the invite-code in key-value stores are short lived and we have a wide range of combinations to iterate upon.

Step 4: (Store Invite Code in Redis)

Store invite-code (key) and resource-id as value in Redis key-value store.

Step 5: (Accepting Invite Code)

Perform a get operation with invite-code as value. If the value exists then use the resource-id and one can explicitly clear the invite code using del in redis-store (depending on the use case of single use-invite code), otherwise redis will anyways be clearing up the key post the expire-timeoutthe value used by the set operation.

Code

Why Redis Helps In Scale for this use-case ?

  • Key-value in memory reads and writes are super fast in redis-store.
  • For the invite-code use case, using redis key-value store helps in faster unique checks and retrievals.
  • Redis set expire timeout feature is reliable and fast for implicit invite code expiry.
  • Retrial mechanism benefits a lot from near milliseconds retrievals from Redis’s key value stores.

Useful Readings:

Link to Repo

Please feel free to drop in your valuable feedbacks, suggestions or code-reviews ❤️.

--

--