SQS’s "temporary queue" feature for request-response patterns is often misunderstood as a way to dynamically create and destroy queues, but its real power lies in its ability to abstract away queue management for a specific, short-lived communication flow.
Let’s see it in action. Imagine a microservice that needs to perform a long-running task and then report back to the caller. Instead of the caller polling a dedicated queue, the service can create a temporary queue, send the request to it, and then provide the temporary queue’s URL back to the caller. The caller then sends its actual request to this temporary queue. Once the task is done, the service places the response in the same temporary queue. The caller, now knowing the temporary queue’s URL, polls it for the response. When the caller is done, the temporary queue is automatically deleted.
Here’s a simplified conceptual flow using the AWS CLI:
-
Service creates a temporary queue:
aws sqs create-queue --queue-name temp-request-response-$(uuidgen) --attributes DelaySeconds=0,VisibilityTimeout=30,MessageRetentionPeriod=300The output will include a
QueueUrl. Let’s say it’shttps://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef. -
Service sends a task ID to the temporary queue:
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef --message-body '{"taskId": "task-abc-123"}' -
Service returns the temporary queue URL to the caller. The caller now knows where to send its request.
-
Caller sends its actual request to the temporary queue:
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef --message-body '{"inputData": "process this"}' -
The service processes the task, finds the request message, performs the work, and sends the response back to the same temporary queue:
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef --message-body '{"result": "processed successfully"}' -
Caller polls the temporary queue for a response:
aws sqs receive-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef --wait-time-seconds 5The caller receives the
{"result": "processed successfully"}message. -
After receiving the response, the caller signals completion (or the service detects no more messages for a period) and the temporary queue is deleted:
aws sqs delete-queue --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/temp-request-response-a1b2c3d4-e5f6-7890-1234-567890abcdef
The core problem this solves is managing ephemeral communication channels without cluttering your SQS namespace with hundreds or thousands of short-lived queues. It provides a clean, decoupled way for services to initiate tasks and receive results without tight coupling to specific, long-lived queues. The CreateQueue API call is idempotent if you use a consistent naming convention, but the real magic is that you don’t need to worry about naming collisions or cleanup if you generate unique names for each request-response pair. The MessageRetentionPeriod and VisibilityTimeout on these temporary queues are crucial. Setting VisibilityTimeout to a reasonable value (e.g., 30 seconds) ensures that if a worker crashes mid-processing, the message becomes visible again for another worker to pick up. The MessageRetentionPeriod (e.g., 5 minutes) ensures that stale messages eventually disappear, preventing infinite queue growth if something goes wrong with the deletion process.
The most surprising aspect is how SQS, a fundamentally asynchronous, fire-and-forget service, can be bent into a synchronous-like request-response pattern. This is achieved by leveraging the fact that a queue URL can be passed as data within a message itself. The sender doesn’t need to know the receiver’s permanent queue; it only needs to know the temporary one created specifically for this interaction. The CreateQueue API call returns a QueueUrl that can be any valid SQS queue URL, including those for FIFO queues, though the temporary queue pattern is more commonly associated with standard queues due to its transactional nature.
The mechanism by which SQS automatically deletes these queues isn’t a built-in feature of CreateQueue itself. You must explicitly call DeleteQueue when the interaction is complete. This is typically handled by the service that initiated the request-response flow, or by a dedicated cleanup process that monitors for queues that have been inactive for a certain duration, often by looking at queue metadata or the last message timestamp if SQS provided such metrics directly. The "temporary" aspect is a pattern implemented by the user, not an intrinsic SQS property.
The next hurdle is handling retries and idempotency when the temporary queue might be deleted before the response is fully processed by the caller.