SQL Server Service Broker provide native support for messaging and queuing in the SQL Server Database Engine and Azure SQL Managed Instance. Developers can easily create sophisticated applications that use the Database Engine components to communicate between disparate databases, and build distributed and reliable applications.
When to use Service Broker
Use Service Broker components to implement native in-database asynchronous message processing functionalities. Application developers who use Service Broker can distribute data workloads across several databases without programming complex communication and messaging internals. Service Broker reduces development and test work because Service Broker handles the communication paths in the context of a conversation. It also improves performance. For example, front-end databases supporting Web sites can record information and send process intensive tasks to queue in back-end databases. Service Broker ensures that all tasks are managed in the context of transactions to assure reliability and technical consistency.
Service Broker is a message delivery framework that enables you to create native in-database service-oriented applications. Unlike classic query processing functionalities that constantly read data from the tables and process them during the query lifecycle, in service-oriented application you have database services that are exchanging the messages. Every service has a queue where the messages are placed until they are processed.
The messages in the queues can be fetched using the Transact-SQL
RECEIVE command or by the activation procedure that will be called whenever the message arrives in the queue.
CREATE QUEUE dbo.ExpenseQueue; GO CREATE SERVICE ExpensesService ON QUEUE dbo.ExpenseQueue;
Messages are sent on the conversation between the services using the SEND Transact-SQL statement. A conversation is a communication channel that is established between the services using the
BEGIN DIALOG Transact-SQL statement.
DECLARE @dialog_handle UNIQUEIDENTIFIER; BEGIN DIALOG @dialog_handle FROM SERVICE ExpensesClient TO SERVICE 'ExpensesService'; SEND ON CONVERSATION @dialog_handle (@Message) ;
The message will be sent to the
ExpenssesService and placed in
dbo.ExpenseQueue. Because there is no activation procedure associated to this queue, the message will remain in the queue until someone reads it.
The messages that are placed in the queue can be selected by using a standard
SELECT query. The
SELECT statement will not modify the queue and remove the messages. To read and pull the messages from the queue, you can use the RECEIVE Transact-SQL statement.
RECEIVE conversation_handle, message_type_name, message_body FROM ExpenseQueue;
Once you process all messages from the queue, you should close the conversation using the END CONVERSATION Transact-SQL statement.
Where is the documentation for Service Broker?
The reference documentation for Service Broker is included in the SQL Server documentation. This reference documentation includes the following sections:
Data Definition Language (DDL) Statements (Transact-SQL) for CREATE, ALTER, and DROP statements
See the previously published documentation for Service Broker concepts and for development and management tasks. This documentation is not reproduced in the SQL Server documentation due to the small number of changes in Service Broker in recent versions of SQL Server.
What's new in Service Broker
Service broker and Azure SQL Managed Instance
Cross-instance service broker message exchange is supported only between Azure SQL Managed Instances:
CREATE ROUTE: You can't use CREATE ROUTE with ADDRESS other than LOCAL or DNS name of another SQL Managed Instance. Port specified must be 4022. See CREATE ROUTE.
ALTER ROUTE: You can't use ALTER ROUTE with ADDRESS other than LOCAL or DNS name of another SQL Managed Instance. Port specified must be 4022. See See ALTER ROUTE.
Transport security is supported, dialog security is not:
CREATE REMOTE SERVICE BINDINGis not supported.
Service broker is enabled by default and cannot be disabled. The following ALTER DATABASE options are not supported:
No significant changes were introduced in SQL Server 2019 (15.x). The following changes were introduced in SQL Server 2012 (11.x).
Messages can be sent to multiple target services (multicast)
The syntax of the SEND (Transact-SQL) statement has been extended to enable multicast by supporting multiple conversation handles.
Queues expose the message enqueued time
Queues have a new column, message_enqueue_time, that shows how long a message has been in the queue.
Poison message handling can be disabled
The CREATE QUEUE (Transact-SQL) and ALTER QUEUE (Transact-SQL) statements now have the ability to enable or disable poison message handling by adding the clause,
POISON_MESSAGE_HANDLING (STATUS = ON | OFF). The catalog view sys.service_queues now has the column is_poison_message_handling_enabled to indicate whether poison message is enabled or disabled.
Always On support in Service Broker
For more information, see Service Broker with Always On Availability Groups (SQL Server).