Choreography

In DDD, choreography is an alternative approach to orchestration for coordinating interactions between different bounded contexts or services. In choreography, each service or bounded context is responsible for initiating and responding to events without a central coordinator. Each service or bounded context is designed to publish events that other services can listen to and react to. This approach promotes a more decentralized architecture where services can evolve independently.

In choreography, each bounded context or service publishes domain events that describe what has happened in the system, and other contexts or services subscribe to these events and react accordingly. There is no central orchestrator that is responsible for coordinating the interactions between different services or bounded contexts.

Example

Imagine a retail store with a customer loyalty program. Whenever a customer makes a purchase, the system needs to check if the customer has enough loyalty points to redeem a reward. If so, the system should deduct the loyalty points from the customer’s account and apply the reward to the purchase.

In a choreography approach, each service involved in this process would publish an event to a message broker whenever it completes its work. The system would have the following services:

  • Order service: responsible for placing and processing orders
  • Loyalty service: responsible for managing the customer loyalty program and rewards
  • Payment service: responsible for handling payment processing

The services would interact with each other through messages, as follows:

  1. The order service publishes an event indicating that a new order has been placed.
  2. The loyalty service subscribes to this event and checks the customer’s loyalty points. If the customer has enough points to redeem a reward, the loyalty service publishes an event indicating that a reward has been applied.
  3. The payment service subscribes to the reward applied event and applies the appropriate discount to the order.

In this scenario, the services work together in a decentralized manner, with each service publishing and subscribing to events as needed. There is no central controller or orchestrator managing the interactions between services. This approach can be more scalable and flexible than orchestration, but also more complex to implement and maintain.

Choreography can be more complex than orchestration, as each service or bounded context needs to handle the events it receives correctly. However, it can also be more resilient, as each service can evolve independently, and failures in one service will not necessarily affect the entire system.