Unravel › Java concepts › Messaging and Kafka
AWS SQS and SNS messaging
SQS is a managed queue and SNS is a managed publish-subscribe topic.
At a glance
SQS is a ticket tray: each ticket is taken by one worker and removed once finished.
- 1A producer calls sendMessage with a body; a consumer calls receiveMessage (long polling with waitTimeSeconds=20), processes the messages and then calls deleteMessageSpring Cloud AWS wraps this as @SqsListener("orders").
- 2A received message is hidden for the visibility timeoutIf it is not deleted in time, it reappears and another consumer gets it, which is how failures are retried.
- 3After maxReceiveCount failed attempts a redrive policy moves the message to a dead-letter queue for inspection
- 4SNS fan-out: publish once to a topic and several SQS queues subscribed to it each get a copyFIFO queues add ordering per message group and deduplication.
- Standard queues deliver at least once and may deliver out of order, so handlers must be idempotent
- Set the visibility timeout above the longest processing time (or extend it while working), otherwise slow messages are processed twice in parallel
- The queue is a distributed store; receiveMessage samples some servers (short polling) or all of them (long polling, preferred and cheaper)
ReceiveMessageRequest request = ReceiveMessageRequest.builder() .queueUrl(queueUrl) .maxNumberOfMessages(10) .waitTimeSeconds(20) .build();for (Message m : sqs.receiveMessage(request).messages()) { handle(m.body()); sqs.deleteMessage(DeleteMessageRequest.builder().queueUrl(queueUrl).receiptHandle(m.receiptHandle()).build());}- line 1 uses ReceiveMessageRequest, the core of AWS SQS and SNS messaging
- line 8 uses DeleteMessageRequest, the core of AWS SQS and SNS messaging
Read the full explanation (about 1 min)
SQS is a managed queue and SNS is a managed publish-subscribe topic. Together they decouple services: a sender drops messages and consumers pull and delete them at their own pace.
A mental model
SQS is a ticket tray: each ticket is taken by one worker and removed once finished. SNS is a loudspeaker that copies the announcement to every tray subscribed to it.
How it works
A producer calls sendMessage with a body; a consumer calls receiveMessage (long polling with waitTimeSeconds=20), processes the messages and then calls deleteMessage. Spring Cloud AWS wraps this as @SqsListener("orders").
A received message is hidden for the visibility timeout. If it is not deleted in time, it reappears and another consumer gets it, which is how failures are retried.
After maxReceiveCount failed attempts a redrive policy moves the message to a dead-letter queue for inspection.
SNS fan-out: publish once to a topic and several SQS queues subscribed to it each get a copy. FIFO queues add ordering per message group and deduplication.
Going deeper
Standard queues deliver at least once and may deliver out of order, so handlers must be idempotent. FIFO queues give order and deduplication but lower throughput.
Set the visibility timeout above the longest processing time (or extend it while working), otherwise slow messages are processed twice in parallel.
Messages are limited in size (256 KB). For larger payloads store the data in S3 and send a reference.
Under the hood
The queue is a distributed store; receiveMessage samples some servers (short polling) or all of them (long polling, preferred and cheaper). Deleting uses the receipt handle from the specific receive, which changes on each delivery, so an old handle cannot delete a message that was redelivered.
Common mistakes
- Deleting a message before the work succeeded.
- Visibility timeout shorter than processing.
- Non-idempotent handlers on a standard queue.
- Short polling in a tight loop.
How Unravel recognises it
When you paste code, Unravel looks for these signs of aws sqs and sns messaging. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@SqsListener - Types:
SqsClientSqsAsyncClientSnsClientAmazonSQSAmazonSNSAmazonSQSClientBuilderSendMessageRequestReceiveMessageRequestDeleteMessageRequestSendMessageBatchRequestPublishRequestSqsTemplateSqsMessageListenerContainerChangeMessageVisibilityRequestand 3 more - Configuration keys:
aws.sqs
Part of a learning path
- Reliable messaging and distributed patterns · previous: JMS and ActiveMQ messaging · next: Idempotency and duplicate handling
Related concepts
Frequently asked questions
What is AWS SQS and SNS messaging?
SQS is a managed queue and SNS is a managed publish-subscribe topic. Together they decouple services: a sender drops messages and consumers pull and delete them at their own pace.
What are the most common mistakes with AWS SQS and SNS messaging?
Deleting a message before the work succeeded. Visibility timeout shorter than processing. Non-idempotent handlers on a standard queue. Short polling in a tight loop.
How can I see AWS SQS and SNS messaging in real code?
Paste your own Java into Unravel and it will point out where aws sqs and sns messaging appears, explain how it works at the detail level you choose, and draw the flow of the code around it.