Nov 20, 20264 min read

Backpressure in Message Queues: Keeping Consumers From Falling Over

Backpressure is how a system slows producers or consumers when work arrives faster than it can be done. Bounded concurrency, flow control, queue-depth alerts, load shedding and rate limits for SQS and Pub/Sub consumers, with a TypeScript example on Encore.

Illustration for Backpressure in Message Queues: Keeping Consumers From Falling Over

A queue is a buffer, and a buffer only helps if the work drains at a rate the rest of the system can survive. When consumers pull messages as fast as they arrive, a burst becomes a flood of database connections or API calls.

Backpressure is the set of limits that prevents this, and this guide covers where to put those limits and how to spot a growing backlog, and shows a TypeScript consumer with bounded concurrency.

What happens without backpressure?

Take a consumer that calls a payment API allowing 20 requests a second. A marketing job publishes 100,000 messages, autoscaling adds consumers because the queue is deep, and the API starts returning 429 responses. Those messages fail, retry, and add load, while the extra consumers hold database connections doing nothing useful.

The queue did its job by absorbing the burst, and the consumers failed theirs by not limiting how much of it they passed on.

How do you bound consumer concurrency?

Concurrency is instances multiplied by concurrent messages per instance, so there are two knobs. Set the per-instance limit from the dependency that is slowest or most limited, since twenty consumers each holding a database connection means twenty connections. For SQS with Lambda, the event source mapping has a maximum concurrency setting for the same reason. For Pub/Sub pull clients, flow control limits outstanding messages and bytes per client.

Add rate limiting inside the consumer when the limit is requests per second instead of concurrent requests, for example a token bucket in front of the payment API call.

Should you shed load instead?

Yes, when a message is only useful for the next few seconds, such as a live price update, because processing it late is worse than dropping it. Give such messages a timestamp and let the consumer discard stale ones, or use a short retention period. Reserve load shedding for data that is cheap to lose, and keep durable queues for everything where each message matters.

How do you slow producers down?

A queue protects consumers, but it does not stop producers from filling it forever. Put limits at the edge: rate limiting per client on the API that publishes, and a maximum queue age alarm that tells you to act before users notice. For internal producers, such as batch jobs, publish in paced chunks instead of all at once.

What should you monitor?

Alert on the age of the oldest message, since that is what users feel as delay, and on dead-letter depth. Queue depth is a useful secondary signal. Combine them with consumer error rate and downstream latency, so a backlog caused by a slow dependency is distinguishable from one caused by a surge of publishes. For background on why bounded queues matter, see the Reactive Manifesto glossary entry on back-pressure.

Bounded consumers with Encore

Encore

Encore is a backend framework with built-in infrastructure, so a Pub/Sub subscription carries its concurrency and retry limits in the same file as its handler. On AWS the topic and subscription become SNS and SQS, and on GCP they become Pub/Sub, in your own account.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

// charges/charges.ts import { Topic, Subscription } from "encore.dev/pubsub"; import log from "encore.dev/log"; export interface ChargeRequested { chargeId: string; amountCents: number; requestedAt: string; // ISO timestamp } export const chargeRequested = new Topic<ChargeRequested>("charge-requested", { deliveryGuarantee: "at-least-once", }); // A small token bucket: about 15 calls a second for this instance. let tokens = 15; setInterval(() => { tokens = 15; }, 1000); async function takeToken(): Promise<void> { while (tokens <= 0) await new Promise((r) => setTimeout(r, 25)); tokens--; } export const _charge = new Subscription(chargeRequested, "charge-card", { handler: async (req) => { await takeToken(); log.info("charging", { chargeId: req.chargeId }); // Call the payment API here, using chargeId as the idempotency key. }, // Per instance. Three instances process up to 12 messages at once. maxConcurrency: 4, retryPolicy: { minBackoff: "5s", maxBackoff: "2m", maxRetries: 10 }, });

Concurrency is limited per instance and the token bucket limits calls per second per instance, so keep both below the payment provider's limit divided by the instance count. Producers publish through the exported topic or an API endpoint that applies rate limits at one place. Failed messages back off and end up in the dead-letter queue after ten tries, and each delivery appears in a trace so a slow dependency shows up as long spans.

A coding agent can lower a concurrency cap in the same change that adds a new downstream call, run the consumer locally with a burst of published messages, and ship it with the queue configuration unchanged.

Best fit: TypeScript teams whose queue consumers call rate-limited APIs or shared databases and who want the limits written next to the handler.

Frequently asked questions

What is backpressure in a message queue?

Backpressure is any mechanism that limits how fast work enters or is processed so a slow component is not overwhelmed. With a queue, the queue itself absorbs bursts, and bounded consumer concurrency keeps downstream systems such as databases and external APIs from receiving more work than they can handle.

How do you apply backpressure to queue consumers?

Cap how many messages each consumer processes at once and how many consumers run in total, based on what the slowest dependency can absorb. Pub/Sub clients offer flow control settings, SQS event sources offer a maximum concurrency, and a service framework can expose the same limit per subscription.

What should you monitor to detect a backlog?

Watch the age of the oldest message and the queue depth, and alert when age exceeds what your product tolerates. Depth alone misleads, because a deep queue that drains quickly is healthy while a shallow one with a stuck message is not.

How do I limit consumer concurrency with Encore?

Set maxConcurrency on the Pub/Sub subscription, which limits how many messages each instance of the service processes at once. Total concurrency is that value multiplied by the number of running instances, so size it against what your database or downstream API can handle.

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.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders