An Introduction To The Service Broker
An Introduction To The Service Broker
As shown in the figure, a client application submits a request to a web server. The web server notes the
request into a queue and control is then returned to the client. The queue is read at a later point in time by a
queue processor and as each message is read off a queue, the queue processor processes the message by
executing whatever is the associated business logic and then persists the result of the processing in a data
store. Information is then sent back to the client in some form (for example email) informing about the status of
the request.
This sort of asynchronous processing is quite common in many enterprise applications and there are standard
technologies that can provide this functionality. However, when building message based applications, there are
a number of challenges that we need to be aware of. The following are some notable issues to be handled:
1. Message Ordering When a queue handles multiple messages, there needs to be some sort of
order among the messages so that related messages are processed in order. For example, if order
header and order line items are all posted into a queue, care must be taken that the order line
items are processed after the order header and all related order items for a particular order header
are processed in sequence.
2. Transactions This is an important requirement. When processing a message off a queue, it is
quite possible that an error can be encountered. In this case, the message must not be lost; rather
it must be posted back into the queue for subsequent processing.
3. Recoverability During the processing of messages from a queue, it is quite possible that the
system can undergo an outage and in this case, the messages in the queue need to be persisted
in some form to ensure that processing resumes when the system is restored.
4. Scalability As messages are posted into the queue, the application must be able to process them
at a rate that does not introduce a performance problem for the application. For example, if
messages in a queue are posted faster than the queue processor can handle them, the system
can experience a performance issue.
Different message processing systems handle the above challenges in different forms. However, if you need all
the above features and not have to code for them, it is better to build the messaging system on an
infrastructure that is designed for recoverability, maintainability, durability, performance and scalability and that
can only be a database system. And what is a database system that ensures all of the above features? SQL
Server, of course :)
Ok, so here is the formal definition of Service Broker. Service Broker is a new feature in SQL Server 2005 that
allows you to build reliable, asynchronous, message-based applications using extensions to the T-SQL
language to process and manage queues. Since queues are managed in the database, you enjoy all the
capabilities of a typical database system, which is transactions, reliability, performance, scalability etc.
Service Broker can easily scale to many thousands of messages per second and it can guarantee message
ordering among the various messages and even across different reading threads! All message processing is
transacted and thus recoverable. Finally, message processing and posting can be distributed across many
participating SQL Servers, thus ensuring true scalability and performance. In the subsequent sections of this
article, we will see the building blocks of Service Broker and write our first, simple message based application.
At the very top of the stack, the application layer, we have two applications that need to exchange messages.
This could be our online book store and order processing application. To enable the two applications to talk to
each other, we need to define a set of meta-data that describes how the applications will talk. The meta-data is
broken into the following concepts:
1. Messages Types Messages are the atom of service broker applications and define the content of
each message. Message types can be either binary, well formed XML or valid XML types.
2. Contracts A contract is a collection of messages that applications will ever exchange for their
lifetime.
3. Services Services are the endpoints into which messages are posted and a service is associated
with a contract so that the messages can be validated.
4. Queue A queue is the primitive that holds messages and is modeled as a table in SQL Server.
Messages are read from a queue by a service program which then proceeds to process the
messages.
5. Transport The transport is the underlying protocol over which messages flow between the service
points and this is a proprietary binary protocol that is native to SQL Server 2005.
6. Dialog Two services communicate by means of a dialog. The dialog is the primitive that ensures
in-order processing across transactions and sending / receiving threads and is an important aspect
of service broker.
All the above meta-data are expressed using extensions to the T-SQL language. For example, there is a
CREATE MESSAGE TYPE for creating message types and a BEGIN DIALOG CONVERSATION for starting a
dialog between two services. We will see more of these new T-SQL commands later on, when we build our first
application.
At each of the above steps, we will use various T-SQL extensions and our example will illustrate some aspects
of each. For more information on the syntax and other options available, refer to the SQL Server 2005 Books
Online.
The following is the T-SQL script batch that we will use. You can execute the script in portions (until each GO
statement) to see the various artifacts of a service broker program being created.
-- We will use adventure works as the sample database
USE AdventureWorks
GO
-- First, we need to create a message type. Note that our message type is
-- very simple and allowed any type of content
CREATE MESSAGE TYPE HelloMessage
VALIDATION = NONE
GO
-- Once the message type has been created, we need to create a contract
-- that specifies who can send what types of messages
CREATE CONTRACT HelloContract
(HelloMessage SENT BY INITIATOR)
GO
-- The communication is between two endpoints. Thus, we need two queues to
-- hold messages
CREATE QUEUE SenderQueue
BEGIN
BEGIN TRANSACTION;
BEGIN DIALOG @conversationHandle
FROM SERVICE Sender
TO SERVICE 'Receiver'
ON CONTRACT HelloContract
-- Send a message on the conversation
SET @message = N'Hello, World';
SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE HelloMessage (@message)
COMMIT TRANSACTION
END
GO
-- Receive a message from the queue
RECEIVE CONVERT(NVARCHAR(max), message_body) AS message
FROM ReceiverQueue
-- Cleanup
DROP SERVICE Sender
DROP SERVICE Receiver
DROP QUEUE SenderQueue
DROP QUEUE ReceiverQueue
DROP CONTRACT HelloContract
DROP MESSAGE TYPE HelloMessage
GO
Note the syntax for sending and receiving messages. The syntax for receiving a message is similar to a
SELECT statement. The RECEIVE statement reads a message off the queue and deletes. This is termed as a
destructive read. Note that you can issue a SELECT statement against a queue to see its contents. SELECT
is non-destructive and is akin to peeking into a queue.
Ok, that brings us to the end of this introductory article and I hope you had a sense of the possibilities with this
new programming model. Before I close the article, there is one question that constantly pops into people mind:
Why do messaging in the database? The following are some valid reasons that I can think of:
1. By representing messages in databases, you can use the familiar syntax of regular database
programming to queue based application also. There is no separate API to know and understand.
2. Database based messaging applications enjoy all the capabilities of a database system. For
example, Service Broker applications are scalable, recoverable and maintainable.
3. Transaction semantics for messaging are built-in in the database. No special programming
semantics are needed.
4. As an aside: The multiple queue reader models can help write multi-threaded applications in SQL
Server. Something that was never possible in earlier versions!
Hope you use this as the starting point to explore this great new addition to SQL Server. In future articles, I
shall write more about some interesting programming use cases with Service Broker.