Redis Publish/Subscription Data Persistance


I am implementing a TCP chat server using node js and redis, however i dont seem to be able to persist chat data on redis using Publish and Subscribe, and hence when i have left the chat room and reentered, i will not be updated on the newest messages, how should i implement something like this?

Problem courtesy of: henghonglee


Publish is not meant to be stored in Redis, even if you chose the disk storage. When it recieves message, it just finds the connections with requested channels and forwards to each. So, it is not storing anything. Even if it did, It should continously try to forward messages (because it's a pub/sub model) which is not very effective. Instead, you should also push (by lpush the messages to a queue, so they can be stored. And when a client connects and has no messages, it can retrieve those messages from queue (without popping, so other newcomers can also use) and then subscribe to channel and recieve new messages.

Solution courtesy of: Mustafa


By default, redis is in memory only. You have to enable persistence explicitly.

There are multiple options, AOF every query being the safest, but probably the slowest.

More details here:

Discussion courtesy of: Pascal Belloncle

This recipe can be found in it's original form on Stack Over Flow.