Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackExchange.Redis ListRightPop not waiting for result

I'm trying to write a producer/consumer system using Redis in C#. Each message produced must be consumed by only one consumer, and I want the consumers to wait for elements created by the consumer. My system must support many produced/consumer sets.

I am using StackExchange.Redis to communicate with Redis, and using lists where elements are added using ListLeftPush and removed with ListRightPop. What I am experiencing is that while the ListRightPop method should block until an element exists in the list (or after a defined timeout), it always returns automatically if there are no elements in the list. This is the test code I wrote to check this:

IDatabase cache = connection.GetDatabase();        
Trace.TraceInformation("waiting "+DateTime.Now);
var res = cache.ListRightPop("test");
Trace.TraceInformation("Got "+res+", Ended" + DateTime.Now);

And I'm getting a nil result after less than 1 second.

like image 397
vainolo Avatar asked Jan 09 '23 20:01

vainolo


1 Answers

The standard pop operations do not block: they return nil if the list is empty or does not exist.

SE.Redis is a multiplexer. Using a blocking pop is a very very bad idea. This is explained more, with workarounds discussed specifically for blocking pops, in the documentation: https://stackexchange.github.io/StackExchange.Redis/PipelinesMultiplexers

like image 108
Marc Gravell Avatar answered Jan 21 '23 14:01

Marc Gravell