jprogrammer
jprogrammer

Reputation: 43

Shared redis instance by multiple instances of same application

We have been asked to implement caching in an application using redis. The application should have a logic to clear cache on startup and initialize it. However, the redis instance can be shared by multiple instances of the application.

e.g. application X has two instances X0 and X1 sharing the same redis instance.

Problem: With multiple instances, it is possible that one instance trying to initialize the cache while other instance is clearing it.

Two questions

1) How do make sure while cache is getting initialized, other instance does not clear it.

One way to solve this problem is to maintain a flag in redis to check if it is being cleared or initialized. If cache is being initialized, do not clear or re-initlize it.

2) Is it good practice to have shared redis instance by multiple application instances?

Upvotes: 3

Views: 6453

Answers (1)

Sripathi Krishnan
Sripathi Krishnan

Reputation: 31528

In general it's not a good idea to share redis. If you only have a limited number of application instances, you are better off creating a separate Redis process for each. Redis is lightweight, so multiple processes running on different parts on the same server works well in practice.

If you cannot install multiple processes, you can have 1 database for each instance. Redis by default allows 16 databases. You can then flush each database independently. Just remember that databases in redis are discouraged, and they have been discontinued in redis cluster.

Upvotes: 3

Related Questions