10

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.

tom redfern
  • 28,053
  • 12
  • 86
  • 116
kaarthick raman
  • 375
  • 2
  • 8
  • 27

3 Answers3

13

it's possible to use the same Redis for multiple microservices, just make sur to well prefix your redis cache key 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 touch others.

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

khanou
  • 1,134
  • 9
  • 15
  • Thanks for your response. Do u mean to say N redis instances for N microservice right? – kaarthick raman Apr 26 '16 at 03:29
  • u recommend to use N instances to make flushall easily but why will we flushall when we have flushdb? – kaarthick raman Apr 29 '16 at 05:02
  • It's more like when something goes wrong on one of your redis, there is no impact on other microservices. But yes, If you use the same redis you can use flushdb. But keep in mind that redis is single thread and flushdb O(N) will block other commands – khanou Apr 29 '16 at 09:12
  • If you have a redis per microservice why wouldn’t you just use an in-memory cache within the web app? saves you network latency and deserialization cost. – Dirk Boer Aug 07 '19 at 10:22
  • @DirkBoer depend of the number of item you will store into your in memory cache and depend of the size of the machine you are running on. The cache can be used to save call to your DB (for big querry) and store millions of items that couldn't be store at the same place that your microservices – khanou Aug 08 '19 at 11:33
2

Please find below the answer to all your questions -

  1. Can all microservices data(cached) be kept under a single instance of redis server? Ans - Yes you can keep all the data under single redis instance, all you need to do is to set that data using different key Name. As redis is basically a Key-Value Database.

  2. Should every microservice have its own cache database in redis? Ans - Not required. Just make different key for each microservice. Also please note that you can use colon (:) to make folders in redis, to identify different microservices easily on Redis Desktop Manager. Example - Key Name X:Y:Z, here Z is placed in Y folder and Y is in X. SO you will get a folder kind of structure. That would be helpful to differentiate different microservices.

    1. How to refresh cache data without setting EXPIRE? Since it would consume more memory. Ans - You can set data again on the same key if you have any change in Microservice response. That Key value will get over written in that case.
1
  1. Can all microservices data(cached) be kept under a single instance of redis server? In microservice architecture it's prefirible "elastic scale SaaS". You can think your Cache service is perse a microservice (that will response on demand) Then you have multiple options here. The recommended practice on data storage is sharding https://azure.microsoft.com/en-us/documentation/articles/best-practices-caching/#partitioning-a-redis-cache .See the diagram below for book Microservices, IoT and Azure

  2. Should every microservice have its own cache database in redis? It's possible to still thinking "vertical partition" but you should consider "horizontal partitions" so again consider sharding; additionally It's not a bad idea to have "local cache" specialy to avoid DoS "Be careful not to introduce critical dependencies on the availability of a shared cache service into your solutions. An application should be able to continue functioning if the service that provides the shared cache is unavailable. The application should not hang or fail while waiting for the cache service to resume."

  3. How to refresh cache data without setting EXPIRE? Since it would consume more memory. You can define your synch polices; I think cache is suitable for things that have few changes. "It might also be appropriate to have a background process that periodically updates reference data in the cache to ensure it is up to date, or that refreshes the cache when reference data changes."

Microservice Architecture For cahe best practices check Caching Best Practices