Service Broker Foundations Workbench

SQL Service Broker, an asynchronous queuing and messaging system for SQL Server 2005, is set to change the way we design and run distributed applications. Adam Machanic makes it all seem so easy...

Simple-Talk’s Workbench series are intended to be loaded into SQL Servers Management Studio, read and executed. You can download the script at the bottom of the article. To make the best use of them we’d encourage you to try things out, to experiment and generally use them more as a starting-off point with an aspect of SQL Server. As Service Broker is such an important introduction, we will be devoting a series of three workbenches to it.

Contents

Introduction to SQL Service Broker

SQL Service Broker (SSB) is a powerful asynchronous queuing and messaging infrastructure available in all editions of SQL Server 2005. It provides tools to handle a wide variety of tasks, from simple workload queuing all the way to advanced message routing between remote servers.

SSB in SQL Server is a paradigm shift; a change for the better and a new way to solve a whole host of difficult problems. Yet SSB has not been widely adopted yet. Whether this is due to time constraints placed on developers who have better things to do than learn Yet Another New Thing or the perceived difficulty of picking up the skills required, I am not certain. What I do know is that SSB is incredibly useful, and really not all that hard to pick up.

Although fairly easy to learn, SSB is still relatively big, and a complex topic – you really can do quite a few different things with it. So I’ve broken this workbench into a three-part series. In this, part one of the workbench, we’ll cover the basics: The Service Broker object types, how to create them, and how to set up the simplest of queues. At the end of this workbench you’ll have a solid foundation for designing basic asynchronous services ‐ not to mention, for continuing in this workbench series.

So without further ado, let’s get started. The Broker awaits!

To begin with, we’ll create a database to work in.

Enabling Service Broker

Depending on how your model database is set up, Service Broker may be disabled by default when you create a new database. Or, you may want to enable it in a database that already exists but in which it is not enabled.

Service Broker is turned on or off on a per-database basis, and you can find out its current state for your database by querying the sys.databases catalog view.

Turning on Service Broker is easy enough, but there is a slight twist: there must either be zero users connected to the database, or you must be the only user connected, when you actually flip the switch. ALTER DATABASE can help us in that regard, but it doesn’t change the fact that somehow, those users have to go.

Let’s find out if Service Broker is turned on.

If the preceding query returned a 0, run the following; note that the ROLLBACK IMMEDIATE option will disconnect all other users from the database – necessary in order to enable SSB. That’s OK in this test database, but be careful when you do this in a production environment.

Database Master Key

Next we will create a database master key. This is not necessarily required, but is recommended since SSB makes use of many of SQL Server 2005’s encryption features. The rest of this workbench will not require a master key, but I want to stress the importance of this step anyway, so I’m leaving it in.

Message Types

Service Broker at its core is a messaging infrastructure, and so the first object to be defined is the type of messages we’d like to send. Message types are the base construct which allow your services to enforce what data can and cannot be sent, and by whom – but we’ll get to that second part shortly.

A message type itself is nothing more than a named declaration of what type of data can be sent. The type of data is known as the data type’s “validation.” Validation can take any of the following forms:

EMPTY
The message sent will be nothing more than a message, with no data; this is useful for pingbacks, acknowledgments, and the like.
NONE
The message sent can be any binary LOB, up to 2 GB in size.
WELL_FORMED_XML
The message sent must be valid XML.
VALID_XML WITH SCHEMA COLLECTION
The message sent must be XML that can be validated against a specific schema – this is where the idea of validation really gets powerful.

For now we’ll use a message type with simple XML validation, to experiment with.

Contracts

Service Broker’s messaging capabilities are based on the idea that messages are sent between two parties, in a two-way exchange known as a “dialog.” The “initiator” is the partner that started the dialog by sending the first message, and the “target” is the partner to whom the first message was sent.

Contracts determine which party – the initiator or the target ‐ can send which message type(s). Note that aside from the first message, which must be sent from the initiator to the target, no ordering or workflow is enforced by SSB. Your contract can define as few or as many message types as you’d like, and you can specify that they can be sent by the INITIATOR, the TARGET, or by ANY (either of them).

Our first contract will be quite simple – either party can send the message.

Queues

Queues are the physical storage containers in which messages reside. In a later workbench we’ll cover some of the more advanced options that queues provide, but for now think of them as as merely special tables.

You can query queues just like any other table, using SELECT, but other standard DDL operations, such as INSERT, UPDATE, and DELETE, will not work – queued data must be manipulated using Service Broker DDL.

Generally speaking, you’ll want one queue on each end of a dialog. So we’ll create two queues: One for the initiator, and one for the target.

At this point you can use SELECT to take a look at the queues, but they are not too exciting. There are no rows returned, since we haven’t yet sent any messages.

Services

Service Broker is designed as a tool that can help you build loosely coupled, service-oriented applications, and its own design keeps these goals in mind. The physical manifestation of messages – the queues – are decoupled from the logical endpoints from which and to which messages are sent. These are known as “services.”

A service serves two purposes: First of all, as already mentioned, it is a logical endpoint that sits on top of a queue. Secondly, and equally important, a service’s definition includes which contract(s) it enforces. Later on, you’ll see that the parties involved in dialogs are services. Since services sit on top of the physical queues, and since they enforce a defined set of contracts, they are the mechanism by which you can restrict which message types can eventually show up in the queue.

Since the service and the queue are decoupled, a queue can have many services feeding it. But that’s a topic we’ll cover in detail in the third workbench!

We’ll create one service for each queue. The services will each enforce the Simple_Contract we defined earlier. A given service can enforce as many contracts as you’d like – if you want to specify more than one, you can comma-delimit their names.

Dialogs (Conversations)

At long last our infrastructure is in place and we are ready to begin actually sending some messages. Just to quickly recap: we’ve created an XML-validated message type, a contract that allows messages of the type to be sent by either the initiator or the target, two queues, and two services on top of those two queues, each of which enforce the contract.

To prepare to send messages, we must first start up a dialog. In its most basic form, a dialog defines the exchange of messages between two services, and specifies a contract that will be used to enforce what messages can and cannot be sent by the involved services.

Once the dialog is created, you will receive a “dialog handle,” a GUID that uniquely identifies the dialog for the initiator–this GUID will be used any time the initiator needs to send messages to the target or receive messages that have to do with the dialog.

Note that the Service Broker primitive over which messages are sent is actually referred to as a “conversation.” A dialog is a specific type of conversation, a two-way exchange between two parties. As of today, it is also the only type of conversation supported by Service Broker, but given the potential for extensibility built into the design I suspect we will see monologs, trilogs, and other conversation types in future versions of SQL Server.

We’ll start a dialog between the two services we’ve created. Notice that the TO SERVICE is specified in quotes? SQL Server doesn’t necessarily need to know about a service in order to start sending messages to it; if TO SERVICE didn’t yet exist, the messages would simply start to queue. More on this topic in the next workbench.

Sending Messages

We’ve now told Service Broker that we want to start talking, but that’s about it. It knows what kinds of messages we want to send, and it knows who will be involved, but we haven’t actually communicated yet.

To send a message, use the SEND statement. Note that you send on a conversation rather than a dialog – this will keep things simple in the future, should other conversation types be added.

To send a message, you need three things: A dialog handle, which identifies your side of the dialog, a message type, which tells SSB how or how not to validate what you send, and of course you also need a message to send.

Now we’ll actually send a message. The GUID we generated in the last section is used by the initiator to send messages to the target.

Peeking at the Queue

Now that a message has been sent, we can presume that it’s sitting in the target queue. But there is only one way to find out, and that’s to take a look.

As mentioned before, queues can be treated just like tables for the sake of SELECT queries. Each message ships with a large amount of information – all available in the queue – and it’s important when working with SSB to know and understand what’s there.

So let’s pause for just a moment and explore…

Assuming that you’ve done everything right up to this point, you should be looking at a single row of data. A few interesting points to notice:

  1. The conversation_handle column contains a GUID, but you might notice that it’s not the same one you used to send the message; the GUID you’re looking at is the target’s handle.
  2. The message_sequence_number is an ascending integer, and for the row you’re looking at its value should be 0. As more data is inserted into the queue, that number will be incremented, much like an IDENTITY column for a normal table. Service Broker guarantees in-order, exactly once delivery, and the message_sequence_number is a big part of how that’s enforced.
  3. The service_name is the name of the service to which the message was sent. Since you’re now looking at the queue – not the service – and since a queue can have many services, it can sometimes be important to know where a particular message originated.
  4. The message_type_name is all-important, as you start creating complex solutions in which many different types of messages are moving back and forth; you’ll want to know what you’re dealing with!
  5. The message_body is binary, but the validation column has a value of “X” so we know that it’s actually just binary-encoded XML.

Receiving Messages

We’ve taken a glance at the contents of the queue, but haven’t actually received the message – it’s still there, waiting to be picked up, after which it will be removed from the queue – exactly once delivery means that the message shouldn’t be able to be picked up more than once.

When receiving messages, you want at a minimum three things:

  1. The message’s type, so that you know what to do with it. The type may be one of the ones defined in the contract used to start the dialog, or it may be one of the system message types–we’ll get into that shortly.
  2. The message’s body, obviously the point of this exercise.
  3. The dialog handle, so that you can reply to the message.

The T-SQL RECEIVE statement is much like SELECT, supporting a column list. It also optionally supports the TOP modifier, which we’ll use here to get only one message at a time.

A WHERE clause can be used too, but only two columns can be predicated: conversation_handle and conversation_group_id. Only one can be used at a time, and the logical operator used must be the equivalence operator – but that’s a topic for a future workbench.

Variables used to hold the handle, type, and body, respectively:

Sending a Message Back

Now that you’ve received a message, you’ve also gotten something else along with it: A dialog handle on which to send something back. The syntax is the same as before, but now we’re going the other way.

Ending the Conversation

We’ve said goodbye to the initiator, but the dialog is still active – and if we let it, it will sit there, ready and waiting for more messages, until the end of time. Or at least until you drop your database.

As I quickly learned on my first real world SSB project, having a bunch of old conversations sitting around in the database is not a good thing. They occupy space, and by leaving them there a busy database can quickly balloon out of control.

Of course, the designers of SSB foresaw this issue and created a way to get rid of them. This is called ending the conversation.

Ending the Conversation, Part 2

So now the conversation has been ended by the target. But wait a minute – the initiator didn’t get the goodbye message. That’s not a good way to end a conversation in person, and neither is it great here. But as it turns out, the conversation is still there, albeit in a slightly disabled state. The target is done and can do nothing else with the conversation. The initiator, on the other hand, can still do a couple of things: it can receive any remaining messages, and it can end the conversation itself.

Just like before, we can receive the messages one by one from the queue. But this time I’m going to modify the code just a bit. The first modification is a loop, to pull back every message on the queue. There are only two, so this shouldn’t take long.

The second modification is a new condition based on the message type, in order to handle an EndDialog message. This is a key Service Broker principle. As a conscientious Service Broker user, any time you (or the services you have created) receive an end dialog message, it is your duty to react in kind, also ending your side of the dialog.

Variables used to hold the handle, type, and body, respectively:

Waiting for Godot

We’ve come a long way, sending specific messages between services on dialogs that we’ve later ended. But before we close this workbench there is one final foundational piece that we must investigate: the enhanced WAITFOR statement that accompanies Service Broker.

WAITFOR, as you might recall, usually goes hand in hand with either the keywords DELAY or TIME, and is used to make your process sleep for a certain amount of time or until a certain time, respectively. It has an entirely new purpose in the Service Broker world: It can be made to wait for a message to appear on a specific queue or for a specific dialog. This means that you don’t have to poll when you’re working with Service Broker. No more messy loops and wasted resources.

The extended version of WAITFOR uses parenthesis, which contain a RECEIVE statement. You can also optionally specify a timeout, after which your process will break and stop waiting if no messages have been received.

Run the following in a new window:

Once you’ve started the WAITFOR, send a new message. You’ll need a new conversation as well.

As soon as you hit send, the batch in the other window should have finished running, and you should see the row from the queue corresponding to the message you just sent.

Cleanup

We’ve made a bit of a mess – and it’s always good to clean up when we’re done playing with new technologies. Once you’ve closed the window you opened to try WAITFOR, the following will wipe the slate clean.

You’ve now experienced a small taste of what Service Broker offers, both as a messaging and queuing technology, but there is a lot more power hidden beneath the surface.

In the next installment we’ll delve deeper, looking at the Service Broker locking semantics, taking messaging across databases, processing messages automatically with activation stored procedures, and investigating some of the Service Broker catalog views.

In part three of this series, we’ll look at applying the basics to produce some interesting solutions including priority queues, blocked callbacks, worker retries, and batching. In that installment I’ll also cover some basic performance techniques for getting the most out of Service Broker.

Downloads

Tags: , , , , ,

  • 48347 views

  • Rate
    [Total: 5    Average: 4.6/5]
  • Steve Seymour

    Nice Job
    Nice job, Adam. You did a fine job at summarizing the core concepts of service broker, and you provided an excellent, simple example.

    ~Steve

  • ALZDBA

    Nice job, here’s an art. about taking SB off-instance
    Mind me adding that if you want to take service broker off instance, this link may be a starting point.
    “Adventures With Service Broker” at http://www.sqlservercentral.com/columnists/jbijnens/2897.asp

  • Adam Machanic

    WAITFOR(RECEIVE * FROM Part2)
    Steve: Thanks for the kind words.

    ALZDBA: Part 2 of this workbench is coming and will cover that topic and more… 🙂

  • Jeremy

    Great Idea
    This is a great idea. Thanks for the work.

  • Jason H

    Just what I needed (Cars lyrics)
    This is perfect, gave the insite on how this stuff works. Just what I needed.

  • Pat E

    Very good foundational article
    Thank you. I like examples. I am lookin forward to subsequent articles.

  • Joe Egan

    Thanks
    …great article – well written and scoped perfectly. Thanks, Adam. Are you sure this wasn’t a howto on commenting code well, though? ;->

    Joe Egan, MCSA, Security+, Network+, Linux+, A+
    http://j0e3gan.blogspot.com

  • sattaru praveen

    Great Article
    Easy To UnderStand and to the point article.

  • Anonymous

    Service Broker
    great and timely article

  • Jeremy

    Great Idea
    This is a great idea. Thanks for the work.

  • Anonymous

    more crap from Redmond…
    Q:how much more crap are we going to attach to this db engine?

    A: until we destroy it…

    liken this to the acclaimed ‘Pivot’ statement; difficult to understand, difficult to implement. generally useless

    “Leave the good dbms alone”

  • leonset

    Very nice tutorial
    The tutorial is very easy to follow, but I’m missing something: how do you access the body of the message and assing it’s XML values to SQL variables? My objective is to be able of making some updates to different tables with the values received with SSB.

    Really newbie here… 😉
    Thanks a lot!

  • Anonymous

    brilliant exposition!
    explains the core things in just enough detail.

  • Anonymous

    Good Tutorial…
    … but as a person completely new to this could you provide one or two real world examples what business problem this would solve and why you would choose this technology? Thanks!

  • Mundo

    But…
    …why would I ever need to use SSB? It looks dead clever and that, but can someone give some examples of when it would make sense to use it?

    Adam says at one point “as you start creating complex solutions in which many different types of messages are moving back and forth”. What kind of solutions require many different types of message to move back and forth?

  • barretld

    Re: Service Broker
    I feel smarter after reading the article……..Thanks

  • barretld

    good read….
    I feel smarter after reading the article, thanks

  • sgambale

    question about login auditing with event notification
    Adam,

    I hope you or someone can address this problem.
    I am stumped!

    I setup an event notification for login using service broker, much like your examples.
    I insert rows into a table when a login occurs. It works fine. The problem is I want to selectively insert rows, depending on the loginname and what happens is once there is a login I do not want to insert into the table, the row sits in the queue and even subsequent logins I want to insert are not.
    This is the code. I have to somehow discard the message I receive when I do not want to insert and I cannot figure how to do it.

    USE [cptstest]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    CREATE PROCEDURE [dbo].[proc_log_user_logins_new_do_nothing]
    AS
    SET NOCOUNT ON;

    DECLARE @message_body XML,
    @message_type_name NVARCHAR(256),
    @dialog UNIQUEIDENTIFIER ;

    –Endless loop
    WHILE (1 = 1)
    BEGIN
    BEGIN TRANSACTION ;

    — Receive the next available message

    WAITFOR (
    RECEIVE TOP(1)
    @message_type_name=message_type_name,
    @message_body=message_body,
    @dialog = conversation_handle
    FROM log_user_logins_queue
    ), TIMEOUT 2000

    –Rollback and exit if no messages were found
    IF (@@ROWCOUNT = 0)
    BEGIN
    ROLLBACK TRANSACTION ;
    BREAK ;
    END ;

    –End conversation of end dialog message
    IF (@message_type_name = ‘http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog’)
    BEGIN
    PRINT ‘End Dialog received for dialog # ‘ + cast(@dialog as nvarchar(40)) ;
    END CONVERSATION @dialog ;
    END ;
    ELSE

    BEGIN
    IF CAST(@message_body.query(‘/EVENT_INSTANCE/LoginName/text()’) AS VARCHAR(100))
    NOT IN (‘m58467′,’ITSERVICESM10077′,’patrol_ssuser’,’patrol_ssadmin’)
    BEGIN
    INSERT INTO log_user_logins (
    EventTime,
    EventType,
    LoginName,
    HostName,
    NTUserName,
    NTDomainName,
    Success,
    FullLog )
    VALUES
    (
    CAST(CAST(@message_body.query(‘/EVENT_INSTANCE/PostTime/text()’) AS VARCHAR(64)) AS DATETIME),
    CAST(@message_body.query(‘/EVENT_INSTANCE/EventType/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/LoginName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/HostName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/NTUserName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/NTDomainName/text()’) AS VARCHAR(100)),
    CAST(CAST(@message_body.query(‘/EVENT_INSTANCE/Success/text()’) AS VARCHAR(64)) AS INTEGER),
    @message_body)
    COMMIT TRANSACTION
    END
    end
    END

  • sgambale

    question about login auditing with event notification
    Adam,

    I hope you or someone can address this problem.
    I am stumped!

    I setup an event notification for login using service broker, much like your examples.
    I insert rows into a table when a login occurs. It works fine. The problem is I want to selectively insert rows, depending on the loginname and what happens is once there is a login I do not want to insert into the table, the row sits in the queue and even subsequent logins I want to insert are not.
    This is the code. I have to somehow discard the message I receive when I do not want to insert and I cannot figure how to do it.

    USE [cptstest]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    CREATE PROCEDURE [dbo].[proc_log_user_logins_new_do_nothing]
    AS
    SET NOCOUNT ON;

    DECLARE @message_body XML,
    @message_type_name NVARCHAR(256),
    @dialog UNIQUEIDENTIFIER ;

    –Endless loop
    WHILE (1 = 1)
    BEGIN
    BEGIN TRANSACTION ;

    — Receive the next available message

    WAITFOR (
    RECEIVE TOP(1)
    @message_type_name=message_type_name,
    @message_body=message_body,
    @dialog = conversation_handle
    FROM log_user_logins_queue
    ), TIMEOUT 2000

    –Rollback and exit if no messages were found
    IF (@@ROWCOUNT = 0)
    BEGIN
    ROLLBACK TRANSACTION ;
    BREAK ;
    END ;

    –End conversation of end dialog message
    IF (@message_type_name = ‘http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog’)
    BEGIN
    PRINT ‘End Dialog received for dialog # ‘ + cast(@dialog as nvarchar(40)) ;
    END CONVERSATION @dialog ;
    END ;
    ELSE

    BEGIN
    IF CAST(@message_body.query(‘/EVENT_INSTANCE/LoginName/text()’) AS VARCHAR(100))
    NOT IN (‘m58467′,’ITSERVICESM10077′,’patrol_ssuser’,’patrol_ssadmin’)
    BEGIN
    INSERT INTO log_user_logins (
    EventTime,
    EventType,
    LoginName,
    HostName,
    NTUserName,
    NTDomainName,
    Success,
    FullLog )
    VALUES
    (
    CAST(CAST(@message_body.query(‘/EVENT_INSTANCE/PostTime/text()’) AS VARCHAR(64)) AS DATETIME),
    CAST(@message_body.query(‘/EVENT_INSTANCE/EventType/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/LoginName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/HostName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/NTUserName/text()’) AS VARCHAR(100)),
    CAST(@message_body.query(‘/EVENT_INSTANCE/NTDomainName/text()’) AS VARCHAR(100)),
    CAST(CAST(@message_body.query(‘/EVENT_INSTANCE/Success/text()’) AS VARCHAR(64)) AS INTEGER),
    @message_body)
    COMMIT TRANSACTION
    END
    end
    END

  • owie

    SSB consuming my memory
    Hi,

    I am using SSB to log audit_login,audit_logout,audit_login_failed. Using an SP to receive the messages. when everything was created (queue, service, route, event notification) i manually ran SP to gather the messages. The logging table gets populated but does not end coz the sproc can not completely ran on all messages in the queue causing the server to run out of memory and stop responding, making all processes fail too! i had it working on a smaller server with no problems. Could it be because of the server has too much connections coming in and out that all messages cant be received by the SP?

  • wkm1925

    SSB – At a glance
    Hi Adam Machanic,

    Your article on SSB really cool. Looks like I can enhance my current solutions for distributed transaction.

    My question as follow,
    I’ve 3 database in a 3 database server. It’s X DB, Y DB and Z DB. Let’s say my distributed transaction as follow
    1. X DB perform insert
    2. Y DB perform insert
    3. Z DB perform insert

    If I’m gonna this SSB. Once Y DB perform insert. Y DB perform insert, then Z DB cannot perform insert due to network down. Did a Y DB, and Z DB is roll back?

    Need help because I’m really beginner of this SSB.