Lesson 3 of 3 Event-Driven Workflows with Azure Event Grid

Filtering, Retrying and Dead-Lettering Events

This lesson explains event type, subject and advanced filters, the retry schedule and policy, dead-letter destinations and batching.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how filters combine.
  • Explain the advanced filter limits and behaviour.
  • Describe the default retry policy and non-retried errors.
  • Explain what happens to undeliverable events.

Event type and subject filters

Without filters, a subscription receives every event from its topic. Three kinds of filter narrow that, and they combine: an event must pass all of them.

  • Event type filters list the types to include, such as Microsoft.Storage.BlobCreated but not BlobDeleted.
  • Subject filters match the start (subjectBeginsWith) or end (subjectEndsWith) of the subject, such as a container path prefix and a .pdf suffix.
  • Advanced filters compare fields in the event, including data fields, with operators.

Subject filters are simple and efficient, which is why publishers design subjects as hierarchical paths. For Blob Storage, a prefix of /blobServices/default/containers/uploads/blobs/invoices/ selects one virtual folder of one container.

Spot the bug

A subscription that gets the wrong events

Exam skills 3.1.2

Find the lines that let unwanted events through this subscription.

Log in to try this activity Interactive activities are free with an account.

Advanced filters

An advanced filter has an operator type, a key and one or more values. Keys address event fields with dot notation, such as data.contentLength or data.tenant; for CloudEvents, attributes such as source and type and extension attributes can also be used. Operators cover numbers (NumberIn, NumberGreaterThan, NumberInRange and others), booleans (BoolEquals), strings (StringIn, StringContains, StringBeginsWith, StringEndsWith and their negations) and presence (IsNullOrUndefined, IsNotNull).

Within one filter, multiple values are combined with OR; separate filters are combined with AND. String comparisons are not case-sensitive. A subscription can have up to 25 advanced filters with 25 filter values in total. Filtering on arrays requires enableAdvancedFilteringOnArrays, and arrays of objects cannot be filtered.

az eventgrid event-subscription create --name large-pdfs --source-resource-id STORAGE_ID \
  --endpoint-type azurefunction --endpoint FUNCTION_ID \
  --included-event-types Microsoft.Storage.BlobCreated \
  --subject-begins-with /blobServices/default/containers/uploads/ --subject-ends-with .pdf \
  --advanced-filter data.contentLength NumberGreaterThan 1048576
True or false

Advanced filters

Exam skills 3.1.2

Decide whether each statement about Event Grid advanced filters is true.

Log in to try this activity Interactive activities are free with an account.

Retry schedule and retry policy

When a delivery fails, Event Grid decides whether to retry based on the error. It waits 30 seconds for a response; a timeout or a retryable status code schedules a retry. Responses that retrying cannot fix are not retried: 400 (bad request), 413 (request entity too large) and 403 (forbidden) for any endpoint, and also 401 for webhooks; such events are dead-lettered immediately if a dead-letter destination exists, or dropped.

Other failures are retried with exponential backoff on a best-effort schedule: after 10 seconds, 30 seconds, 1 minute, 5 minutes, 10 minutes, 30 minutes, 1 hour, 3 hours, 6 hours, and then every 12 hours, with some randomization. Endpoints that keep failing are also put on probation and receive deliveries more slowly. The retry policy on each subscription limits retries with two settings, and whichever limit is reached first ends delivery.

SettingRangeDefault
Maximum number of attempts1 to 3030
Event time to live1 to 1,440 minutes1,440 minutes (24 hours)
Fill in the value

Retry arithmetic

Exam skills 3.1.2

Recall Event Grid's retry defaults and work out when delivery stops.

Log in to try this activity Interactive activities are free with an account.

Dead-letter destinations

When an event exhausts its retry policy, or meets a non-retryable error, Event Grid drops it unless the subscription has a dead-letter destination. Dead-lettering is off by default. The destination is a Blob Storage container, and Event Grid writes the undelivered event there with extra properties such as deadLetterReason, deliveryAttempts and lastDeliveryOutcome. There is a delay of about five minutes between the last attempt and the write, and if the storage container is unavailable for four hours, the event is dropped.

Dead-lettered events can be inspected and replayed by a repair process; a second Event Grid subscription on the dead-letter storage account can even notify operators each time an event lands there. When Event Grid uses a managed identity to write dead letters (or to deliver to Service Bus or Event Hubs), that identity needs a data role on the destination.

az eventgrid event-subscription create --name invoices-to-function --source-resource-id STORAGE_ID \
  --endpoint-type azurefunction --endpoint FUNCTION_ID \
  --deadletter-endpoint "$STORAGE_ID/blobServices/default/containers/deadletters" \
  --max-delivery-attempts 10 --event-ttl 120

This subscription dead-letters events after 10 attempts or 120 minutes, whichever comes first. With the default schedule, 120 minutes allows about seven attempts, so in practice the TTL ends delivery before the attempt limit.

The following diagram compares the fate of one Event Grid event during a long outage under the default retry policy and a fixed one.

What happens to an event published as the 30-hour outage began. With the default policy, Event Grid retried for 24 hours and then dropped the event, because no dead-letter destination was configured. With an event time to live of 240 minutes and a Blob Storage dead-letter destination, retries stop after four hours and the event waits in storage until a replay process resends it.
Times are measured from when the event was published.
True or false

Event Grid dead-lettering

Exam skills 3.1.2

Decide whether each statement about Event Grid dead-letter destinations is true.

Log in to try this activity Interactive activities are free with an account.

Batching and delivery properties

By default, Event Grid delivers one event per request (as an array with one element). Output batching, off by default, delivers up to a configured maximum number of events per request (1 to 5,000) and a preferred batch size in kilobytes, which improves throughput for high-volume handlers. Batches are all-or-nothing: if the handler fails, the whole batch is retried, so a handler should accept only as many events as it can process within 30 seconds. Subscriptions can also add up to 10 custom delivery properties, HTTP headers such as an authorization header for a webhook or properties for Service Bus messages, and an expiration time after which the subscription is deleted.

Scenario questions

Batching and delivery properties

Exam skills 3.1.2

Choose the subscription setting that fits each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

Lost events during an outage

A webhook handler in Container Apps was down for 30 hours during a failed deployment. When it recovered, events from the first six hours of the outage had never arrived, and nothing recorded them. The team needs undelivered events kept for replay, and wants delivery attempts to stop after four hours so that stale events are not processed late.

The subscription used the default retry policy, so Event Grid retried for 24 hours and then dropped the events because no dead-letter destination was configured. The fix is to configure a dead-letter destination in a Blob Storage container and set the event time to live to 240 minutes. Undelivered events then land in storage after four hours of retries, where a replay process can resend them once the handler is healthy.

Raising the maximum attempts to 30 is already the default and would not keep events. Output batching changes throughput, not retention. Adding an advanced filter would only reduce which events are sent.

Check yourself

A handler returns HTTP 400 for an event. Does Event Grid retry it?

Show answer

No. 400, 403 and 413 responses (and 401 for webhooks) are not retried. The event is dead-lettered at once if a dead-letter destination is configured, and dropped otherwise.

Exam Tips

Remember for the exam

  • Filters combine with AND: event types, subject begins/ends with, and advanced filters (values within one filter are OR).
  • Up to 25 advanced filter values per subscription; string comparisons are case-insensitive.
  • Default retry policy: 30 attempts or 24 hours; 400, 403 and 413 (and 401 for webhooks) are not retried.
  • Dead-lettering to a Blob Storage container is off by default; without it, expired events are dropped.