AWS Builder Center

Building a Reliable Event-Driven System Using Amazon SQS and AWS Lambda

Learn how to build reliable event-driven applications using Amazon SQS and AWS Lambda. Explore message queues, asynchronous processing, dead-letter queues, retries, and scalable serverless architectures through a real-world order processing example.

Building a Reliable Event-Driven System Using Amazon SQS and AWS Lambda

Introduction

Modern applications often need to process thousands of requests without slowing down the user experience.
For example, an e-commerce application may receive thousands of orders at the same time. Processing every order immediately can overload the backend and cause failures during traffic spikes.
A better approach is to use asynchronous processing.
AWS provides Amazon Simple Queue Service (SQS) for storing messages temporarily and AWS Lambda for processing those messages automatically.
Together, they provide a scalable and reliable architecture for event-driven applications.



1. What is Amazon SQS?

Amazon Simple Queue Service (SQS) is a fully managed message queuing service provided by AWS.
Instead of sending a request directly from one application component to another, the producer can place a message into an SQS queue.
The consumer can then process that message independently.
For example:
Producer → SQS Queue → Consumer
This creates a layer of separation between different components of an application.
SQS is commonly used for:
  • Background job processing
  • Order processing
  • Email processing
  • Data processing
  • Event-driven applications
  • Decoupling microservices



2. Why Use a Queue?

Consider an online shopping application.
When a customer places an order, several operations may need to happen:
  1. Validate the order.
  1. Process payment.
  1. Update inventory.
  1. Generate an invoice.
  1. Send an email notification.
If all these operations happen synchronously, a temporary failure in one service can affect the entire request.
With SQS, the application can place the order information into a queue and immediately return a response to the customer.
The background processing can happen later.
User
↓
Application
↓
SQS Queue
↓
Lambda
↓
Order Processing
This improves reliability and allows different components to scale independently.



3. How AWS Lambda Works with SQS

AWS Lambda can be configured to automatically process messages from an SQS queue.
When messages are available, Lambda receives a batch of messages and executes the function.
The basic workflow is:
Application
↓
SQS Queue
↓
Lambda
↓
Business Logic
↓
Database / Notification / Other AWS Services
The application does not need to continuously check the queue manually.
AWS manages the integration between SQS and Lambda.



4. Creating an SQS Queue

To create a queue, open the Amazon SQS console and choose Create queue.
There are two major types of SQS queues:

Standard Queue

Standard queues provide very high throughput and support at-least-once message delivery.
They are suitable for applications where extremely high scalability is required.

FIFO Queue

FIFO stands for First-In-First-Out.
FIFO queues are designed for situations where message ordering and controlled duplicate processing are important.
For example, an application processing sequential financial transactions may require ordered processing.



5. Creating the Lambda Function

Next, create an AWS Lambda function.
The Lambda function will receive messages from the SQS queue and process them.
A simplified Python example looks like this:
import json

def lambda_handler(event, context):

for record in event["Records"]:
message = json.loads(record["body"])

print("Processing order:", message["orderId"])

return {
"statusCode": 200
}
The  event["Records"]  collection contains the messages received by the Lambda function.
The application can then perform operations such as updating DynamoDB, processing payments, or sending notifications.



6. Connecting SQS with Lambda

After creating both services, configure an event source mapping between SQS and Lambda.
This allows Lambda to poll the queue and invoke the function when messages are available.
The overall flow becomes:
Producer
|
| Send Message
↓
SQS Queue
|
| Event Source Mapping
↓
AWS Lambda
|
↓
Application Logic
This architecture allows the producer and consumer to operate independently.



7. Handling Failed Messages

Failures are unavoidable in distributed applications.
For example, a Lambda function may fail because of:
  • Temporary database problems
  • Network errors
  • Invalid input
  • External service failures
SQS supports retry behavior when message processing fails.
If a message cannot be processed successfully after repeated attempts, it can be moved to a Dead-Letter Queue (DLQ).
The architecture becomes:
Main SQS Queue
↓
Lambda
↓
Processing
|
| Failure
↓
Dead-Letter Queue
The DLQ allows developers to inspect failed messages separately instead of losing them.
This is especially useful for debugging and recovery.



8. Visibility Timeout

SQS provides a feature called visibility timeout.
When Lambda receives a message, that message temporarily becomes invisible to other consumers.
This prevents the same message from being processed simultaneously by multiple consumers under normal conditions.
If processing succeeds, the message can be removed from the queue.
If processing fails, the message can become visible again and be retried according to the configured behavior.



9. Real-World Example: Order Processing

Consider an online shopping platform.
When a customer places an order:

Step 1

The frontend sends the order to the backend.

Step 2

The backend places the order information into an SQS queue.
Example message:
{
"orderId": "ORD1024",
"productId": "P501",
"quantity": 2
}

Step 3

The customer does not need to wait for every background operation to complete.

Step 4

Lambda receives the message from SQS.

Step 5

Lambda processes the order.
For example, it can:
  • Update inventory.
  • Store order information in DynamoDB.
  • Generate an invoice.
  • Trigger an SNS notification.

Step 6

If processing fails repeatedly, the message can be moved to the Dead-Letter Queue.
This creates a reliable asynchronous order processing system.



10. Benefits of SQS and Lambda

Decoupling

SQS separates application components so that one service does not have to depend directly on another.

Scalability

Lambda can process increasing workloads automatically, while SQS acts as a buffer during traffic spikes.

Reliability

Messages can remain in the queue until they are successfully processed.

Fault Tolerance

Failed messages can be retried and eventually moved to a Dead-Letter Queue.

Serverless Architecture

There is no need to manage queue servers or application servers manually.

Asynchronous Processing

Long-running background operations can happen independently from the user's request.



11. Common Use Cases

SQS and Lambda can be used for many applications:
E-commerce
Process orders, inventory updates, and invoices.
Email Processing
Queue email requests and process them asynchronously.
Image Processing
Queue uploaded images for background processing.
Data Pipelines
Process large amounts of data without blocking the main application.
Microservices
Allow independent services to communicate through messages.
Notification Systems
Queue notification requests before processing them.



12. SQS vs SNS

SQS and SNS are both AWS messaging services, but they solve different problems.
SQS is primarily a queue used to store messages until they are processed.
SNS is primarily a publish/subscribe service used to distribute messages to multiple subscribers.
For example:
SQS:
Producer → Queue → Consumer
While:
SNS:
Publisher → SNS → Multiple Subscribers
They can also be used together.
For example, SNS can distribute an event to multiple SQS queues, allowing different services to process the same event independently.



Conclusion

Amazon SQS and AWS Lambda provide a powerful foundation for building reliable event-driven applications.
SQS acts as a buffer between application components, while Lambda processes messages automatically without requiring server management.
Features such as retries, visibility timeout, and Dead-Letter Queues make the architecture more reliable and fault tolerant.
By combining SQS with Lambda, developers can build scalable asynchronous systems that continue to work efficiently even when application traffic increases.
Keep Learning and Keep Building!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article