Search Sreeni's Insights

Filter by content type
technical November 9, 2017 6 min read

Event-driven microservices with Event Grid and Service Bus

Event-driven microservices combine Event Grid notifications and Service Bus commands, with guidance on delivery, ordering, idempotency, correlation and durable workflow design.

Share:

Event-driven microservices with Event Grid and Service Bus

Microservices are becoming difficult to operate when every service is calling every other service synchronously. A fund subscription can involve order validation, unit reservation, payment and notification, and one slow dependency is holding the complete request open. Events and commands can reduce direct coupling, but only when the team understands delivery, ordering and duplicate processing.

In the 2016 event-driven architecture article, we discussed Service Bus topics and subscriptions. Refer to this article for the messaging foundation. In the previous How-to article, we also looked at Event Grid as a preview event-routing service. In this article, we are going to see how Event Grid and Service Bus can work together in an event-driven microservice architecture.

TL;DR Summary

  • An event says something happened; a command asks a service to do something.
  • Event Grid is useful for reactive push notifications and discrete events.
  • Service Bus is useful for durable queues, topics, sessions, transactions, ordering and dead-letter handling.
  • A common flow is Event Grid notification → handler → Service Bus command.
  • Consumers must be idempotent because delivery can be at least once.
  • An outbox helps publish an event reliably with the local transaction.

Challenges

Synchronous coupling

If the subscription service calls payment, allocation and notification services synchronously, one unavailable dependency is failing the user operation. The retry behaviour is also becoming different in every caller.

Events are not commands

An event such as FundSubscriptionAccepted is a fact. A command such as ReserveUnits is asking one owner to act. Treating both as the same message is creating unclear ownership and accidental side effects.

Delivery is not exactly once

Event Grid and Service Bus can deliver a message again after a timeout or consumer failure. If a consumer creates a document or reserves units twice, the architecture is not safe even though the message was valid.

What is event-driven microservices?

Event-driven microservices communicate important state changes as events and direct work requests as commands, instead of requiring every operation to call another service synchronously.

In simple terms, a service publishes what happened, and another service decides whether it needs to react. A command is sent to one owner when a specific action is required.

The following diagram depicts Event Grid notifying a handler which sends commands through Service Bus.

Event Grid and Service Bus microservices
Event Grid and Service Bus microservices

The purple path is the notification-to-command route. Event Grid is not replacing Service Bus; they are serving different delivery and ownership needs.

Why event-driven architecture is important?

Event-driven architecture is important because it is reducing synchronous coupling and allowing services to scale or recover independently. The subscription service can record an accepted order and continue while allocation consumes a durable command.

In our case, an accepted fund subscription can publish an event, an allocation handler can issue a ReserveUnits command, and the document service can react to the final allocation event. The customer-facing response must still define whether acceptance means “received” or “fully completed”.

Event Grid versus Service Bus

ConcernEvent Grid previewService Bus
Main roleReactive event routingDurable messaging and commands
DeliveryPush with retries and at-least-once behaviourQueues, topics, subscriptions and dead-letter
Event sourceResource groups, subscriptions, Event Hubs and custom topicsApplication producers and consumers
OrderingNot the main guaranteeSessions and queue design can support ordering
TransactionNotification serviceTransactions and reliable receive/send patterns
CostPer-event pricingMessaging operations and capacity

Use Event Grid for a discrete notification which many handlers may observe. Use Service Bus when a command must wait in a durable queue, be retried, dead-lettered or processed with a session.

Use both services

The flow can be structured as follows,

  1. The subscription service commits FundSubscriptionAccepted.
  2. An outbox record is written in the same local transaction.
  3. An outbox publisher sends the event to an Event Grid custom topic.
  4. An Event Grid handler validates the event and sends ReserveUnits to Service Bus.
  5. The allocation service consumes the command and publishes UnitsReserved or UnitsRejected.
  6. Other services react to the final event.

The outbox is protecting the gap between a database commit and event publishing. If the process fails after the commit, the outbox record is retried. If publishing succeeds but the publisher does not record it, the consumer must still handle duplicates.

Idempotent consumers

Store an event or command id before applying a non-repeatable action, or design the action to be safely repeated. A ReserveUnits handler can use an idempotency key composed of the subscription id and command id, and it can check whether units were already reserved.

Use correlation id across the event, command and logs. Add timeout and retry rules around downstream calls, and send poison messages to a dead-letter path after a bounded number of attempts.

Selection criteria

Choose Event Grid when the publisher is an Azure resource, subscription, Event Hubs source or custom topic and the handler is reacting to a fact. Choose Service Bus when the receiver owns a command, needs durable backlog, ordering, transactions, sessions or dead-letter operations.

Use both when a broad event should trigger one or more reliable commands. Do not send every internal method call through a topic; synchronous calls can be clearer when the caller needs an immediate result.

Keep event contracts versioned and small. A consumer should be able to ignore fields it does not need, while a breaking meaning change should use a new event type or a migration path. Record the publisher, owner, retention expectation and known consumers so an event does not become an anonymous integration point.

The operational dashboard should show event publication failures, delivery retries, queue age, dead-letter count and consumer completion. A green publisher metric is not enough when the allocation queue is growing and investors are waiting for a subscription result.

Define retention and replay carefully. A retained event can help rebuild a projection, but replaying a financial command is not automatically safe. Keep facts separate from commands, and make a replay tool select the event types and consumers deliberately.

For the fund subscription example, the event schema should not include a payment credential or an entire customer profile. Publish the identifiers and business facts needed by consumers, then let an authorised service retrieve protected details through its own boundary. This is reducing accidental data spread across queues and event stores.

Must Do / Should / Don’t

Must Do

  • Must distinguish events from commands.
  • Must design consumers for duplicate delivery.
  • Must use correlation and idempotency identifiers.
  • Must define retries, timeout, dead-letter and eventual-consistency behaviour.

Should

  • Should use an outbox for reliable publishing.
  • Should use Event Grid for reactive notification and Service Bus for durable commands.
  • Should monitor queue depth, delivery failure and consumer latency.

Don’t

  • Don’t assume an event means every downstream action completed.
  • Don’t depend on exactly-once delivery.
  • Don’t use messaging to hide a synchronous operation which needs an immediate answer.

Wrap up

In this article, we have looked at events, commands, Event Grid, Service Bus, outbox publishing, idempotent consumers, correlation and eventual consistency.

I really appreciate that you took the time to read this article. In the next article, I will walk you through the Saga pattern for managing transactions across microservices.

Related Posts