Sending and receiving messages in AWS - Time & Space Complexity
When sending and receiving messages in AWS, it is important to understand how the time taken grows as the number of messages increases.
We want to know how the number of API calls changes when we handle more messages.
Analyze the time complexity of the following operation sequence.
// Send multiple messages to an SQS queue
for (let i = 0; i < messages.length; i++) {
sqs.sendMessage({ QueueUrl: queueUrl, MessageBody: messages[i] });
}
// Receive messages from the queue
const received = sqs.receiveMessage({ QueueUrl: queueUrl, MaxNumberOfMessages: 10 });
This sequence sends each message one by one to the queue, then receives up to 10 messages at once.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Sending messages with
sendMessageAPI call. - How many times: Once per message sent (equal to the number of messages).
- Receiving operation: One
receiveMessagecall that fetches up to 10 messages at a time.
As the number of messages grows, the number of send calls grows the same way, one per message.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 sendMessage calls + 1 receiveMessage call |
| 100 | 100 sendMessage calls + 1 receiveMessage call |
| 1000 | 1000 sendMessage calls + 1 receiveMessage call |
Pattern observation: The sending calls increase directly with the number of messages, while receiving calls stay constant if we only receive once.
Time Complexity: O(n)
This means the time to send messages grows directly with how many messages you send.
[X] Wrong: "Sending multiple messages is always one API call regardless of count."
[OK] Correct: Each message requires its own send call unless you use batch APIs, so calls grow with message count.
Understanding how message sending scales helps you design systems that handle load smoothly and predict costs.
"What if we used batch sending to send 10 messages at once? How would the time complexity change?"