Skip to content

FIFO queues#

A queue whose name ends in .fifo keeps the order of messages inside a message group and deduplicates messages.

service.py
import logging

from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker, SnsTopic, SqsMessage, SqsQueue

broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)
logger = logging.getLogger(__name__)

orders = SqsQueue(
    "orders.fifo",
    declare=True,
    # without this attribute every publish names a deduplication id
    attributes={"ContentBasedDeduplication": "true"},
)
events = SnsTopic(
    "events.fifo",
    declare=True,
    attributes={"ContentBasedDeduplication": "true"},
)


@broker.subscriber(orders, topic=events, max_workers=4)
async def handle(event: dict[str, int], msg: SqsMessage) -> None:
    # the messages of one group are handled in order, one after another
    logger.info("%s %s", msg.message_group_id, event)


async def send() -> None:
    await broker.publish({"id": 1}, orders, message_group_id="customer-42")
    await broker.publish({"id": 2}, events, message_group_id="customer-43")

Groups#

Every message to a FIFO queue or topic needs a message_group_id. The service hands out the messages of one group in order, and it hands out no further message of a group while one of that group is in flight. So the messages of one group are handled one after another, and different groups run in parallel.

test_fifo.py
    async with TestSqsBroker(service.broker) as br:
        with pytest.raises(SqsServiceError) as error:
            await br.publish({"id": 1}, service.orders)
        assert error.value.code == "MissingParameter"

A receive can return several messages of one group, in order. Settle every message of a FIFO queue: an unsettled message locks its group until its visibility timeout runs out.

Concurrent subscribers#

A subscriber with max_workers > 1 on a FIFO queue dispatches by group. The messages of one group from one receive form a run that one worker handles in order. Different groups run in parallel.

When a message of a run is nacked, by the handler or by NACK_ON_ERROR, the rest of the run goes back to the queue with visibility 0 without being handled, so the order of the group holds on redelivery. A rejected message does not stop its run: it is dropped and the rest keep their order.

A batch subscriber gets the receive in the order of the service, and settles it as a whole.

Deduplication#

A message to a FIFO queue also needs a deduplication_id, unless the queue deduplicates by content. Set ContentBasedDeduplication in the attributes of the declaration, as the example above does, and SQS derives the id from the body. SQS drops a message whose id it has seen in the last five minutes.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker

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


@broker.subscriber("payments.fifo")
async def handle(payment: dict[str, int]) -> None: ...


async def send() -> None:
    # SQS drops a second message with this id within five minutes
    await broker.publish(
        {"id": 1},
        "payments.fifo",
        message_group_id="customer-42",
        deduplication_id="payment-1",
    )

Topics#

A FIFO topic is named with the .fifo suffix, takes a message_group_id and deduplicates the same way, with ContentBasedDeduplication on the topic. It delivers only to FIFO queues. A declaration that joins a FIFO side with a standard one raises SetupError.

delay is not available for FIFO queues and topics.