Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 523
kaarthick raman Avatar asked Apr 25 '16 04:04

kaarthick raman


3 Answers

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.

like image 181
khanou Avatar answered Sep 27 '22 16:09

khanou


  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

like image 35
Alberto Gurrion Avatar answered Sep 27 '22 17:09

Alberto Gurrion


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.
like image 21
Akash Gangrade Avatar answered Sep 27 '22 18:09

Akash Gangrade