Consumer concurrency and backpressure
Handler concurrency controls how many callbacks a consumer runs at once. Fetch batch size controls how many deliveries it claims in a database fetch. They affect different resources and should be configured separately.
Start from the handler consumer example. The following variants cap active callbacks at four and fetch up to four deliveries at a time. The demonstration handler prints the message; replace it with bounded, cancellation-aware application work.
Bound handler work
The events topic and jobs queue must already exist. These excerpts use an established connection and remain active until the application follows Graceful shutdown.
- Go
- Rust
- TypeScript
consumer, err := conn.ConsumeHandler("jobs",
func(ctx context.Context, message *postgremq.Message) {
if ctx.Err() != nil { return }
fmt.Println(message.ID)
},
postgremq.WithTopic("events"), postgremq.WithBatchSize(4),
postgremq.WithMaxInFlight(4), postgremq.WithVT(30))
if err != nil { return err }
return <-consumer.NotifyClose(make(chan error, 1))
let consumer = conn.consume_with_handler(
"jobs",
postgremq::ConsumeOptions::default()
.batch_size(std::num::NonZeroU32::new(4).unwrap()),
std::num::NonZeroUsize::new(4),
|message: postgremq::Delivery| async move {
if message.stopped().is_cancelled() { return Ok(()); }
println!("{}", message.message_id());
Ok(())
},
).await?;
const consumer = client.consumeHandler('jobs', async (message) => {
if (message.signal.aborted) return;
console.log(message.id);
}, {
topic: 'events', batchSize: 4, maxInFlight: 4,
visibilityTimeoutSec: 30,
});
The Go excerpt waits for consumer termination and returns its close error; application lifecycle code must close the connection from outside that wait. Keep the returned consumer handle in Rust and TypeScript so termination can be observed. These limits apply to this consumer, not the entire queue or database. Three processes each configured for four handlers can run up to twelve callbacks, subject to available work and ordering.
Batch size is not a global in-flight limit
A fetched delivery is already claimed even if it is buffered before application processing. Its visibility timeout and attempt state therefore matter before a handler begins. SDKs manage buffered claims and normally release unattempted work on orderly shutdown, but a crash can leave those claims to expire.
Increasing batch size can reduce fetch round trips while increasing reserved work, memory use, and the amount left behind by a stopped process. It does not guarantee an even distribution among workers or make the whole batch one application transaction.
A handler limit does not cap every outstanding delivery owned by the SDK. Choose a batch consistent with processing capacity and measure actual backlog and recovery. Go and TypeScript default to unlimited handler concurrency; Rust can also request no handler limit. Set a finite value deliberately for constrained downstream work.
Stream consumers own dispatch
A sequential stream loop processes one application item at a time, but the SDK can still have prefetched deliveries. To process concurrently, bound application dispatch before accepting more work. Creating a goroutine, task, or promise for every item without a limit transfers an unbounded backlog into application memory.
Both stream and handler consumers use automatic extension unless configured otherwise. A queue with many long-lived deliveries therefore also creates renewal work. Do not size concurrency using only callback CPU; include database connections and downstream requests.
Apply backpressure where capacity is limited
Choose the handler limit from downstream concurrency and pool capacity. Reserve connections for settlement and renewal rather than assigning every pool slot to business transactions. Increase concurrency gradually while observing queue age, operation duration, renewal loss, and downstream errors.
FIFO groups impose another limit: only one head in each group and queue can proceed. Adding workers cannot parallelize one group. Use keys that match the required ordering boundary, and monitor group skew rather than treating all ready rows as immediately parallel work.
During overload, bounded consumers keep the backlog in PostgreSQL where it can be measured and recovered. They do not create unlimited database capacity. Plan storage, retry policy, and acceptable queue age using Performance and capacity.