Service Broker

Applies to: SQL Server Azure SQL Managed Instance

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.

Overview

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.

Service broker

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.

Creating services

Database services are created by using the CREATE SERVICE Transact SQL statement. Service can be associated with the message queue create by using the CREATE QUEUE statement:

CREATE QUEUE dbo.ExpenseQueue;
GO
CREATE SERVICE ExpensesService
    ON QUEUE dbo.ExpenseQueue; 

Sending messages

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 ExpensesService 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.

Processing messages

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:

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 between instances of Azure SQL Managed Instance and message exchange between SQL Server and Azure SQL Manage Instance is currently in public preview:

  • CREATE ROUTE: Port specified must be 4022. See CREATE ROUTE.
  • ALTER ROUTE: Port specified must be 4022. See ALTER ROUTE.

Transport security is supported, dialog security is not:

  • CREATE REMOTE SERVICE BINDING is not supported.

Service broker is enabled by default and cannot be disabled. The following ALTER DATABASE options are not supported:

  • ENABLE_BROKER
  • DISABLE_BROKER

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).

Next steps

The most common use of Service Broker is for event notifications. Learn how to implement event notifications, configure dialog security, or get more information.