Search code examples
cachingredismicroservices

How to use redis for number of micro-services?


I am very much new to redis. I have been investigating on redis for past few days.I read the documentation on cache management(lru cache), commands ,etc. I want to know how to implement caching for multiple microservice(s) data . I have few questions:

  1. Can all microservices data(cached) be kept under a single instance of redis server?
  2. Should every microservice have its own cache database in redis?

  3. How to refresh cache data without setting EXPIRE? Since it would consume more memory.

Some more information on best practices on redis with microservices will be helpful.


Solution

  • It's possible to use the same Redis for multiple microservices, just make sure to prefix your redis cache keys to avoid conflict between all microservices.

    You can use multi db in the same redis instance (i.e one for each microservice) but it's discouraged because Redis is single threaded.

    The best way is to use one Redis for each microservices, then you can easily flush one of them without touching others.

    From my personal experience with a redis cache in production (with 2 million keys), there is no problem using EXPIRE. I encourage you to use it.