Caching in a microservice with multiple replicas in k8s

Viewed 43

I've a Golang based micro-service which has an in-memory cache as follows:

  1. Create object -> Put it in cache -> Persist
  2. Update object -> Update the cache -> Persist
  3. Get -> Get it from the cache
  4. Delete -> Delete cache entry -> Remove from data store.

On a service re-start, the cache is populated from the data store.

The cache organizes the data in different ways that matches my access patterns.

Note that one client can create the object, and other clients can update it at a later point in time.

Everything works fine as long as I've one replica. But, this pattern will break when I increase the replica count in my deployment.

If I have to go to the DB for each GET, it defeats the purpose of the cache. The first thought is, to move the cache out. But, this seems like a fairly common problem when moving to multi-replica microservices. So, curious to understand alternatives.

Thanks for your time.

1 Answers

Mainly many things depends on how you structure your application.

  1. One common solution is use Redis Cache or Distributed Cache. Here advantage is that your all services will go to same cache to manage object. This will give more consistent data.

  2. Another approach that you can take and this will be some how more complex. Try to use sharding.

    • For Get Operation based on Id of object, you have to route request to specific instance. That instance will have that object in cache. If not then it read from db and put it in that instance cache. Eachtime for that object it will go that instance. This is applicable to Update and Delete operation.

    • For create operation.

      • If you want DB generate Id automatically for object then there is once chance object created in DB and then it return that Id and based on Id you have to route request and that way for first access after creation will be from DB but after that it will be in cache of that instance.
      • If you have provision that Id can be manually generated then during creation if you have to prefix Id with something that map to instance.

Note : In distributed system , there is no one solution. You always have to decide which approach works for you scenario.

Related