Member-only story
Moving Messages in AWS: Super-Fast Lambdas Use Batches
Turbo-charge your Lambda functions by sending messages in batches

A fundamental principle of building systems in a serverless world is that your functions should do their job as quickly as possible. The longer functions run, the more they cost.
Regardless of what your Lambda functions are doing, in the majority of use cases, a big slice of the processing time will be spent sending data somewhere else.
If the goal is super-fast Lambdas; and sending data costs so much time, we should be trying to make this action as efficient as possible. The solution is batching data before sending.
Scope
I’ll cover the following topics:
- What is batching?
- Performance: Just how much faster is batching?
- Complexity and error handling: Is batching just too complicated to be worth it?
- Don’t Repeat Yourself (DRY): Can using libraries make batching the most straightforward option?
What Is Batching?
Many AWS Services have batch-write APIs. Terminology can vary between services: “batch write”, “batch send messages,” and “put records,” for example.
However, they all mean the same thing — instead of sending a piece of data as an individual item, you can group separate items and send them in a single transaction, reducing processing time.
Performance
To prove just how much time is saved, let’s run some tests.
These tests will use two Lambda functions which will send data to an SQS queue. They are identical except one uses send_message
and the other uses send_message_batch
. We’ll use AWS X-Ray to profile the running time.
SQS send_message_batch
has a maximum batch size of ten messages.
If you would like to run the following examples yourself, you can! The code and deployment instructions are available on GitHub.
The method that deals with sending messages is broadcast_messages
.