Message queues decouple producers from consumers by buffering messages until consumers are ready. They absorb spikes, isolate failures, and keep systems working when downstream services slow. Queues suit webhook ingestion and background work.

Core Concepts

  • Queue vs Topic
  • Queue (point-to-point): one message is consumed by one worker in a competing-consumer group.
  • Topic (pub/sub): one event is consumed by multiple independent subscriber groups.
  • Terminology varies: RabbitMQ uses exchanges, Kafka uses topics/partitions, and Service Bus uses subscriptions.
flowchart LR
    P[Producer or Webhook Receiver] --> E[Exchange or Broker Router]
    E --> Q1[Queue Orders]
    E --> Q2[Queue Billing]
    Q1 --> C1[Consumer Worker A]
    Q2 --> C2[Consumer Worker B]
  • Delivery guarantees

  • At-most-once: possible loss, no redelivery.

  • At-least-once: an unacknowledged delivery is retried, so duplicates are expected.

  • A bounded retry policy may terminate in an owned DLQ. Retention or TTL expiry is a separate loss boundary, not part of the at-least-once guarantee.

  • Effectively-once for one side effect is usually at-least-once + idempotency + transactional boundary.

  • End-to-end exactly-once across external systems is generally not realistic.

  • Ordering and partitioning

  • Ordering is usually per partition/queue shard, not global.

  • More partitions improve throughput but weaken global order guarantees.

  • If per-entity ordering matters (for example OrderId), a stable key routes all of that entity’s events to one partition.

  • Retries/redelivery and competing consumers can reorder events.

  • Kafka rebalances can cause duplicate processing when offsets were not committed. Out-of-order effects usually come from multi-partition reads or concurrent handlers.

Reliability Patterns

  • DLQ for poison messages

  • A DLQ fits when messages repeatedly fail and block healthy traffic.

  • Broker specifics: Service Bus uses MaxDeliveryCount. RabbitMQ uses DLX + TTL/retry queues. Kafka has no broker DLQ and uses an app dead-letter topic.

  • A DLQ is operated as a first-class system: alerts, replay tooling, retention ownership.

  • Retry with backoff

  • Transient failures are retried with exponential backoff + jitter.

  • If the broker supports delayed delivery, broker-managed delay is preferable. Otherwise retry queues/topics provide the delay.

  • Idempotency keys

  • A durable idempotency key (MessageId or business key) is persisted.

  • Check-then-act races, so the key is reserved or upserted atomically (unique index or transactional insert) before side effects are applied.

  • The business write and idempotency completion commit in one database transaction. Broker ack/offset commit follows after success.

  • Ack modes and offset commits

  • Auto-ack favors throughput but risks loss on mid-processing crashes.

  • Manual ack after successful side effects favors correctness.

  • RabbitMQ nack/requeue and Service Bus Abandon cause retry/redelivery. Dead-lettering is separate.

  • Kafka uses offset commits instead of ack/nack: the offset is committed after processing, and idempotency provides duplicate safety.

  • Lock or visibility expiration can also trigger redelivery, so long handlers need lock renewal/extension.

  • Backpressure

  • Prefetch/QoS limits in-flight work.

  • Queue depth, lag, and oldest-message age are tracked to avoid memory and latency collapse.

.NET Worker Implementation

A worker acknowledges only after the business effect or an owned quarantine record is durable:

public sealed class InvoiceWorker(
    IQueueConsumer consumer,
    IInvoiceHandler handler,
    IDeadLetterPublisher deadLetters) : BackgroundService
{
    protected override async Task ExecuteAsync(
        CancellationToken stoppingToken)
    {
        await foreach (var delivery in consumer.ReadAllAsync(stoppingToken))
        {
            try
            {
                await handler.HandleAsync(
                    delivery.Message,
                    stoppingToken);
                await delivery.AckAsync(stoppingToken);
            }
            catch (InvalidMessageException error)
            {
                await deadLetters.PublishAsync(
                    delivery,
                    error.Code,
                    stoppingToken);
                await delivery.AckAsync(stoppingToken);
            }
            catch (TransientDependencyException)
            {
                await delivery.RetryAsync(stoppingToken);
            }
        }
    }
}

HandleAsync should reserve a unique message or business-operation key in the same transaction as its state change. A crash after that commit but before AckAsync then produces a harmless redelivery. Dead-letter or quarantine publication must succeed before the original delivery is acknowledged.

Concurrency is bounded by downstream capacity, intake stops during shutdown, and monitoring tracks oldest-message age, in-flight count, handler latency, retries, dead-letter rate, and idempotency conflicts. A short queue can still be unhealthy when one old message never completes.

.NET Platform Choices

Software Architecture/Distributed Systems/Message Queues/RabbitMQ fits routing-heavy queues and latency-sensitive tasks. Software Architecture/Distributed Systems/Message Queues/Kafka fits replayable event streams. Azure Service Bus fits managed messaging with queues/topics and dead-lettering.

OptionStrengthsTradeoffsTypical .NET fit
Software Architecture/Distributed Systems/Message Queues/RabbitMQRich routing, easy work queues, low latencyCluster operations remain the team’s responsibility unless managedBackground jobs, webhook pipelines, command dispatch
Software Architecture/Distributed Systems/Message Queues/KafkaHigh throughput, durable log, strong replayPartition model and ops complexityEvent streaming, analytics, event sourcing feeds
Azure Service BusFully managed with enterprise messaging featuresCost and platform couplingAzure-native workflows and integration
  • IDistributedCache is not a queue.
  • Cache stores key-value state. Queues store ordered work items/events with ack/retry semantics.

Delivery Attempts, Processing Effects, and Idempotency

Broker guarantees describe delivery attempts at a boundary. They do not automatically guarantee business effects. An at-least-once broker may redeliver after a consumer commits ChargeCustomer but crashes before acknowledgement. The second attempt is correct broker behavior and a dangerous duplicate unless the charge operation uses a stable idempotency key.

Broker behaviorConsumer sequenceResult
At-most-onceAcknowledge, then processA crash can lose work
At-least-onceProcess, then acknowledgeA crash can repeat work
Transactional broker scopeAtomically consume and publish inside one brokerExternal database or HTTP effects remain outside that transaction

For InvoicePaid { EventId = 91, InvoiceId = 42 }, the consumer reserves EventId=91 with a unique constraint in the same database transaction that marks invoice 42 paid. A redelivery then observes the completed reservation and acknowledges without applying the transition twice. This produces one durable effect even though delivery was attempted more than once.

Messaging Patterns

Ownership and fan-out determine the pattern, not broker terminology:

  • Competing consumers: several workers share one logical subscription. Each message is handled by one worker. It suits scaling work such as image processing.
  • Publish/subscribe: each subscription receives the event independently. It fits when OrderPlaced drives billing, email, and analytics.
  • Request/reply: a request carries a correlation ID and a reply address. It fits only when asynchronous transport is required but the caller still needs a response. It preserves temporal coupling.
  • Priority queue: urgent work is selected first. Lower-priority work needs protection from starvation, and not every broker offers strict priority.
  • Dead-letter channel: terminally failed messages leave the hot path with failure metadata and an owned replay process.
  • Claim check: a large payload is stored in object storage, and its identifier, checksum, and authorization context travel through the broker.

Patterns combine. A video upload can publish a claim-check message to a competing-consumer queue, then emit VideoProcessed to multiple subscribers.

Choosing a Broker

software architecture message queues

The choice follows replay, routing, ordering, delivery, retention, managed-service, and operating requirements rather than popularity.

NeedSoftware Architecture/Distributed Systems/Message Queues/RabbitMQ classic/quorum queuesSoftware Architecture/Distributed Systems/Message Queues/Kafka
Work distributionStrong fit with acknowledgements and exchangesPossible through consumer groups, but retained-log semantics dominate
ReplayConsumed messages normally leave the queue. Replay needs republishing or a retained designNative offset replay within retention
RoutingExchanges, bindings, topics, and headersTopic and partition selection
OrderingPer queue, affected by redelivery and competing consumersPer partition
OperationsQueue depth, unacked messages, redelivery, node healthPartitions, replication, rebalances, lag, retention

RabbitMQ Streams

RabbitMQ Streams adds a replicated append-only log with non-destructive consumers and offset/timestamp replay. A super stream partitions traffic and preserves order only within each partition. Streams fit when RabbitMQ is already the operational center and the workload needs large fan-out, replay, or large backlogs. Kafka fits when retained logs, partitioned consumer groups, and its ecosystem are the primary model.

RabbitMQ classic or quorum queues fit GenerateInvoice jobs that need acknowledgements and flexible routing. Kafka fits OrderPlaced events consumed by billing, fraud, analytics, and replayable projections.

Managed services such as Azure Service Bus, Amazon SQS/SNS, and Google Pub/Sub are often better when the team does not want to operate brokers. Their exact ordering, deduplication, dead-letter, size, retention, and throughput contracts need direct comparison rather than an assumption of open-source semantics.

Pitfalls

Assuming Global Order

Most brokers guarantee order only within a partition, and retries, prefetch, or competing consumers can still reorder effects. When an entity’s transitions must stay ordered, its events are partitioned by the entity key, concurrency for that key is limited, and the consumer discards already-applied duplicates and, for full-state events, superseded versions, but does not skip a missing earlier change.

Retrying a Poison Message Forever

A message that always fails can consume worker capacity and starve healthy traffic. Retries are bounded, the failure moves to a dead-letter path, its owner is alerted, and enough metadata is kept to replay it after repair.

Applying an At-least-once Effect Twice

Redelivery is expected after an acknowledgement is lost. A charge, email, or external call therefore needs a stable idempotency key reserved atomically with the local state change.

Letting Backlog Age Go Unseen

Queue depth alone hides whether one old message is stuck or fresh traffic is arriving faster than consumers can drain it. Oldest-message age, in-flight work, redelivery, and consumer throughput show when backpressure or added capacity is needed before memory or latency limits fail.

Questions

References

2 items under this folder.