For data communication across processes, I intend to use Redis list. The producer pushes to the list, while a set of consumers consume the list contents using BRPOP.
In order to restrict the list growing infinitely in size, I want to restrict the list size to a fixed value (say 10K items). I was surprised to find no equivalent command like BLPUSH or BRPUSH. Is this a deliberately omitted by Redis folks?
So, I assume I have to create a Txn with Watch/multi to check the list size before pushing. Is this the right way, or any better techniques available?
I would go for a lua script for this functionality.
LUA that accepts one key (list name), and two arguments,
new_element_name
andmax_size
. Return value could be theLPUSH
return value or-1
when the list is full.Here's a script that does this:
You should load it once with SCRIPT LOAD:
And use it with EVALSHA