Retrieve multiple messages from SQS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When you consume Amazon SQS efficiently, you usually do not receive one message at a time. SQS lets you request up to 10 messages in a single ReceiveMessage call, which reduces API overhead and improves throughput. The important part is that receiving a batch is only half the job; you also need visibility-timeout handling and deletion after successful processing.
The Main SQS Limit to Remember
The ReceiveMessage API can return at most 10 messages per request. That means “multiple messages” in SQS really means a batch of up to 10, not an arbitrary large pull.
The most useful parameters are:
- '
MaxNumberOfMessages: maximum messages to return, up to 10' - '
WaitTimeSeconds: enables long polling' - '
VisibilityTimeout: controls how long received messages stay hidden from other consumers' - '
MessageAttributeNames: if you need custom message attributes'
A Boto3 Example
This Python example receives up to 10 messages with long polling.
If the queue is empty, Messages may be missing entirely, so the get(..., []) pattern matters.
Delete Messages After Successful Processing
Receiving a message does not remove it from the queue. SQS hides it temporarily using the visibility timeout. After you process it successfully, delete it explicitly.
Batch deletion reduces API calls just like batch receipt does.
Why Long Polling Matters
If you call ReceiveMessage repeatedly with short polling, you can waste requests and still get empty responses even when messages are arriving.
Setting WaitTimeSeconds to a positive value enables long polling. That makes the consumer wait briefly for available messages, which usually lowers cost and improves efficiency.
Long polling is often the default recommendation unless you have a very specific low-latency polling requirement.
Visibility Timeout Is Part of Correctness
If processing takes longer than the visibility timeout, the message can become visible again before your worker finishes. That leads to duplicate processing.
So choose the timeout to match realistic processing duration, or extend it while work is still in progress.
This is especially important because SQS is designed for at-least-once delivery. Your consumer should already be prepared for duplicate deliveries.
Batch Processing Strategy
A good consumer loop usually does this:
- receive up to 10 messages
- process them one by one or in a small internal batch
- delete only the messages that were handled successfully
- leave failed ones undeleted so they can return after visibility expires
That pattern works well with retry logic and dead-letter queues.
Common Pitfalls
A common mistake is assuming that receiving a message removes it. It does not. You must delete it after successful processing.
Another mistake is setting MaxNumberOfMessages=10 and expecting exactly 10 every time. SQS returns up to 10, not a guaranteed full batch.
A third issue is using short polling with high request frequency and then wondering why SQS cost is higher than expected.
Summary
- SQS can return up to 10 messages per
ReceiveMessagecall - Use long polling with
WaitTimeSecondsfor better efficiency - Messages are hidden after receipt, not deleted
- Delete processed messages explicitly, preferably with
DeleteMessageBatch - Match visibility timeout to processing time so messages do not reappear too early
Related reading
- Retrieve queue length with Celery (RabbitMQ, Django)
- Retrieve Timestamp based data from Kafka
- Retrieving number of unacknowledged messages in RabbitMQ queue from Java/ Spring
- Retry logic in kafka consumer
- Retrieve S3 file as Object instead of downloading to absolute system path
- Retrieve the full name of a service in Kubernetes
- Retry Lost or Failed Tasks (Celery, Django and RabbitMQ)
- Return from Kafka consumer when there is no message

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.