Search code examples
microservicesdistributed-computingsaga

A distributed sequence of actions over services that can horizontally scale


I have a microservice distributed sequence of action. Service A needs to tell service B to do something and once that is complete it will tell service C. The sequence is important so I'm using the saga pattern as you can see.

My issue is that service B can scale and each instance needs to receive the message and complete the action. The action must happen on every service B instance. Then service C should only run once all the service B instances have completed their task.

It is a cache purge that must happen on each instance. I have no control over this architecture so the cache for service B is coupled to each instance. I would have a shared cache for the instances if I could.

I have come up with this orchestration solution but it requires maintaining state and lots of extra code to handle edge cases which I would like to avoid.

  1. service A sends the same message to all service B instances which it knows about
  2. all service B instances send success to service A
  3. On the final service B success, service A messages service C

Is there a better alternative to this?


Solution

  • Assuming that you can't rearchitect service B, you've captured the essential complexity of the operation: A will have to track instances of service B and will have to deal with a ton of edge cases. The process is fundamentally stateful.

    If the cache purge command is idempotent (i.e. you don't care if it happens multiple times in the process) you can simplify some of the edge case handling and can get away with the state being less durable (on failure you can start from the beginning instead of needing to reconstruct where you were in the process).