1. Intoduction

Redis is an open sourced in-memory data structure used as database, cache and message broker. This makes it crazy fast when it comes to loading data that hasn’t been changed for a long time.

For example, you go to 100xDev website,

image.png

This data isn’t changed over a long period of time, hence calling it everytime from postgres everytime doesn’t make sense.

2. In memory data structure

image.png

You might be thinking how does redis store the data in-memory, well here is the answer.

save 900 1       # Save the dataset every 900 seconds if at least 1 key changed
save 300 10      # Save the dataset every 300 seconds if at least 10 keys changed
save 60 10000    # Save the dataset every 60 seconds if at least 10000 keys changed

2.1 Pushing to a queue

LPUSH problems 1
LPUSH problems 2

2.2 Popping from a queue

RPOP problems
RPOP problems 

2.3 Blocked pop

BRPOP problems 0 -> infinitely blocked
BRPOP problems 30 -> second key

The last argument represents the timeout before the blocking should be stopped.

3. Talking to redis via Node.js

There are various clients that exist that let you talk to redis via Node.js https://www.npmjs.com/package/redis Let’s initialize a simple Node.js express server that takes a problem submission (very similar to leetcode) as input and sends it to the queue.