Routing Messages with Topics and Subscriptions
This lesson explains publish-subscribe in Service Bus, the three filter types, rule actions, auto-forwarding and receiving from subscriptions.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish queues from topics.
- Compare SQL, correlation and boolean filters.
- Explain why the default rule must be removed.
- Describe rule actions and auto-forwarding.
Publish and subscribe
A queue delivers each message to one consumer. When several independent systems need the same message, for example an order that must be embedded for search, recorded for billing and sent to a notification service, a topic is used instead. Senders send to the topic exactly as they would to a queue. Each subscription on the topic behaves like a virtual queue that receives its own copy of every message that matches its rules, and has its own lock duration, delivery count and dead-letter queue.
Subscriptions are independent: a slow or failing billing consumer does not delay the search consumer, and adding a new consumer means adding a subscription, not changing the sender. Within one subscription, several receivers compete for messages as they do on a queue. A topic can have up to 2,000 subscriptions.
Topics and subscriptions
Filter types
Each subscription has one or more rules, and each rule has a filter that decides whether a message is copied to the subscription. A new subscription gets a default rule named $Default with a filter that matches everything. Filters evaluate message properties, not the body, which is why routing information belongs in application or system properties.
priority > 3 AND region = 'EU'If a subscription has several rules, a message is copied when any rule matches, and the copy is made once even if several match. Removing the $Default rule is therefore necessary when adding a selective rule; otherwise the default rule still matches everything.
The following diagram shows how the subscriptions route each file.

Which filter type?
Rule actions and auto-forwarding
A rule can have an action, written in a SQL-like syntax, that modifies the copy's properties as it enters the subscription, such as SET priority = 'high' or REMOVE tenant. Actions apply only to SQL filter rules and change only that subscription's copy.
Auto-forwarding chains entities: a subscription or queue can forward every message automatically to another queue or topic in the same namespace. It is used to fan in messages from several subscriptions to one processing queue, or to route a subscription's messages to a queue owned by another team.
az servicebus topic subscription create --resource-group RG --namespace-name NAMESPACE \
--topic-name orders --name high-value
az servicebus topic subscription rule delete --resource-group RG --namespace-name NAMESPACE \
--topic-name orders --subscription-name high-value --name '$Default'
az servicebus topic subscription rule create --resource-group RG --namespace-name NAMESPACE \
--topic-name orders --subscription-name high-value --name total-over-1000 \
--filter-sql-expression "total > 1000 AND region = 'EU'"Rules can also be managed from code with ServiceBusAdministrationClient and its create_rule method, using SqlRuleFilter or CorrelationRuleFilter objects.
Actions and forwarding
Receiving from a subscription
Receiving from a subscription differs from receiving from a queue only in how the receiver is opened: client.get_subscription_receiver(topic_name="orders", subscription_name="high-value"). Settlement, locks, delivery counts and dead-lettering work the same way. The sender needs only Data Sender on the topic; each consumer needs Data Receiver on its own subscription, which keeps permissions narrow.
Routing AI processing jobs
An ingestion service sends a message for every uploaded file to one topic. Image files must go to an OCR worker, audio files to a transcription worker, and every file must also be recorded by an audit service. The file type is known to the sender. Workers must not receive files they cannot handle.
The sender sets an application property such as fileType on each message. The OCR subscription has a correlation filter matching fileType = image, and the transcription subscription one matching fileType = audio, each with the $Default rule removed. The audit subscription keeps the default true filter, so it receives every message. Correlation filters are the most efficient choice for exact-value routing.
Filtering on a value inside the JSON body would not work, because filters evaluate properties only. Separate queues would require the sender to know every consumer. A single queue with competing consumers would deliver each file to only one worker.
A subscription with a new SQL filter still receives every message. What was missed?
Show answer
The $Default rule, which matches everything, was not removed. A message is copied if any rule matches.
Exam Tips
Remember for the exam
- A topic copies each message to every subscription whose rules match.
- Filters evaluate properties, not the body: SQL filters, correlation filters (fastest) or boolean filters.
- Remove the $Default rule when adding a selective rule.
- Auto-forwarding chains subscriptions or queues to another entity in the namespace.