Senior
How would you design the interaction between microservices if a business process consists of several steps and requires data consistency between services?
sobes.tech AI
Answer from AI
The following approaches are typically used for designing interactions between microservices in a multi-step business process with data consistency requirements:
-
Orchestration
- A central service (orchestrator) manages the sequence of microservice calls.
- The orchestrator tracks the state of each step and makes decisions about continuation or compensation.
-
Choreography
- Each microservice reacts to events published by other services.
- There is no central controller; consistency is achieved through event exchange.
-
Using the Saga pattern
- The transaction is broken into a series of local transactions within each service.
- Compensating actions are performed in case of errors to rollback changes.
- Sagas can be implemented via orchestration or choreography.
-
Idempotency and repeatability
- All operations should be idempotent to safely repeat requests in case of failures.
-
Event-driven architecture
- Microservices exchange events through message brokers (Kafka, RabbitMQ).
- Ensures asynchrony and fault tolerance.
-
Monitoring and tracing
- It is important to have centralized monitoring and tracing (e.g., OpenTelemetry) to track the state of the business process.
Example: for an online store order
- The orchestrator initiates the "Payment Verification" step.
- Upon successful payment, an event "Payment Confirmed" is published.
- The delivery service subscribes to this event and begins preparing for shipment.
- If an error occurs at any step, the orchestrator initiates compensating actions (e.g., refund).
This approach allows ensuring data consistency between microservices without using distributed transactions, which are complex and poorly scalable.