Optimizing Event-Driven Design in AWS Lambda
Tags: serverless, aws, eventdriven
Published: 2021-01-15 02:27:34

AWS Lambda is a Serverless computer service from AWS. One of the most unique aspects of AWS Lambda is its pay-per-use fee structure. Because you only pay for what you use, you want to design events that reduce execution time as much as possible. Event-driven design ensures greater cost savings and the optimal serverless configuration.

This article introduces event-driven design and discusses how you can optimize your Lambda instance using event-driven design.

## What is event-driven design?

In general, event-driven design is a type of programming model that executes processing once triggered by a predefined action or state change. In the AWS serverless architecture, there are many cases where AWS Lambda is triggered according to actions and states from various AWS services.

For example, AWS Lambda often begins with situations such as the following and proceeds with subsequent processing:

- A file is uploaded to S3 → AWS Lambda is triggered to read the file and perform conversion processing
- A request arrives at an API Gateway → AWS Lambda receives the request and returns an appropriate response
- Data is updated in DynamoDB → AWS Lambda performs related processing such as sending notifications associated with the data update

## Benefits of event-driven design

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wh4if6zcbba4rler186m.png)

- Lambda is connected like a string to each AWS service by an event.
- The execution time of the computing resource (Lambda) is limited from the triggering of the event to its completion, and requires no unnecessary waiting time.
- Using queuing services such as SQS also has the advantage of facilitating fault tolerance and error handling.

## Compare with polling
  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vk6zki8tewx7z694kzqi.png)
  
In the case of polling, requests are sent at regular intervals to detect changes in the status of other services and linked systems. Since it’s not possible to accurately grasp when the state changes, this function requires a lot of computing resources. Polling is one of the reasons why users see increased Lambda expenses in their serverless configurations.
  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s1lg4nenr5787c6ys2zf.png)
  
In an event-driven configuration, processing is performed only when needed, eliminating unnecessary waiting periods and minimizing execution time. It’s also possible to further optimize your serverless architecture by separating time-consuming processing with help from another AWS service or implementation that supports multithreading.

## Cost benefits of event-driven design
Event-driven design allows you to cost-effectively execute events rather than waiting for state changes and processing of external actions. An event-driven configuration reduces the execution time of these processes, thus reducing AWS Lambda costs, which are highly dependent on execution time. In other words, event-driven design helps you avoid higher costs incurred from unnecessary waiting time. 

For those with complex system configurations, the use of services such as Step Functions can also help simplify event-driven design. 

By keeping an event-driven design in mind, you can reduce the cost of computing resources and thus the costs of your AWS costs.