Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to fully separate two redis databases for pub/sub usage?

Scenario: Two instances of an application share the same redis instance, but use different databases. The application makes use of the redis pub/sub functions to exchange data between services.

Problem: When application instance A publishes something (on redis database 1), application instance B (running on redis database 2) receives the message.

Expectation: As both instances of the application use a different database, I would expect not only that the keys in redis are hold separately, but pub/sub subscribers aswell.

Question: Can I tell redis to keep pub/sub separate for each database?

like image 535
tehsheri Avatar asked Aug 17 '17 14:08

tehsheri


People also ask

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

Is Redis good for pub sub?

Aside from data storage, Redis can be used as a Publisher/Subscriber platform. In this pattern, publishers can issue messages to any number of subscribers on a channel. These messages are fire-and-forget, in that if a message is published and no subscribers exists, the message evaporates and cannot be recovered.

How do I use Redis as a pub sub?

Basic Pub-Sub modelStart by opening three terminal sessions and launch the Redis CLI in each of them. Once you have all the terminals open and set up, use one of the terminals to SUBSCRIBE to a channel. The name will entirely depend on you, and you can name it whatever you want.

How Redis Pub/Sub works internally?

Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.


1 Answers

No - PubSub is shared across all clients connected to the server, regardless of their currently SELECTed database (shared database/numbered database/keyspace). While you can use different channels and such, real separation is possible only by using two Redis instances.

Note: using shared/numbered databases isn't recommended - always use dedicated Redis instances per app/service/use case

like image 117
Itamar Haber Avatar answered Nov 16 '22 02:11

Itamar Haber