Quick revision
The short answer
An SQS consumer loops on receiveMessage with waitTimeSeconds(20) for long polling and up to 10 messages per call, processes each message, and deletes it with its receipt handle only after processing succeeds. While a consumer works on a message, the message is invisible for the visibility timeout; if it is not deleted in time it reappears, and after maxReceiveCount failed attempts a redrive policy moves it to a dead-letter queue. Spring Cloud AWS hides the loop behind @SqsListener, which polls, deletes on success, and lets us set concurrency and acknowledgement modes.
What to remember
In this answer, we saw that an SQS consumer is a loop of receive, process and delete. Long polling with a 20-second wait removes empty responses and their cost, the visibility timeout hides a message while we work and must be longer than processing, deletion with the receipt handle comes only after success, and a redrive policy with maxReceiveCount moves poison messages to a dead-letter queue. Spring Cloud AWS's @SqsListener does the polling and deletion for us and lets us tune concurrency and acknowledgement.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An SQS consumer loops on receiveMessage with waitTimeSeconds(20) for long polling and up to 10 messages per call, processes each message, and deletes it with its receipt handle only after processing succeeds. While a consumer works on a message, the message is invisible for the visibility timeout; if it is not deleted in time it reappears, and after maxReceiveCount failed attempts a redrive policy moves it to a dead-letter queue. Spring Cloud AWS hides the loop behind @SqsListener, which polls, deletes on success, and lets us set concurrency and acknowledgement modes.
Background to this question
Amazon SQS is the simplest way to hand work from one Java service to another on AWS, and almost every backend developer meets it. But SQS has no push and no automatic acknowledgement: the consumer must ask for messages, and must delete them itself. Getting that loop wrong leads to the usual production bugs, such as messages processed twice, messages lost because they were deleted too early, or a bill full of empty receive calls. In this answer, we will recap how SQS delivery works, write the consumer loop with the AWS SDK 2.x, look at long polling, visibility timeout, deletion and dead-letter queues one by one, and then see how Spring Cloud AWS removes most of the code.
1A Quick Recap of How SQS Delivers Messages
An SQS queue stores messages until a consumer receives and deletes them. Receiving does not remove a message. Instead SQS hides it for a period called the visibility timeout, 30 seconds by default, and gives the consumer a receipt handle, a token that identifies this particular receipt. If the consumer deletes the message with that handle before the timeout ends, the message is gone. If not, the message becomes visible again and another receive can get it.
This design is what makes SQS reliable: a consumer that crashes halfway never deletes the message, so the work is retried. It also means a standard queue delivers at least once, and our processing must cope with seeing the same message again.
7 messages in order. Play them, or step through them one at a time.
If the delete never comes, the message becomes visible again.
2Writing the Consumer Loop With SDK 2.x
Here is the core of a consumer. It builds one SqsClient, then repeatedly asks for a batch of messages, processes each, and deletes the ones that succeeded in a single batch call.
Notice that a failed message is simply not deleted. We do not need to send it back; it reappears on its own once the visibility timeout expires. Notice also that deleteMessageBatch can partly fail, so production code reads the failed list in the response and logs or retries those deletes.
3Long Polling and Why It Saves Money
Without waitTimeSeconds, a receive call returns immediately, and if the queue is empty the answer is an empty list. A loop like ours would then call SQS thousands of times a minute and pay for each empty request. It may also miss messages, because short polling only asks a subset of SQS servers.
With waitTimeSeconds(20), the maximum, SQS holds the request open until at least one message arrives or 20 seconds pass. An idle consumer makes about three calls a minute instead of thousands, and messages are returned as soon as they arrive. We can also set ReceiveMessageWaitTimeSeconds on the queue so every consumer gets long polling by default. The only thing to check is that the HTTP client's read timeout is longer than 20 seconds, or the client gives up before SQS answers.
4Visibility Timeout, Deletion and Dead-Letter Queues
The visibility timeout must be longer than the time it takes to process a message, including retries inside our code. If processing takes 40 seconds and the timeout is 30, the message reappears while we are still working on it and another consumer picks it up. We set the timeout on the queue, or per receive with visibilityTimeout(), and for long jobs we can extend it with changeMessageVisibility.
Deletion must come after success, never before. Deleting first and then failing loses the message for good. Deleting after success means a crash between processing and deleting causes a second delivery, which is why consumers are idempotent.
Some messages fail every time, for example a malformed payload. Without a limit they would loop forever. A redrive policy on the queue names a dead-letter queue and a maxReceiveCount: once a message has been received that many times without being deleted, SQS moves it to the dead-letter queue, where we alert on it and inspect it.
5How Spring Cloud AWS Simplifies It
Spring Cloud AWS 3.x, built on SDK 2.x, turns all of the above into an annotated method. The container behind @SqsListener runs the polling loop with long polling, converts the body to our type with Jackson, calls our method on a pool of threads, and deletes the message when the method returns normally. If the method throws, the message is not deleted and comes back after the visibility timeout.
The acknowledgement mode decides when deletion happens: on success by default, always, or manually, where we inject an Acknowledgement parameter and call acknowledge() ourselves. Settings such as maxConcurrentMessages, the poll size and the visibility timeout for received messages are attributes or container factory properties, so we tune behaviour without writing loop code. Check the reference documentation for your Spring Cloud AWS version for the exact attribute names.
6Follow-up questions
Why must SQS consumers be idempotent?
Because a standard queue delivers at least once. A message can arrive twice when processing outlasts the visibility timeout, when the consumer crashes after processing but before deleting, or occasionally because of how SQS stores copies. If handling it twice charges a card twice, we have a bug. So we make the effect of a second delivery harmless, for example by recording processed message ids or business keys in a table with a unique constraint, or by using upserts and conditional updates that produce the same result when repeated.
Should I process the 10 messages of a batch in parallel?
Often yes, for throughput, but the visibility timeout starts for all 10 at receive time, so the slowest message decides whether the others are still safe. Smaller batches or parallel processing keep a slow message from pushing the rest past their timeout. Spring Cloud AWS processes messages of a poll concurrently by default, up to maxConcurrentMessages.
What happens if the delete call itself fails?
The message stays in the queue and becomes visible again after the timeout, so it is processed a second time. That is another reason for idempotency, and for checking the failed entries in a batch delete response.
7Conclusion
In this answer, we saw that an SQS consumer is a loop of receive, process and delete. Long polling with a 20-second wait removes empty responses and their cost, the visibility timeout hides a message while we work and must be longer than processing, deletion with the receipt handle comes only after success, and a redrive policy with maxReceiveCount moves poison messages to a dead-letter queue. Spring Cloud AWS's @SqsListener does the polling and deletion for us and lets us tune concurrency and acknowledgement.
Open with
The consumer loop builds one SqsClient and repeatedly calls receiveMessage with a ReceiveMessageRequest that sets the queue URL, maxNumberOfMessages of 10 and waitTimeSeconds of 20. The wait time turns on long polling: SQS holds the call until a message arrives or 20 seconds pass, so an idle consumer makes a few calls a minute instead of thousands of empty ones, which saves cost.
Then, if they want more
When I receive a message it is not removed, it becomes invisible for the visibility timeout, 30 seconds by default. I process it and only then delete it with deleteMessage or deleteMessageBatch, using its receipt handle. If processing fails I simply do not delete it, and it reappears after the timeout for another try.
So the timeout must be longer than my worst processing time, otherwise a second consumer gets the same message while I am still working. To stop messages that always fail from looping forever, the queue has a redrive policy with a dead-letter queue and a maxReceiveCount, say 5, and I alert on the dead-letter queue.
Because delivery is at least once, the handler must be idempotent. With Spring Cloud AWS, I just write a method annotated with @SqsListener on the queue name.
The framework runs the long-polling loop, converts the JSON to my type, deletes the message when my method returns, leaves it when it throws, and lets me set concurrency, poll size and acknowledgement mode, including manual acknowledgement.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- AWS documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write the core loop of a Java SQS consumer using the AWS SDK 2.x, and explain long...": https://scrivio-interview-library.netlify.app/spring-boot/aws/fedf4aa959/