Using Redis as a queue

By | August 13, 2017

Redis is a great key / value in memory database.

It can be used as an efficient queue as well. One or multiple producers can push events to queue and one or multiple consumers can read data from Redis.

Producer side

The producer can push data to consumers using RPUSH Redis command. This command pushes data to the end of the list. It list is unknown, it is automatically created.

For example RPUSH key1 “string1”.

Another alternative is to push data to the beginning on the list using the LPUSH.
For more information check the following article:

One or more producers can push data to the same list using the same key, like key1 above.

Consumer side

Redis supports block read on the list, so, the client application will block on socket read until data is available or timeout expired. If multiple consumers wait for data on the same list, only one consumer will receive the data. The other consumers will block until more data is available. According to the Redis docs, the one that waited longer will receive the data.

For this, Redis has the BRPOP and BLPOP operation. For example:
BRPOP key1 timeout

The consumer can block on one or multiple keys. For example:
BRPOP key1 key2 key3 timeout

Timeout value

The read operation can be blocked based on a timeout. A timeout of zero can be used to block indefinitely.

Looking forward hearing your comments and suggestions!