Home > SQL Server Tips > > Service Broker in SQL Server: How and when to use it
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

Service Broker in SQL Server: How and when to use it


By Serdar Yegulalp, Contributor
Rating: -4.33- (out of 5)

The description that Microsoft provides for SQL Server 2005's Service Broker is maddeningly vague: "Service Broker is a new technology … that helps database developers build secure, reliable, and scalable applications … [It] provides queuing and reliable messaging as part of the Database Engine. These features provide the infrastructure necessary to build high-performance applications that easily scale-up or scale-out."

What does all this mean for the average database developer? For one thing, it's suddenly much easier to write applications that work across multiple instances of SQL Server, whether on the same machine or across multiple machines. Service Broker guarantees that a message passed from one server to another will get there. If by chance it doesn't, the resulting error can be trapped and dealt with.

Service Broker's basic unit of communication between two instances of SQL Server is called a dialog; and it allows messages to be passed in both dir...


RELATED CONTENT
SQL Server Stored Procedures
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Top tips and tricks for SQL Server database development
Top 10 SQL Server development tips of 2008
SQL Server trigger vs. stored procedure to receive data notification
SQL Server errors, failures and other problems fixed from the trenches
SQL Server and data manipulation in T-SQL
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure

SQL Server High Availability, Scalability and Reliability
SQL Server high availability: Options and caveats
Are data warehouses made for the cloud?
Top load balancing methods for SQL Server
Ensuring high availability of SSAS databases
Maintaining high availability of SQL Server virtual machines
Creating fault-tolerant SQL Server installations
Scaling up vs. scaling out with SQL Server 2008
How to configure storage in SQL Server database with more writes than reads
SQL Server database replication tutorial
Licensing a standby server for SQL Server replication
SQL Server High Availability, Scalability and Reliability Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


ections. It also ensures that the messages are received in the order they were sent. This way, you're not, for instance, trying to reference data that hasn't yet been inserted. Messages can also be subjected to a contract, which is, essentially, a rule enforced on both ends regulating what message types can be included in a conversation and who is authorized to send and receive them. Messages are subject to security restrictions and can be encrypted in transit, which comes in handy if you're using WAN links to pass said messages.

The really interesting stuff comes with Service Broker's queuing system. Received messages are held in a queue -- a concept that should already be familiar to SQL Server admins. Service Broker's queues are not just passive mechanisms; they can be queried with a SELECT statement like a table (for instance, to see if a given message is in the queue and waiting to be processed). This makes it possible to do some fairly elegant anticipatory programming.

Here are some examples of reasons you might use Service Broker:

  • Asynchronous triggers. You can construct asynchronous triggers either to a remote server or looping back to the same server. The latter is actually fairly common. A stored procedure or T-SQL command could fire a trigger that queues up another job through Service Broker, which is performed in a separate transaction and divorced entirely from the context of the original job.

  • Anticipatory scheduling. If you have a job that runs regularly and you want to make sure no commands of a given type are running or about to be run (such as a bulk insert or some other major data-changing action), you can poll the message queue. This will see if any such jobs are waiting and then either void them or wait until they have been processed before beginning the scheduled job.

  • Asynchronous data collection. If you need to poll several different servers for data, you could use the Service Broker to dispatch a request and perform the data collection that way. The results could be collected in a table and then sorted later.

  • Parallel processing. Any operations that need to be performed on multiple servers at once (such as harvesting and aggregating information from several databases) could be coordinated through Service Broker.

  • As a substitute for "contrived queues." Many databases I've worked with involved a mechanism I call a "contrived queue" -- a table in the database that functions as a queue. A programmer could switch this sort of thing over to Service Broker and benefit from having a standardized way to queue actions for local processing. It is more efficient and a bit better at self-documenting, since it might not be as clear what the queue table is for or how it's used.
  • In short, any operations that require queues or messages to be set up and passed consistently and reliably between instances of SQL Server, or even back to the same instance, could benefit from using Service Broker.

    About the author: Serdar Yegulalp is editor of the Windows Power Users Newsletter. Check it out for the latest advice and musings on the world of Windows network administrators -- and please share your thoughts as well!

    More information from SeachSQLServer.com

  • ATE: Stored procedure concurrency problems in SQL Server 2005
  • Tip: Developing with SQL Server 2005 Express
  • Guide: T-SQL Learning Guide


    Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2010, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts