Path: redb-route/redb.Route.Sqs/README.md Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sqs/README.md ---- # redb.Route.Sqs Amazon **SQS** + **SNS** transport for [redb.Route](https://github.com/redbase-app/redb), via the native **AWS SDK for .NET v4** (`AWSSDK.SQS`, `AWSSDK.SimpleNotificationService`). One package, two schemes — `sqs://` (queue: consume + produce) and `sns://` (topic: publish + SNS→SQS fan-out). LocalStack / ElasticMQ compatible. ## Installation ```bash dotnet add package redb.Route.Sqs ``` ```csharp services.AddRedbRoute(route => { route.Services.AddRedbRouteSqs(); // registers sqs:// and sns:// route.AddRouteBuilder(); }); ``` ## Usage ```csharp using redb.Route.Sqs.Fluent; // Consume a queue — long-poll, 4 competing consumers, delete after processing From(Sqs.Queue("orders").WaitTimeSeconds(20).ConcurrentConsumers(4)) .Process(HandleOrder); // Produce to a queue .To(Sqs.Queue("orders").Region("eu-west-1")); // FIFO queue (name ends in .fifo) — group id required .To(Sqs.Queue("orders.fifo").MessageGroupId("${header.customerId}")); // Publish to an SNS topic .To(Sns.Topic("events").Region("us-east-1")); // SNS → SQS fan-out: subscribe a queue, then consume it with sqs:// .To(Sns.Topic("events").SubscribeSnsToSqs("arn:aws:sqs:us-east-1:000000000000:events-q")); // Same, but raw delivery — the queue gets the bare payload (no SNS JSON envelope), // and SNS attributes arrive as SQS attributes (so trace context survives the hop): .To(Sns.Topic("events") .SubscribeSnsToSqs("arn:aws:sqs:us-east-1:000000000000:events-q") .RawMessageDelivery()); ``` > **Envelope vs raw.** By default SNS wraps the payload in a JSON notification envelope > (`{"Type":"Notification","Message":"",...}`), so the subscribing queue receives the > *envelope*, not your payload — and SNS message attributes are buried inside it. Add > `.RawMessageDelivery()` (sets `RawMessageDelivery=true` on the auto-created subscription) to get the > **bare payload** and have SNS attributes map to SQS message attributes. It applies only to the > `subscribeSnsToSqs` auto-subscription; the default stays `false` (AWS-compatible envelope). Raw URIs work too: `sqs://orders?waitTimeSeconds=20&concurrentConsumers=4`, `sns://events?region=us-east-1`. ## Concurrency & delivery - **`concurrentConsumers=N`** runs N competing receive loops — up to N messages processed in parallel (the SQS-native model). See the framework-wide **Concurrency & Parallelism** guide (`CONCURRENCY.md`) for how this compares to the `.Threads(N)` processing EIP. - **At-least-once**: a message is deleted only after it processes successfully. On failure it is left for redelivery after the **visibility timeout** (or reset to 0 immediately with `resetVisibilityOnFailure=true`). - **`extendMessageVisibility=true`** keeps a message hidden while a long handler runs (heartbeat). - **Inside `.Transacted()`** the delete comes last, after the database and the deferred sends have committed. - **Producers join the transaction.** Inside `.Transacted()` an SQS send or an SNS publish leaves once the database has committed and is dropped if the block rolls back. `transacted=false` sends at once, outside the transaction; `transacted=true` requires an enclosing block and fails outside one. See the **Transactions** guide (`TRANSACTIONS.md` in the [redb.Route repository](https://github.com/redbase-app/redb)). - **Ordering** is preserved only per FIFO message group and only with `concurrentConsumers=1`. ## Batch mode `batching=true` hands the route one InOnly exchange per batch, its body a `List` with one message per SQS message (its body, its `redbSqs.*` headers and attributes, as a single message gets them), on the batch contract every broker of redb.Route shares, with Apache Camel's option names: - `maxBatchMessages` (default 10): the largest batch, collected over receives of up to 10 (the SQS limit per receive); - `pollTimeoutMs` (default 1000): the batch goes on once no message came for that long; every receive that brings one restarts it. SQS long-polls in whole seconds, so one receive waits at least a second; - `batchingIntervalMs`: the batch goes on at the latest this long after its first message. `maxNumberOfMessages`, `waitTimeSeconds` and `delay` are not used in batch mode. The batch message carries `redbSqs.queue`. Settlement, once after the route, by the rule of a single message applied to every message of the batch: - the route succeeded: the batch is deleted (`DeleteMessageBatch`, 10 per call); an entry SQS refuses is logged and comes back after its visibility timeout; - the route failed: the messages stay invisible until the visibility timeout and come back (with `resetVisibilityOnFailure=true` at once); the queue's redrive policy moves a message to its dead letter queue after `maxReceiveCount` receives; - the consumer stops while a batch is collected or routed: its messages are made visible again at once. A receive in flight is not abandoned (an abandoned long poll stays open at the queue and would take those messages again): the stop waits for it, at most `pollTimeoutMs` rounded up to whole seconds and never over 20 s, and gives its messages back with the rest; - `ackMode=auto` deletes the batch on receipt, before the route. A batch's messages stay invisible while it is collected and routed: give the queue (or `visibilityTimeout`) a timeout longer than both. `extendMessageVisibility` is refused with `batching=true`. Each of `concurrentConsumers` workers collects and routes its own batches. Verified against LocalStack 3.8.1 (community, the emulator of the test stand), not against AWS. What an emulator does not show: duplicate deliveries on a standard queue (at-least-once: a batch may hold a message delivered before), throttling of the batch calls, IAM permissions (`sqs:DeleteMessageBatch`, `sqs:ChangeMessageVisibilityBatch` besides the single-message ones), and the real timing of long polls. ## Sending a list A body that is a list of `IMessage` goes out one SQS message per element, in `SendMessageBatch` calls of up to 10: each element with its own body and attributes (the parent's headers are not copied), and on a FIFO queue its own `redbSqs.messageGroupId` and `redbSqs.messageDeduplicationId` headers, else the endpoint's options. As in Apache Camel, entries SQS refuses do not fail the send: `redbSqs.batchSuccessful` and `redbSqs.batchFailed` count them, `redbSqs.batchFailedEntries` lists the refused ones, and the endpoint records an error. Any other list goes out as one message, its JSON. ## Key options | Option | Applies to | Notes | |---|---|---| | `region`, `serviceUrl`, `accessKey`/`secretKey`, `sessionToken`, `profileName`, `useDefaultCredentialsProvider` | both | `serviceUrl` targets LocalStack/ElasticMQ | | `waitTimeSeconds` (0–20), `maxNumberOfMessages` (1–10), `visibilityTimeout` | SQS consumer | long-poll + messages per receive + hide time | | `concurrentConsumers`, `extendMessageVisibility`, `ackMode`, `resetVisibilityOnFailure` | SQS consumer | concurrency + ack | | `batching`, `maxBatchMessages`, `pollTimeoutMs`, `batchingIntervalMs` | SQS consumer | batch mode | | `delaySeconds`, `messageGroupId`, `messageDeduplicationId` | SQS producer | FIFO; a list of `IMessage` goes out by `SendMessageBatch` | | `transacted` | SQS and SNS producer | unset follows an enclosing `.Transacted()` block, `false` sends at once, `true` requires a block | | `autoCreateQueue` / `autoCreateTopic`, `topicArn`, `subject`, `messageStructure` | both | topology + SNS payload | | `subscribeSnsToSqs` + `subscribeQueueArn` | SNS | subscribe a queue to the topic on start | | `rawMessageDelivery` | SNS | on that auto-subscription, deliver the bare payload + map SNS attrs → SQS attrs (default `false` = JSON envelope) | ## Headers A producer sends exchange headers as string message attributes of the same name; a `redbSqs.attr.X` header goes out as attribute `X`, and the other `redbSqs.*` / `redbSns.*` headers never go out. A consumer puts each attribute under `redbSqs.attr.X`, so a received attribute cannot pose as a control header of the route. `headerFilterStrategy=#name` (SQS and SNS) names an `IHeaderFilterStrategy` in the registry that decides which plain headers go out and which attributes come in, as in Apache Camel. It replaces the default, `SqsHeaderFilterStrategy` or `SnsHeaderFilterStrategy`, entirely; derive from that class to add to it. On the way in the strategy is asked with the attribute's own name (`X`, not `redbSqs.attr.X`). A `redbSqs.attr.X` header is an explicit request and goes out whatever the strategy says. ```csharp ctx.AddToRegistry("correlationOnly", new DefaultHeaderFilterStrategy { OutFilter = ["Correlation-ID"], FilterOnMatch = false, // a whitelist: only the listed headers go out }); // sqs://orders?region=eu-west-1&headerFilterStrategy=#correlationOnly ``` ## Part of Part of the [redb.Route](https://github.com/redbase-app/redb) enterprise integration framework. ## Named connection factory Keep credentials out of the route URI: register a factory in the context registry and reference it by name. A set-but-unknown name fails loud at startup — a typo can never silently fall back to inline URI parameters. ```csharp context.AddToRegistry("prod", new AwsConnectionFactory { Region = "eu-west-1", AccessKey = "svc", SecretKey = secrets.AwsSecret, }); // sqs://orders?connectionFactory=prod ``` ## Concurrency The default is **1** concurrent consumer — the industry norm (Camel, Spring, the Azure SDK all ship 1): a single consumer preserves ordering and your handlers need no thread safety. Parallelism is an explicit opt-in: ``` concurrentConsumers=4 # a fixed worker count concurrentConsumers=auto # max(CPU count, 2) — the NServiceBus formula ``` Anything else — `0`, a negative, a typo — fails at endpoint creation naming the option (the old int-typed option silently fell back to 1). Raising the value trades ordering for throughput: messages from the same queue are processed out of order, and your processors must be safe to run in parallel. ## Tracing On the `redb.Route` activity source (`AddSource("redb.Route")`). The W3C context travels in the message attributes `traceparent`, `tracestate` and `baggage`; SNS attributes reach SQS with `rawMessageDelivery`. - **SQS consumer.** One `Consumer` span per message, `{queue} receive`, over the whole unit of work. Its parent is the sender's `traceparent`; without one it is a root, never a child of the activity the receive loop inherited from whoever started the routes. The sender's baggage is back on it, the route's spans are its children. A failed route, a timeout inside it included, marks it an error; the consumer's own stop does not. In batch mode one span per batch, a root linked to the `traceparent` of every message that has one, with `messaging.batch.message_count`. - **SQS and SNS producers.** One `Producer` span per send or publish; an error when it fails, unless our own token cancelled it. The message carries the context of this span: a `traceparent` copied from a received message is replaced. A message may carry at most 10 attributes: when the headers and the trace context together exceed that, the send fails naming both counts, and nothing is dropped to make it fit. A list of `IMessage` is one span with `messaging.batch.message_count`; every element carries its context. - `RouteEngineOptions.EnableTelemetry = false` opens none of these spans. A context that came in still goes out.