DEV Community

Cover image for Event-Driven AWS: Implementing Choreography Pattern
Axel
Axel

Posted on • Edited on

Event-Driven AWS: Implementing Choreography Pattern

Definition

In Choreography, there is no central controller. Each service knows what to do and when to do it based on events. Services communicate with each other by emitting and responding to events, acting independently. The workflow emerges as a result of these independent actions, with no single service having visibility or control over the entire process.

Key Concepts of the Choreography Pattern

  • Decoupling Services: EventBridge enables different services to react to events without needing direct connections between them.
  • Scalability: Services only process events relevant to them, and EventBridge automatically scales to handle a large number of events.
  • Flexible Routing: You can set up rules that route events to specific targets based on event content.
  • Centralized Event Bus: EventBridge can aggregate events from multiple sources, making it easy to manage complex workflows.

Use Case

In this scenario, we follow the flow of an aircraft booking using the Choreography with EventBridge :

Choreography Pattern

Services

  1. Booking Service: Handles customer flight bookings and creates events like BookingCreated.
  2. Payment Service: Processes payments and emits events like PaymentConfirmed or PaymentFailed.
  3. Seat Assignment Service: Assigns seats to customers based on availability and preferences.
  4. Ticket Service: Issues tickets after payment is confirmed.
  5. Amazon SES Notification: Sends confirmation emails or SMS messages to customers.

Event-Driven Flow using EventBridge

  1. Booking Created:
    • The Booking Service creates a new flight booking and publishes a BookingCreated event to EventBridge.
  2. Payment Processing:
    • The Payment Service listens for the BookingCreated event on EventBridge.
    • Once the payment is processed, the service emits a PaymentConfirmed event (or a PaymentFailed event if payment fails).
  3. Seat Assignment:
    • The Seat Assignment Service listens for the PaymentConfirmed event and assigns a seat to the customer.
    • After a seat is assigned, it may emit a SeatAssigned event.
  4. Ticket:
    • The Ticket Service listens for the SeatAssigned event. Once payment is confirmed, the service issues a ticket to the customer.
    • After the ticket is issued, the Ticket Service emits an event to the Amazon SES Notification.
  5. Notifications:
    • The SES Notification receive the event and sends appropriate notifications (emails) to the customer.

EventBridge Failover Behavior

When Amazon EventBridge fails to invoke a target, it offers several mechanisms to handle such failures effectively:

  • Retries: EventBridge will automatically retry sending the event to the target based on the retry policy set for the rule (e.g. The number of retry attempts, the delay between retries, The maximum time) before treating the event as a permanent failure.
  • Dead Letter Queue (DLQ): If an event can't be delivered after the configured retry attempts, EventBridge will send the event to a Dead Letter Queue (DLQ). You can use an Amazon SQS queue or an Amazon SNS topic as the DLQ to capture failed events for further analysis.
  • FailedInvocations Metric: EventBridge tracks failed invocations through the FailedInvocations metric in Amazon CloudWatch. This helps you monitor and troubleshoot issues related to event delivery.
  • EventBridge Stops Trying: If 24 hours have passed since the first attempt to deliver an event and the target hasn't processed it, EventBridge will stop retrying. At this point, the event is marked as a permanent failure.

In action

You can go to this post to see it in action : here

Conclusion

In conclusion, the Choreography Pattern on AWS, where independent services communicate through events using Amazon EventBridge. Each service reacts to events without centralized control, enabling flexibility, scalability, and decoupling. It describes a real-world use case of flight booking, with services like booking, payment, seat assignment, and ticketing all emitting and responding to specific events. The process flows seamlessly, with notifications sent to customers via SES after key events like payment confirmation and seat assignment.

Top comments (0)