Skip to content

Visibility and flow control#

SQS hides a received message from other consumers for its visibility timeout. A message that is not deleted in that time returns to the queue. The subscriber keeps the messages in work hidden, and FlowControl bounds how many it holds.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker, FlowControl, SqsQueue

broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)


@broker.subscriber(
    SqsQueue("orders", visibility_timeout=60),
    flow_control=FlowControl(
        polls=2,
        max_in_flight=50,
        settle_wait=0.1,
        max_hold=7200.0,
    ),
    max_workers=10,
)
async def bill(order: dict[str, int]) -> None: ...

FlowControl#

Field Meaning
polls Concurrent ReceiveMessage loops. The default is 1.
max_in_flight Messages received and not yet settled, at most. max_workers + polls * max_messages when omitted.
settle_wait Seconds that deletes and visibility changes wait to go out together. The default is 0.05.
max_hold Seconds a message is held at most before its visibility is no longer extended. The default is 3600.

The in-flight cap#

A poll takes the slots it needs before it calls ReceiveMessage, and gives back the slots a short receive did not fill. The broker never holds more than max_in_flight unsettled messages. When every slot is taken, the poll waits.

Visibility#

When visibility_timeout is not set on the SqsQueue, the subscriber reads the queue's own value once at start. When it is set, every receive passes it. A subscriber refuses a visibility below 5 seconds with SetupError: the extension cannot keep a shorter timeout alive.

Extension#

While a handler works, the broker extends the visibility of the message. With a visibility of V seconds, a task wakes every V / 3 seconds and extends each held message whose deadline falls before the next wake, to V seconds from now. The calls go out in batches of 10. A message that was just received is not extended on the next tick.

An extension that fails is logged, and the message may be redelivered while its handler still runs. Settling a message removes it from the extension table at once, so a deleted message is never extended.

max_hold#

A message held longer than max_hold is dropped from the extension table. SQS makes it visible again when its visibility runs out, and the broker logs a warning. The handler running on it is not cancelled. max_hold is capped at 43140 seconds, because SQS refuses to extend a receive beyond 12 hours.

Set max_hold above the longest time a handler needs.

Failed batch entries#

A delete or a visibility change that fails inside a batch is put back into the next flush once. A second failure is logged with its code. The message then follows the rule of SQS: a delete that did not happen is redelivered after the visibility timeout.