Stackexchange how Redis implements Brpop/blpop

Source: Internet
Author: User
Tags redis server



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.