Edit

Share via


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.

Diagram of Service Broker process flow.

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 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;

Send 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 AS 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's no activation procedure associated to this queue, the message will remain in the queue until someone reads it.

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

Service Broker documentation

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 isn't 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:

Transport security is supported, dialog security isn't:

  • CREATE REMOTE SERVICE BINDING isn't supported.

Service Broker is enabled by default and can't be disabled. The following ALTER DATABASE options aren't 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 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 and ALTER QUEUE 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.

Availability group support in Service Broker

For more information, see Service Broker with Always On Availability Groups (SQL Server).