6

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?

tehsheri
  • 63
  • 4

2 Answers2

8

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

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
0

As https://redis.io/docs/manual/pubsub/#database--scoping suggests

If you need scoping of some kind, prefix the channels with the name of the environment (test, staging, production...).

Qiulang
  • 10,295
  • 11
  • 80
  • 129