Abstract
The new version of Microsoft SQL Server "Yukon" comes together with a set of technologies capable of enlarging the horizon for database applications. One of the most remarkable technologies is the Service Broker that makes it possible to build database-intensive distributed applications. The Service Broker actually implements a set of distributed communication patterns to add messaging capabilities to SQL Server applications. In this article we explore the core concepts of the Service Broker and show how to use its potentialities to build applications with message interchanging.
The Broker Pattern
Currently, distributed applications are one of the most exciting areas in software development. The first kind of distributed applications was based on the client-server model. In most of these applications, clients have to be concerned with server communication details. For instance, the client should know the IP Address of the machine hosting the server as well as other details. The solution to these problems comes with the Broker pattern when it is used in order to hide the implementation details of remote service invocation by encapsulating them into a layer different from the business component itself. This layer enables clients to invoke operations on the server side just as they would on a local interface. The implementation of this pattern can hide the server communication details inside the broker that has traditionally added other functionalities such as locating clients and so on. One of the most famous brokers in the history of distributed programming has been the Object Request Broker in the CORBA standard that has added some novel concepts to the distributed application development.
The YUKON Service Broker
The YUKON Service Broker is an implementation of the Broker pattern that comes as part of the current version of YUKON and that makes it possible to build reliable, asynchronous message-based database applications. The use of the Service Broker allows the developers to build distributed applications delegating all the system level messaging details to the Service broker and concentrating their efforts in the problem domain. The Service Broker lets SQL Server applications send and receive messages. This technology also adds advantages to the messaging capabilities like reliability or asynchronous messaging.
The Service Broker exposes the distributed application components as services holding conversations among them. It also manages these services in sending, receiving and processing messages. Within a typical Service Broker scenario a source service sends a message to a target service. On its arrival, the Service Broker inserts it into a queue associated with a target service and then it can be processed by a service program. Once the message is processed, the target service responds. Due to the messaging potentialities of the Service Broker, services can communicate for days using an asynchronous communications approach.
The Service Broker Architecture
Technically, the Service Broker can be viewed as a group of T-SQL extensions used to create components to develop message-based applications. The service functionality is provided for by a set of SQL Server objects:
- Service Program: A service program may be a stored procedure written in a SQL/CLR manner used to process messages. It can send messages to be processed by other service programs.
- Queue: A Queue is the component that stores the messages before they are processed by a service program associated with them. This queue provides asynchronous messaging between services and offers other potentialities like lock-related messages.
- Message Type: A Message Type represents a message format used to communicate with services. Specifically, they can communicate with each other by interchanging message type instances.
- Contract: A Contract is a set of names of message types that specifies the direction of a message in a conversation.
- Service: A service is a logical endpoint that represents a collection of the above mentioned objects performing some specific task. Services are stored in SQL Server and have a name.
- Dialogs: Dialogs represent a bidirectional conversation between two Service Broker endpoints.
- Service Instances: The service instances identify conversations working together to achieve some tasks.
- Routes: A route is used to direct messages to the correct services even between different instances of SQL Server.
- Remote Service Bindings: Remote Service Bindings associate a remote service with a user in a local database.

Message Types
Inter-service communication is carried out by interchanging messages. Just as sentences follow a structure in real life conversations, messages sent to services must also follow some format dictated by the Message Type used to describe the message contents (binary or XML data). In the case of XML messages, the data Service Broker always checks whether the content is valid or not, that is, it checks if messages are appropriately structured or if they follow some XML Schema. Message Types always remain in the database.
Contracts
The fact that a service exposes a contract to describe the operations it supports constitutes a pillar of the Service-Oriented Architecture. A Service Broker contract describes which messages may be used to perform a desired task and also which endpoints may use specific message types. For instance, a contract may specify that some messages may only be sent by the target service.
Queues
The role of a queue is to store messages. It is directly associated with a service. When a message arrives, the Service Broker inserts it into the queue. Likewise, when the application receives a message, the Service Broker deletes this message. A queue is also associated with the service program representing a stored procedure or program activated when the message is received. The maximum number of service programs to be activated when a message arrives is specified in the definition of the queue. This makes it possible to increase the message processing performance. The queue stores the message content and the information related to it, for example the contract, the relationship with other messages, etc.
Service Programs
The Service Program is the entity that processes the messages. Normally, it is either a stored procedure or a managed external program. As stated above, the Service Program is activated when a message arrives to a queue extracting the message and processing it. When it does this, it can send messages to other services.
Services
The service identifies a set of tasks performed by various objects specifying the contracts and the queues that store the messages. A Service represents a logical endpoint grouping other objects. To create a service it is necessary to define the message types to be sent or received as well as the contracts, the service program to process the messages and the queue that is supposed to hold the messages.
Dialogs
Dialogs represent conversations that involve two endpoints: the initiator and the target. As part of the contract and based on its role, it is necessary to define which messages can be sent or received by a service. A key aspect in any conversation is the message reliability ensured by the receipt acknowledgments of the dialog messages. The Service Broker, in turn, saves all the messages in a conversation as soon as they are acknowledged by the other endpoint in the conversation.
Another important aspect in a conversation is its lifetime. When the initiating application begins the dialog, it may specify the lifetime of this dialog. The service programs at both endpoints must terminate the dialog when they finish their work.

Routes
A route identifies an address used by the broker to send messages to a service. It does so in a unique manner. It is used whenever the Service Broker needs to send a message during a conversation. More than one route can be defined for a service while the broker may choose one at random.
Remote Service Bindings
To communicate with remote services the Service Broker should use certificate-based security. A remote service binding objective is to associate a local database user with a remote service name. The messages sent to a remote service are encrypted using the public key of the local user certificate associating this public key to a user in the remote service with permission to store messages in the remote service queue.
Service Instances
The Service Broker uses Service Instances to identify a group of related dialogs. Technically, the Service Instance is represented by a unique identifier data type. When the broker receives a message, it adds the service instance identifier to the message before inserting it into the queue.
Another effect caused by the use of the Service Instance identifier is to provide exclusive access to the messages. An isolated Service Program can at a given time read messages with the same Service Instance Identifier. The Service Instance identifier remains valid until all conversations related to this identifier end. The Service Instance identifier is the primary key in all tables that maintain the state of the applications. This feature simplifies the retrieval of data associated with applications.
Further Remarks
The Service Broker represents a significant beginning to provide a queue-based messaging technology fully integrated to database applications. Its architecture is very interesting in regards to the way it merges concepts such as Service, Contract, etc from the "Service-Oriented Architecture" with others that make up the queue-based messaging technologies. Consequently, the resulting architecture is very powerful and simple. I believe that in the future, the Service Broker might upgrade itself with some other features from the messaging technologies like message-filtering, message header processing, maybe SOAP messaging, message sequencing, among others.
Using the Service Broker
Up to now we have been exploring the Service Broker concepts and architecture. Now we will implement a brief example to show the use of some of the main components of the Service Broker. Suppose there is a system that manages customer information provided by an external entity (another system perhaps). We have designed a very simple Service to receive an XML message with data identifying a customer, to insert that message in a database and to log a message indicating that the action was performed.
The first step can be to define the message structure. An XML Schema can define this. The following code adds the XML Schema to the database.
Join the conversation! Your thoughts help the community grow.