Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Laymen's terms, what is Redis Pub/Sub?

Why would I use it? Give some very basic examples.

like image 395
TIMEX Avatar asked Apr 26 '11 05:04

TIMEX


People also ask

What is Redis pub sub?

Redis Pub/Sub is the oldest style of messaging pattern supported by Redis and uses a data type called a “channel,” which supports typical pub/sub operations, such as publish and subscribe. It's considered loosely coupled because publishers and subscribers don't know about each other.

Is Redis Pub/Sub blocking?

However a small handful of Redis commands are long-term blocking or non-atomic. The PUBSUB subscribe and psubscribe commands are non-atomic in that the command will register it's request to have any messages sent to the given channel(s) sent to it as well, but the actual data can arrive much later – ie.

What is use of pub sub?

Pub/Sub allows services to communicate asynchronously, with latencies on the order of 100 milliseconds. Pub/Sub is used for streaming analytics and data integration pipelines to ingest and distribute data.

Is Redis Pub/Sub distributed?

The Pub/Sub messaging of Redis can be extended to create interesting distributed events. Let's say we have a structure that is stored in a hash but we want to update clients of it only when a particular field exceeds a numerical value as defined by the subscriber.


1 Answers

A redis client subscribes to receive messages marked with a specific tag, termed channel. Other clients publish to this channel. Redis notifies each subscribing client each time a message is published by anyone to the channel.

You can also subscribe to a channel pattern - think regex matching.

This helps make code distributable. It allows bits of code to run in different processes, and potentially even different machines, and to communicate with each other via these queues.

This feature comes from repeated user requests. There is an example use-case given here:

a news-related site needs to update the cached copy of its home page every time that a new article is published.

The background cache worker process subscribes to all channels that begin with ‘new.article.’:

redis> PSUBSCRIBE new.article.*

The article publishing process creates a new technology article (in this example, this article has ID ‘1021’), adds the article’s ID to the set of all technology articles, and publishes the article’s ID to the ‘new.article.technology’ channel:

redis> MULTI
OK
redis> SET article.technology.1021 "In today's technology news, ..."
QUEUED
redis> SADD article.technology 1021
QUEUED
redis> PUBLISH new.article.technology 1021
QUEUED
redis> EXEC
1. OK
2. (integer) 1
3. (integer) 1

At this point, the background cache worker process will receive a message and know immediately that a new technology article was published, subsequently executing the appropriate callback to re-generate the home page.

http://redis.io/topics/pubsub

like image 185
Will Avatar answered Sep 21 '22 21:09

Will