Turn on long polling for Amazon SQS at the queue or per request, see what it does to empty receives and your request bill, and size consumer timeouts so polls do not stall shutdowns.

Amazon SQS consumers pull messages with ReceiveMessage, and by default that call uses short polling. It samples a subset of the SQS servers for the queue and returns right away, often with nothing in it even when messages exist. A worker in a tight loop on an idle queue then spends its day making empty, billable requests.
With long polling, ReceiveMessage waits up to 20 seconds for a message, queries every server for the queue, and returns as soon as something arrives. This guide covers how to turn it on, what it does to cost and latency, and the timeout settings that trip people up.
The SQS developer guide describes both modes and recommends long polling for most consumers.
Setting ReceiveMessageWaitTimeSeconds on the queue applies long polling to every consumer that does not pass its own value, including consumers written by other teams, which makes it the safest place to set it.
In Terraform the same setting is receive_wait_time_seconds = 20 on the aws_sqs_queue resource. The SQS Terraform guide shows a full queue with a dead-letter queue.
A consumer can override the queue attribute on each call. Here is a Go worker with the AWS SDK v2 that long-polls, processes up to ten messages per call and deletes each one after it succeeds:
func Poll(ctx context.Context, client *sqs.Client, queueURL string, handle func(context.Context, types.Message) error) error {
for ctx.Err() == nil {
out, err := client.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueURL),
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20,
VisibilityTimeout: 1200,
})
if err != nil {
if ctx.Err() != nil {
return nil
}
time.Sleep(time.Second) // back off on throttling or network errors
continue
}
for _, m := range out.Messages {
if err := handle(ctx, m); err != nil {
continue // the message becomes visible again after VisibilityTimeout
}
_, _ = client.DeleteMessage(ctx, &sqs.DeleteMessageInput{
QueueUrl: aws.String(queueURL),
ReceiptHandle: m.ReceiptHandle,
})
}
}
return nil
}
Two settings around this loop matter more than the poll itself. The HTTP client timeout must be longer than WaitTimeSeconds, or the SDK cancels a healthy 20-second poll as a timeout; the long polling setup page calls this out for every SDK. The second is shutdown: a worker that receives SIGTERM may sit in a poll for up to 20 seconds, so cancel the context on SIGTERM and give the container a stop timeout longer than the wait. On ECS the default stopTimeout is 30 seconds, which fits.
VisibilityTimeout is separate from the wait time. It starts when a message is returned and controls how long other consumers cannot see it. Size it for the whole batch, not a single message. Ten messages at 90 seconds each need more than 900 seconds of hiding, so the sample uses 1200. A shorter timeout would make an earlier message visible again while this loop is still working. The visibility timeout guide covers sizing it from your processing time.
SQS charges per request, and a ReceiveMessage that returns nothing still counts. SQS pricing lists standard queue requests at $0.40 per million after the first million each month in us-east-1, with each 64 KB chunk of a payload billed as one request.
The short-polling figure is per worker thread, so ten idle workers cost ten times as much. Long polling also lowers latency for sparse traffic, since a message that lands during a wait is returned immediately instead of on the next loop iteration.
Best fit: any SQS consumer that runs continuously, which is most of them. Short polling only makes sense when one thread must poll several queues and cannot block on one.
Encore brings infrastructure from code: you declare a Pub/Sub topic and subscription in Go, and on AWS Encore provisions SNS and SQS for them in your own account. The receive loop is part of the runtime. Encore's Go runtime calls ReceiveMessage with WaitTimeSeconds set to 20, the maximum, and sets the visibility timeout from the subscription's AckDeadline, so the long-polling and visibility settings above are applied for you. Failed messages are retried and then moved to a dead-letter queue that Encore configures with the subscription.
package fulfillment
import (
"context"
"time"
"encore.dev/pubsub"
"encore.dev/storage/sqldb"
)
type OrderPaid struct {
OrderID string `json:"order_id"`
}
var OrdersPaid = pubsub.NewTopic[*OrderPaid]("orders-paid", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
var db = sqldb.NewDatabase("fulfillment", sqldb.DatabaseConfig{Migrations: "./migrations"})
var _ = pubsub.NewSubscription(OrdersPaid, "create-shipment", pubsub.SubscriptionConfig[*OrderPaid]{
Handler: CreateShipment,
AckDeadline: 60 * time.Second, // becomes the SQS visibility timeout
MaxConcurrency: 20, // messages processed at once per instance
RetryPolicy: &pubsub.RetryPolicy{MaxRetries: 5},
})
func CreateShipment(ctx context.Context, e *OrderPaid) error {
// Idempotent: a redelivered message finds the existing shipment row.
_, err := db.Exec(ctx, `
INSERT INTO shipments (order_id, status) VALUES ($1, 'pending')
ON CONFLICT (order_id) DO NOTHING`, e.OrderID)
return err
}
Locally encore run runs the same topic on an in-memory broker, so you can call the endpoint that publishes from the development dashboard and see the handler run in the trace. Encore provisions an IAM role for the service.
A coding agent adding a consumer writes the subscription and handler in the same change and checks it against the local broker, with no queue attributes or IAM statements to get right by hand.
Best fit: Go services where you want SQS consumers with sensible polling, visibility and dead-letter settings declared next to the handler.
If you write SQS consumers yourself, set ReceiveMessageWaitTimeSeconds=20 on every queue, pass WaitTimeSeconds=20 in your client, and make your HTTP timeout and container stop timeout longer than the wait. If your consumers run on Encore, the runtime already long-polls at 20 seconds, and the settings you tune are AckDeadline, MaxConcurrency and the retry policy on the subscription.
Long polling makes ReceiveMessage wait up to 20 seconds for a message to arrive instead of returning immediately. SQS queries all of its servers for the queue and returns as soon as at least one message is available, or an empty response when the wait time ends.
Set the queue attribute ReceiveMessageWaitTimeSeconds to a value from 1 to 20, or pass WaitTimeSeconds on each ReceiveMessage call. The per-request value overrides the queue attribute, and a value of 0 means short polling.
Usually, yes. Every ReceiveMessage call is a billable request, including empty ones. A short-polling consumer on an idle queue can make several requests per second, while a long-polling consumer makes at most about three requests per minute per poller when no messages arrive.
Yes. Encore's Go runtime receives from SQS with WaitTimeSeconds set to 20, the maximum, and sets the visibility timeout from the subscription's AckDeadline, so you get long polling without configuring the queue.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.