How to implement asynchronism with AWS Lambda to improve the performance of your applications?
Integrating asynchronism in your applications significantly improves their performance, especially in intensive tasks such as database updates. In this use case, we will learn how to asynchronously add "likes" to users in an application, using AWS services such as API Gateway, SQS and Lambda.
What is the architectural flow of the application?
In order not to saturate the databases and respect the limits of Lambda, we will use an architecture focused on asynchronous services. Here is how it is structured:
- API Gateway: this endpoint receives the request from the client with the user ID to which the likes will be added.
- SQS (Simple Queue Service): Temporarily stores the requests in a queue, ensuring that the data is processed without being lost.
- Lambda: It will be configured to process the SQS queue requests, ensuring that only one instance of Lambda is active at a time.
The connection is direct from API Gateway to SQS, without going through an intermediate Lambda, optimizing the data flow and protecting your database from being overloaded.
How to configure plugins to optimize the API Gateway- SQS connection?
Thanks to the flexibility of the serverless framework, you can use available plugins that facilitate the use of AWS. To connect API Gateway to SQS:
-
Search for the rightplugin that allows the necessary connections. An example is a plugin capable of interacting with any Amazon resource, but in this case it explicitly looks for SQS.
-
Installing the plugin: This is done by installing the dependency from the terminal and configuring the serverless.yml
file:
plugins: - serverless-apigateway-service-proxy
custom: apiGatewayServiceProxies: - sqs: path: /like method: post queueName: likeQueue cors: true responseMessage: "Success"
-
Additional plugins: Consider the Lift
plugin that allows you to create queues and assign Lambdas without writing a lot of code. It includes settings such as batch size to control how many messages the Lambda processes simultaneously.
How to create and configure the Lambda Worker?
The Lambda Worker is responsible for processing the requests stored in the queue. Here are the essential steps:
-
Create the handler: define a function in a handler.js
file inside a dedicated folder, for example:
module.exports.handler = async (event) => { console.log(event); };
-
Configure the worker in serverless.yml
: This is where the Lambda function, its handler and other necessary configurations are defined:
constructs: likeQueue: type: queue batchSize: 1 worker: handler: likeUser/handler.handler package: exclude: - '**' include: - likeUser/** reservedConcurrency: 1
-
Deploy the application: Use serverless framework commands to deploy and verify the changes.
How to test the asynchronous system?
After having configured and implemented the architecture, it is crucial to test it to verify its correct operation:
-
Send a POST request through Postman with the required body, making sure that the API Gateway endpoint responds correctly.
-
Check Lambda Worker logs to make sure it is processing the messages. Use the serverless framework command line to view the logs directly.
With this application, you will be able to handle asynchronous processes effectively, optimizing response times and ensuring the consistency of your application. Keep exploring and learning about more advanced features!
Want to see more contributions, questions and answers from the community?