AWS Serverless and Microservices — Part 4

Continuing with our Design Journey of YAFDP Application using AWS Serverless Stack from earlier parts —

Part 1How to structure Microservices using AWS Serverless stack and using Synchronous Request-Response pattern with API Gateway and Lambda

Part 2Covers Designing of Asynchronous event submission using API Gateway, SQS and Lambda. And how to implement Saga Orchestration Pattern with Serverless Step Function service.

Part 3Covers approach of sending the status of asynchronous task submission, in our case New Food Order Placement, to the client.

Part 4 (This blog) — Covers Placing New Food Delivery Order using Choreograph Saga Event Driven Pattern.

Part 5Using Serverless stack for designing Notification Service.

In Part 2, we implemented New Food Delivery Order Use case using Orchestration Saga Pattern. In this blog, we will implement the same flow with Choreograph Saga Pattern using AWS Serverless Stack. The pattern is called as Event Driven Pattern.

Choreograph based Saga is an Event based pattern in which Services emit events as and when an activity is performed, and other services listen for the events they are interested in. And based on the event received, the service performs the next activity and then it further emits events. It’s an extreme way of Loose coupling between Services.

To get more details on this pattern, do take a look at the below blog —

When working with Event based system, the first thing is to identity all the events which can flow into the system. Let’s identity the same for our Use case —

The list of events mentioned above covers all the possibilities of activities that can happen while placing a New Food Delivery Order.

Once the events are identified, the next step if to identity which Service shall be interested in which events. For our Use case —

AWS offers Event Bridge, a serverless event bus that makes it easy to connect applications together using data from your own applications. It is a highly available service, helps in simplified event routing and also helps to improve the overall availability of the system. For more details on Event Bridge, take a look at below link —

Captured below is the sequence of events emitted by different services for the New Food Order Delivery use case —

1) New Order Placement — Order_Initiated event emitted
2) New Order Placement — Payment_Success event emitted
3) New Order Placement — Restaurant_Confirmed_Order event emitted
4) New Order Placement — Delivery_Scheduled event emitted

The New Order Placement flow above highlights how different services integrate with each other without actually knowing about them. All they care about is emit the correct event when the activity occurs.

With Event Driven Architecture, it is also very easy to add new features. Let’s say, we now want to give Loyalty points to the user when a New Order is placed. For this to implement, all we need to do is create a new service, let’s call it as Loyalty Service and have it subscribed to the Restaurant_Confirmed_Order event. Once the Order is confirmed, Loyalty service adds points against the end user.

That’s it for this blog. In the next and final blog of the series, I shall cover designing Notification Service.

Till then, Cheers!!!

#AWS #CloudArchitect #CloudMigration #Microservices #Mobility #IoT