Today when using the Stackexchange Redis client. I wanted to use Brpop, but I found that Stackexchange Redis did not provide an API and had no way of looking for documents.
The original Stackexchange Redis uses multiplexing (multiplexing), which means that it only maintains a single connection with Redis server. When there is a concurrent request, it automatically uses the pipeline (pipeline) to send each request, and each request waits until the previous request has been executed. For this reason, Stackexchange Redis does not provide the appropriate API for Brpop/blpop because these two operations are likely to block the entire mulitplexer.
So, what's the way to implement BRPOP/BLPOP operations? The answer is given in the document, using Pub/sub:
sub.Subscribe(channel, () => {
string work = db.ListRightPop(key);
if (work != null) Process(work);
});
//...
db.ListLeftPush(key, newWork, flags: CommandFlags.FireAndForget);
sub.Publish(channel, "");
Note: 1. The data in this implementation is not really passed through Pub/sub, Pub/sub is just a notification, that is, when data is added to the queue, the Subscriber is notified and the Subscriber is taken out of the list.
2. When there are multiple consumer subscriptions, only one consumer can take this value
3. If there is no consumer subscription, the data will always exist in the list
4. You need to ensure that consumers are able to handle this backlog of data (which seems troublesome) when your customers are being notified of the new data being confiscated by restarting. )
Reference: HTTPS://GITHUB.COM/STACKEXCHANGE/STACKEXCHANGE.REDIS/BLOB/MASTER/DOCS/PIPELINESMULTIPLEXERS.MD
Stackexchange how Redis implements Brpop/blpop