Leveraging AWS SQS in Spring boot Applications using Spring Cloud.
Seamless Messaging: Integrating AWS SQS with Spring Boot for Scalable Microservices
Code Gang, Assemble!
Today, let’s explore the mélange of AWS SQS and Spring Boot applications. In modern distributed architectures, services need to talk to each other asynchronously without tight coupling. This is where Amazon Simple Queue Service (SQS) comes in.
With Spring Boot, integrating AWS SQS is effortless, enabling reliable message processing, decoupled services, and fault tolerance. Let’s dive deep into what SQS is, how to integrate it into Spring Boot, how it differs from Apache Kafka, and when to use it.
What is AWS SQS?
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that allows decoupled communication between microservices and distributed systems. It ensures that messages are delivered reliably without needing persistent connections between services.
A distributed system is a collection of computer programs that utilize computational resources across multiple, separate computation nodes to achieve a common, shared goal.
Why Use AWS SQS?
✅ Decouples Services → Prevents dependencies between microservices.
✅ Highly Scalable → Scales automatically to handle varying loads.
✅ Reliable Message Delivery → Supports at-least-once message delivery which means a messaging system's guarantee that messages will be delivered at least once to the recipient.
✅ Cost-Effective → Pay only for messages sent and received.
Types of SQS Queues:
Standard Queue → High throughput, best-effort ordering, at-least-once delivery .
FIFO Queue → Strict ordering and exactly-once processing (ideal for financial transactions).
Connecting AWS SQS to Spring Boot
Spring Boot offers multiple ways to interact with AWS SQS:
AWS SDK (Manual Approach) → You write code to send/receive messages.
Spring Cloud AWS (Recommended) → Provides auto-configuration and easy integration.
JMS (Java Message Service) → Abstracts SQS communication using standard messaging APIs.
How to Integrate AWS SQS in Spring Boot
Step 1: Add Dependencies
Include the required dependencies in pom.xml:
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
<version>3.0.2</version>
</dependency>Step 2: Configure AWS Credentials
Set up your AWS access details in application.yml:
yaml
aws:
access-key: YOUR_ACCESS_KEY
secret-key: YOUR_SECRET_KEY
region: us-east-1
sqs:
queue-name: my-queueAlternatively, configure credentials via the AWS CLI (Recommended for security):
sh
aws configureRecommended Security Practice: Instead of storing credentials in
application.yml, use AWS IAM Roles or environment variables.
Create the SQS Producer (Message Sender)
The producer will send messages to the configured SQS queue.
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.springframework.stereotype.Service;
@Service
public class SqsProducer {
private final SqsTemplate sqsTemplate;
public SqsProducer(SqsTemplate sqsTemplate) {
this.sqsTemplate = sqsTemplate;
}
public void sendMessage(String message) {
sqsTemplate.send("my-queue", message);
System.out.println("Message sent to SQS: " + message);
}
}🔹
SqsTemplateabstracts SQS interactions and handles message serialization.
Create the SQS Listener (Message Receiver)
The consumer will automatically receive and process messages using @SqsListener.
import io.awspring.cloud.sqs.annotation.SqsListener;
import org.springframework.stereotype.Service;
@Service
public class SqsConsumer {
@SqsListener("my-queue")
public void receiveMessage(String message) {
System.out.println("Received message from SQS: " + message);
}
}🔹
@SqsListener("my-queue")automatically listens for messages on the given queue.
Testing the Integration
You can test this by creating a simple REST controller to trigger message sending.
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/sqs")
public class SqsController {
private final SqsProducer producer;
public SqsController(SqsProducer producer) {
this.producer = producer;
}
@PostMapping("/send")
public String sendMessage(@RequestParam String message) {
producer.sendMessage(message);
return "Message sent: " + message;
}
}How to Test?
Start the Spring Boot application.
Send a message via Postman or browser:
http://localhost:8080/sqs/send?message=HelloSQSCheck the application logs. The consumer should print:
Received message from SQS: HelloSQSHandling JSON Messages
If your messages are in JSON format, you can deserialize them into Java objects.
Modify Producer to Send JSON Messages
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.springframework.stereotype.Service;
@Service
public class SqsJsonProducer {
private final SqsTemplate sqsTemplate;
private final ObjectMapper objectMapper = new ObjectMapper();
public SqsJsonProducer(SqsTemplate sqsTemplate) {
this.sqsTemplate = sqsTemplate;
}
public void sendUser(User user) throws JsonProcessingException {
String message = objectMapper.writeValueAsString(user);
sqsTemplate.send("my-queue", message);
System.out.println("User JSON sent: " + message);
}
}Modify Listener to Receive JSON Messages
import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.sqs.annotation.SqsListener;
import org.springframework.stereotype.Service;
@Service
public class SqsJsonConsumer {
private final ObjectMapper objectMapper = new ObjectMapper();
@SqsListener("my-queue")
public void receiveUser(String message) {
try {
User user = objectMapper.readValue(message, User.class);
System.out.println("Received User: " + user);
} catch (Exception e) {
e.printStackTrace();
}
}
}User Model Class
public class User {
private String name;
private int age; // Constructors, Getters, Setters }Dead Letter Queue (DLQ) Handling
To prevent message loss, configure a Dead Letter Queue (DLQ) in AWS SQS settings. This ensures that failed messages are stored for later debugging.
How Messaging Happens Between Services?
Message Flow:
Producer Service → Sends messages to the SQS queue.
SQS Queue → Stores messages until a consumer processes them.
Consumer Service → Fetches, processes, and deletes the message from SQS.
This approach decouples services, making the architecture resilient and fault-tolerant.
AWS SQS vs. Apache Kafka: Choosing the Right Messaging System
When building distributed systems, selecting the right messaging system is crucial. AWS SQS and Apache Kafka are both popular choices, but they serve different purposes. Let’s break down their differences.
1️⃣ Use Case & Purpose
AWS SQS is designed as a simple, scalable message queue for asynchronous communication between microservices. It ensures decoupling of services and guarantees message delivery.
On the other hand, Apache Kafka is a distributed event streaming platform built for handling real-time data streams at high throughput. It stores and processes events over time, making it ideal for event-driven architectures.
2️⃣ Message Ordering & Delivery Guarantees
SQS offers FIFO (First-In-First-Out) queues to ensure strict message ordering, but its standard queues follow an at-least-once delivery model with best-effort ordering.
Kafka, however, provides strict message ordering within partitions, making it more suitable for real-time event processing where order matters. It supports at-least-once and exactly-once semantics with proper configurations.
3️⃣ Message Retention
AWS SQS retains messages for a maximum of 14 days, after which they are permanently deleted. Once a message is processed and removed, it is no longer available.
Kafka, in contrast, stores messages persistently for a configurable retention period (days, weeks, or even indefinitely). This allows consumers to reprocess old messages and enables event replay.
4️⃣ Scalability & Throughput
SQS is fully managed and auto-scales based on demand. However, FIFO queues have limited throughput (up to 300 transactions per second per queue).
Kafka provides massively high throughput by distributing messages across partitions and brokers, allowing horizontal scaling. It is ideal for big data processing and log aggregation.
5️⃣ Message Processing Model
SQS follows a push-pull model, where producers push messages to the queue, and consumers must poll the queue to retrieve them.
Kafka operates on a publish-subscribe model, where producers publish events to topics, and multiple consumers can consume them in parallel, making it more efficient for real-time streaming.
6️⃣ Complexity & Management
AWS SQS is serverless and fully managed, requiring minimal setup and maintenance. You don’t need to manage clusters or storage, making it easier for microservices communication.
Kafka, on the other hand, requires setting up and managing brokers, partitions, and consumer groups. It needs careful tuning for fault tolerance and scalability, making it more complex but powerful.
7️⃣ Cost Considerations
SQS follows a pay-per-use model, where you are charged for the number of API requests (messages sent/received). It is cost-effective for simple messaging but may become expensive at high volumes.
Kafka’s cost depends on the infrastructure you provision. Running a Kafka cluster on AWS (MSK) or self-hosted involves EC2, storage, and network costs, which can be higher but offer more flexibility.
When to Use AWS SQS?
When you need simple, reliable message queuing with auto-scaling.
When your services require asynchronous communication without message persistence.
When you want a fully managed, low-maintenance solution.
When your workload consists of task queues, event-driven processing, or background jobs.
When to Use Apache Kafka?
When you need high throughput, real-time event streaming, or log processing.
When you want message replay ability and persistent storage.
When you have a complex event-driven architecture with multiple consumers.
When you need strict ordering and multi-subscriber processing of messages.
Limitations of AWS SQS
Not for Real-Time Streaming → Use Kafka or Kinesis for real-time data.
No Guaranteed Ordering in Standard Queue → Use FIFO queues for strict order.
Lower Throughput in FIFO Queues → Max 300 transactions per second.
No Message Processing Logic → Unlike Kafka, SQS does not store event history.
Closure
AWS SQS is a powerful, scalable, and easy-to-use message queue that helps decouple microservices and ensures reliable asynchronous processing. While it’s not a replacement for Kafka, it shines in simplicity, reliability, and cost-effectiveness.
For Spring Boot applications, integrating SQS provides a seamless messaging mechanism to build resilient, distributed systems.
Coming soon..In the upcoming posts we will look more AWS lambda and its involvement in event driven use-cases, how to get acquainted with AWS lambda with different AWS services and Spring boot application.
For Now, I am signing off..!!

