Event-driven architecture on Azure: the patterns and when to use each
Service Bus, Event Grid, Event Hubs or Durable Functions: sort by whether the message is a command, a fact or a stream, and by what should happen when nobody is listening. Why the Functions Event Hubs trigger skips a failing batch instead of stalling.
ON THIS PAGE
On Azure, choose between Service Bus, Event Grid, Event Hubs and Durable Functions by asking what the message is. A command, something one service must do, goes on a Service Bus queue. A fact, something that happened which anyone may react to and nobody is obliged to, goes through Event Grid. A stream, a series of readings to be read in order and perhaps again, goes into Event Hubs. And when the order of the steps is itself the business rule, you want a Durable Functions orchestration, not more messages.
Ask what should happen when nobody is listening#
Imagine the consumer switched off for an afternoon. A command should wait for it. A fact may go. A stream should be kept for a set time, read or not. The three services promise exactly that:
- Service Bus holds a message until a consumer settles it, with no practical default expiry on Standard and Premium. A receiver takes a lock, not the message, so a crash hands it to someone else. After ten deliveries by default it moves to the queue's dead-letter subqueue.
- Event Grid, in the push model Azure's own events use, sends each event to every matching subscription and keeps nothing. With no subscription, the event is gone. With a failing endpoint, it retries for 24 hours or 30 attempts (the defaults), then drops the event unless you gave the subscription a Blob Storage container for dead letters.
- Event Hubs appends to a partitioned log, and each consumer group keeps its own position. Retention is a day on Basic, up to seven on Standard and up to ninety on Premium and Dedicated. Reading removes nothing; nothing is dead-lettered.
A fact that someone must act on is a command#
The common mistake is to subscribe a must-not-miss consumer straight to Event Grid. An invoice landing in a storage container is a fact; to billing, it is a command. If billing is down for a day and a half, a direct subscription has dropped that invoice; at best it sits in a dead-letter blob you redrive with your own code.
Let Event Grid deliver to a Service Bus queue instead. Everyone else still sees a fact; billing gets a backlog, locks and a dead-letter subqueue. The message ID Event Grid puts on the Service Bus message stays the same across its retries, so duplicate detection with a window as long as the 24-hour retry period discards Event Grid's own repeats at the door.
az servicebus queue create -g billing -n invoices --namespace-name billing-bus \
--enable-duplicate-detection true --duplicate-detection-history-time-window P1D
az eventgrid event-subscription create --name invoices-to-billing \
--source-resource-id "$STORAGE_ACCOUNT_ID" \
--included-event-types Microsoft.Storage.BlobCreated \
--endpoint-type servicebusqueue --endpoint "$INVOICES_QUEUE_ID"Duplicate detection needs Standard or Premium, and a longer window costs throughput, so match it to the retry period. It stops duplicates arriving, not redelivery after a lost lock, so the consumer still needs an idempotency key.
On Azure Functions, a stream skips rather than stalls#
Event Hubs has no locks: a reader keeps a checkpoint per partition and must move it forward. The Event Hubs trigger in Azure Functions moves it after every batch, whether your function succeeded or threw. Lambda on a Kinesis shard does the opposite, retrying a failing batch until its records expire unless you set a limit. On AWS a poison record stalls the shard; on Azure the batch that threw is left behind and the partition carries on. Only a crash or a timeout leaves the checkpoint in place, which is where duplicates come from.
So catch everything at the top of the function, and when an event keeps failing, park it on a Service Bus queue. A retry policy (here in function.json) holds the checkpoint until its retries finish, pausing that partition. Its count lives in one instance's memory and resets to zero if that instance fails, so treat the maximum as approximate:
"retry": {
"strategy": "exponentialBackoff",
"maxRetryCount": 4,
"minimumInterval": "00:00:05",
"maximumInterval": "00:02:00"
}When the sequence is the rule, orchestrate#
Messages are the wrong tool when a step depends on how the others went. If the card is charged only after stock is reserved, and a failed shipment must refund it, a chain of queues scatters one order's state and nobody owns the outcome. A Durable Functions orchestrator does: the sequence is ordinary code, its progress an append-only history (Microsoft now recommends the managed Durable Task Scheduler to keep it). It is unloaded while it waits, then runs again from the top, reusing finished results, so it must be deterministic: no system clock, no random numbers, no I/O of its own. Start it with the order number as its instance ID, not the random default, and only if no instance with that ID exists, running or finished: a redelivery then finds the first run rather than starting a second.
On AWS and Google Cloud#
On AWS, commands go on SQS, facts through EventBridge, streams into Kinesis Data Streams, and sequences into Step Functions. On Google Cloud, Cloud Tasks carries commands to one named endpoint, Eventarc routes facts, Pub/Sub gives each subscription its own copy, Managed Service for Apache Kafka holds streams and Workflows runs sequences. Event-driven architecture on AWS sorts the AWS pieces the same way; messaging on all three clouds sets their ordering, replay and dead letters side by side.
The diagram#
The four services by the kind of message each carries: commands queued for an order worker, an orchestration calling its activities in turn, a new invoice pushed into billing's own queue, and device readings each consumer group reads from its own offset. Open it in archhy and make a copy to adapt it.
