I am building a PUB/SUB system backed by Redis.
I have one publisher and tons of subscribers. The subscribers are not that reliable, they can lose connection at any time and need to be able to "recover" from a lost connection.
There is a twist though, I would like my backlog capped at some number, meaning that a faulty subscriber should be able to recover only up to N messages.
The trivial solution is:
- Publisher publishes message X
- X is pushed onto a list
RPUSH list message
- Message is encoded to include its index in the list
- Message is published to consumers (with the index embedded)
PUBLISH channel encoded
If a consumer needs to re-establish:
- It asks the redis for all the values in the list after the index it has and executes a
PSUBSCRIBE
atomically
Up to here we are all good.
My big question is, what if I want the backlog list to be capped at N items?
Is there any way I can keep an ever increasing index AND a capped backlog in the list?