Presentation is loading. Please wait.

Presentation is loading. Please wait.

Service Broker Overview Janis Griffin Senior DBA, Confio Software

Similar presentations


Presentation on theme: "Service Broker Overview Janis Griffin Senior DBA, Confio Software"— Presentation transcript:

1 Service Broker Overview Janis Griffin Senior DBA, Confio Software
SQL Server Service Broker Overview Janis Griffin Senior DBA, Confio Software 1 1

2 Who Am I? Senior DBA for Confio Software
20+ Years in SQL Server & Oracle DBA and Developer Specialize in Performance Tuning Review Performance of 100’s of Databases for Customers and Prospects

3 Agenda Introduction To Service Broker – Why?
Broker Component Definitions – What? Simple Use Case – How? Case Study – Tying It Altogether New Features in 2008

4 Introduction – Service Broker
Available in 2005 / 2008 Sql Server Is automatically enabled when database is created. Allows For Message-Based Applications (SOA) Speeds up processing & Shortens interactive response time Increases overall application throughput Highly Flexible & Reliable Can create stand-alone independent applications that can be loosely coupled together. Useful in Auditing and/or Replication Applications Can De-Couple Tightly Integrated Applications Asynchronous calls in single instance or across multiple instances (Scalability) Reduces the need for distributed transactions (Linked Server)

5 Broker Component / Objects
Messages & Message Types Data type is Varbinary(max) – up to 2GB per message Guarenteed Delivery (better than MSMQ – standard window messaging) Validation Types: NONE, EMPTY, WELL_FORMED_XML, etc. Contracts Define who can send what types of messages - Initiator/Sender or Target/Receiver Can have multiple message types. Services Represents business tasks that processes messages on a queue Logical Endpoints define message types, contracts, and queues holding messages Can be a Stored Procedure and/or Other Programs linked with a Dialog Queues Hidden Tables which holds the messages - Only Select / Recieve commands FIFO processing / Can Automatically Processed Messages via Activiation service Dialogs and/or Conversations Mechanism to put Messages in Queue Created for a particular task & Occur between 2 end points (service brokers). Conversation Groups Keep related Messages together Can be used to rollback all message processes if one message fails 5

6 Service Broker Architecture

7 Simple Message Example
if DATABASEPROPERTY('BrokerDB', 'Version') is not null drop database BrokerDB go create database BrokerDB -- ALTER DATABASE [BrokerDB] SET ENABLE_BROKER; -- SET NEW_BROKER; -- service broker is automatically enabled if newly created -- it’s disabled if database restored or attached SELECT is_broker_enabled, service_broker_guid, is_honor_broker_priority_on FROM master.sys.databases WHERE name = ’BrokerDB’;

8 Create QUEUES & SERVICES
use BrokerDB go CREATE QUEUE receiveQ CREATE SERVICE receiveS ON QUEUE receiveQ ([DEFAULT]) -- SERVICE 'receiveS' can receive contract of DEFAULT. -- Brackets are needed because DEFAULT is a reserved word. -- SERVICE receiveS CREATED

9 Create QUEUES & SERVICES
CREATE QUEUE sendQ go CREATE SERVICE sendS ON QUEUE sendQ -- SERVICE 'sendS' doesn't have contract. -- It can name contract in ‘Begin Dialog’ -- SERVICE sendS CREATED

10 Let’s Try Sending A Message
UNIQUEIDENTIFIER; BEGIN TRY BEGIN TRAN; BEGIN DIALOG FROM SERVICE sendS TO SERVICE 'receiveS' WITH ENCRYPTION = OFF; SEND ON ('Hello Out There - You Beautiful People!!! Lets Have Some FUN!!!'); COMMIT; END TRY BEGIN CATCH PRINT ERROR_MESSAGE() ROLLBACK; END CATCH -- 2-Way, bad practice to end conversation here - EndDialog Message left in receiveQ go

11 Look for Message Information
IS MESSAGE SENT?? How can you tell? select * from receiveQ; -- If no records (errors), select * from sys.transmission_queue Other sys views to review: sys.conversation_endpoints sys.conversation_groups sys.conversation_priorities (2008 only)

12 Receive Message from receiveQ
UNIQUEIDENTIFIER; VARCHAR(MAX); BEGIN TRAN; RECEIVE @m=CAST(message_body as VARCHAR(MAX)) FROM receiveQ; -- For 2-way, don't end conversation ‘with cleanup’ clause -- END WITH CLEANUP; END COMMIT; go 12

13 Look for Message -- MESSAGE RECEIVED?? How can you tell?
select * from sendQ go -- if using ‘END WITH CLEANUP -- no record in sendq select * from sys.conversation_endpoints

14 Receive Message from sendQ
UNIQUEIDENTIFIER; VARCHAR(MAX); BEGIN TRAN; RECEIVE @m=CAST(message_body as VARCHAR(MAX)) FROM sendQ; END COMMIT; go

15 Better To Automate SendQ Msgs
Use BrokerDB go -- better to have a ACTIVATION program on sendQ to processes end conversation & errors CREATE PROCEDURE sendQ_msg_cleaner AS UNIQUEIDENTIFIER; SYSNAME; NVARCHAR(4000); BEGIN TRANSACTION; WAITFOR ( = [conversation_handle], @message_type = [message_type_name], @message_body = CAST([message_body] AS NVARCHAR(4000)) FROM sendQ), TIMEOUT 1000;

16 More sendQ_msg_cleaner proc.
IS NOT NULL BEGIN = N' RAISERROR (N'Received error %s from service [receiveQ]', 10, -- must have SYSADMIN privs to specify 'WITH LOG' options END END COMMIT; = NULL; BEGIN TRANSACTION; WAITFOR ( = = = CAST([message_body] AS NVARCHAR(4000)) FROM sendQ), TIMEOUT 1000; GO

17 Set up ACTIVATION on sendQ
ALTER QUEUE sendQ WITH ACTIVATION ( STATUS = ON, MAX_QUEUE_READERS = 2, PROCEDURE_NAME = [sendQ_msg_cleaner], EXECUTE AS OWNER); GO

18 Service Broker Information
sys.service_message_types - One row per message type defined. sys.message_types_xml_schema_collection_usages - One row for each message type that refers to an XML schema collection. The XML schema collection is used for message validation. sys.service_contracts - One row per contract. sys.service_contract_message_usages - One row for each use of a message type by a contract. Message types are not contract-specific; for example, Contract 1 can use message types A, B, and C and Contract 2 in the same database can use message types A, B, D, and E. sys.service_queues - One row for each queue defined. sys.services - One row for each service defined in a database.

19 More Information Cont. sys.service_contract_usages - One row for each use of a contract by a service. Contracts are not service-specific; for example, Service 1 can use contracts A and B, and Service 2 can use only contract A. sys.internal_tables - A queue is a special kind of "view" over an internal table. You can see metadata that refers to the queue tables by looking in this table for internal tables with an internal_type of 201. The parent_id of the internal queue table in sys.internal_tables is the object id of the queue. You cannot access these internal tables directly. Services, contracts, and message types are only metadata - Queues contain message bodies with the actual message and occupy space in the database, like tables do. You can use the system stored procedure sp_spaceused to determine how much space is used by each queue. sys.transmission_queue - A special table that holds messages (and takes up room in the database's default FILEGROUP). The sys.transmission_queue table does not have a corresponding table in sys.internal_tables, so sp_spaceused doesn't recognize this table. Queues and service metadata are used when you start up a dialog conversation from one service to another.

20 DMV’s For Service Broker
sys.dm_broker_activated_tasks contains a row for each stored procedure activated by Service Broker. It can be joined to dm_exec_sessions.session_id using the spid column. sys.dm_broker_connections contains a row for each Service Broker network connection. sys.dm_broker_forwarded_messages contains a row for each Service Broker message that an instance of SQL Server is in the process of forwarding. sys.dm_broker_queue_monitors contains a row for each queue monitor in the instance. A queue monitor manages activation for a queue.

21 Wait Types For Service Broker
BROKER_CONNECTION_RECEIVE_TASK - Service Broker (and Database Mirroring) connection endpoint has a list of buffers posted to receive data from the network. Avg_wait_time_ms should be very low. BROKER_ENDPOINT_STATE_MUTEX - Each time there is a state change for a Service Broker connection endpoint during the connection or handshake phase. Avg_wait_time_ms for this wait type should be low. BROKER_EVENTHANDLER - Each SQL server instance has a primary event handler thread for processing Service Broker startup/shutdown and timer events. Often considered an Idle Wait BROKER_INIT - This wait type is charged each time Service Broker fails to initialize internal broker managers for any database. These events should be rare. BROKER_MASTERSTART - This wait type is charged only during instance startup. BROKER_RECEIVE_WAITFOR - is for the WAITFOR RECEIVE SQL statement, where the statement execution waits for messages to arrive in the user queue. BROKER_REGISTERALLENDPOINTS - is charged only during instance startup, when Service Broker is waiting for all endpoint types to be registered. BROKER_SERVICE - is charged when next hop destination list associated with a target service/broker instance pair gets updated or re-prioritized due to addition or removal of a dialog to the target service/broker instance pair. BROKER_SHUTDOWN - is charged only during instance shutdown, when Service Broker waits a few seconds for its primary event handler and all connection endpoints to shutdown. BROKER_TASK_STOP - Service Broker has several task handlers to execute broker internal tasks related to transmission of messages, asynchronous network operations and processing of received messages. BROKER_TO_FLUSH - For performance reasons Service Broker maintains all dialog state (TO - transmission object) in memory as well as in temporary tables on disk. Every time a TO is updated, it is scheduled to be flushed lazily to the temporary table on disk. BROKER_TRANSMITTER - Service Broker has a component known as the Transmitter which schedules messages from multiple dialogs to be sent across the wire over one or more connection endpoints.

22 Case Study – Ordering System
Portal to Order Motorcycle Parts Over the Internet BrokerDBDatabase customers inventory orders billing shipping

23 Case Study – Ordering System
use BrokerDB go CREATE TABLE orders ( order_id INT PRIMARY KEY IDENTITY, customer_id INT, order_date DATETIME, product_id CHAR(10), order_quantity INT, tot_amount DECIMAL(7,2)) CREATE TABLE inventory ( quantity INT)

24 Simple Ordering Transaction
INSERT INTO inventory VALUES ('B4-SX300AZ', ) Go -- Put it in A Simple Transaction Without Service Broker BEGIN TRANSACTION INSERT INTO orders VALUES (3333,getdate(),'B4-SX300AZ', 10, 44.90); UPDATE inventory SET quantity = quantity - 10 WHERE product_id ='B4-SX300AZ' ; COMMIT go

25 Case Study – Ordering System
orders BrokerDBDatabase inventory SendS Service ReceiveS Service Dialog SendQ Queue ReceiveQ Queue Receive Receive Store Proc Application Send Send

26 De-couple Orders From Inventory
‘Sending Update Inventory Information’ Procedure. CREATE PROCEDURE send_update_inventory VARCHAR(10)) AS UNIQUEIDENTIFIER; BEGIN TRY BEGIN DIALOG FROM SERVICE sendS TO SERVICE 'receiveS' WITH ENCRYPTION = OFF; SEND ON + ',' END TRY BEGIN CATCH PRINT ERROR_MESSAGE() END CATCH go

27 Get Info Off Queue & Update Inv
WHILE 1 = 1 BEGIN RECEIVE TOP(1) @h=conversation_handle, @t=message_type_name, @m=CAST(message_body as VARCHAR(MAX)) FROM receiveQ WHERE conversation_group_id IF <> 1 BREAK; = N' = N' END CONTINUE END Need error handling if real order entry system = as int); = UPDATE inventory SET quantity = quantity WHERE product_id END COMMIT TRANSACTION; go CREATE PROCEDURE update_inventory AS UNIQUEIDENTIFIER; UNIQUEIDENTIFIER; VARCHAR(MAX); NVARCHAR(128); INT; CHAR(10); WHILE 1 = 1 BEGIN BEGIN TRANSACTION WAITFOR( GET CONVERSATION FROM receiveQ), TIMEOUT 100 ; IS NULL ROLLBACK TRANSACTION; BREAK; END

28 Turn on Activation ALTER QUEUE receiveQ WITH ACTIVATION (STATUS=ON,
PROCEDURE_NAME=update_inventory, MAX_QUEUE_READERS=5, EXECUTE AS SELF ) go

29 Let’s try it out SET NOCOUNT ON DECLARE @i INT, @q INT SET @i = 0
= 10 < 100 BEGIN BEGIN TRANSACTION INSERT INTO orders 4.49); EXECUTE send_update_inventory COMMIT + 1; END go

30 Demonstration Simulation of Order Entry With Inventory Without Inventory -- Let's try it out set nocount on INT = 0 = 10 < 100 BEGIN BEGIN TRANSACTION INSERT INTO orders 4.49); EXECUTE send_update_inventory COMMIT + 1; END go SELECT * FROM inventory ---- Turn ON ACTIVATION for receiveQ QUEUE ALTER QUEUE receiveQ WITH ACTIVATION (STATUS=ON, PROCEDURE_NAME=update_inventory, MAX_QUEUE_READERS=1, EXECUTE AS SELF ) go SELECT COUNT(*) from receiveq go -- RUN orders transaction INT = 0 < 1000 BEGIN BEGIN TRANSACTION INSERT INTO orders VALUES(3333,getdate(),'B4-SX300AZ', 1, 4.49); EXECUTE send_update_inventory 'B4-SX300AZ',1 COMMIT + 1 END SELECT * FROM inventory SELECT * FROM inventory go -- Let's try it out set nocount on INT = 0 = 10 < 100 BEGIN BEGIN TRANSACTION INSERT INTO orders 4.49); EXECUTE send_update_inventory COMMIT + 1; END Go ---- Turn off ACTIVATION for receiveQ QUEUE ALTER QUEUE receiveQ WITH ACTIVATION (STATUS=OFF, PROCEDURE_NAME=update_inventory, MAX_QUEUE_READERS=5, EXECUTE AS SELF ) SELECT COUNT(*) from receiveq go -- RUN orders transaction INT = 0 < 1000 BEGIN BEGIN TRANSACTION INSERT INTO orders VALUES(3333,getdate(),'B4-SX300AZ', 1, 4.49); EXECUTE send_update_inventory 'B4-SX300AZ',1 COMMIT + 1 END SELECT * FROM inventory ---- Turn off ACTIVATION for receiveQ QUEUE ALTER QUEUE receiveQ WITH ACTIVATION (STATUS=OFF, PROCEDURE_NAME=update_inventory, MAX_QUEUE_READERS=5, EXECUTE AS SELF ) go

31 On Separate SQL Servers
Certificates & keys Orders Inventory Routes Service Binding Routes Service Binding Endpoint Endpoint SendS Service ReceiveQ Service Dialog SendQ Queue ReceiveQ Queue Receive Receive Store Proc Send Send Stored Proc

32 Remote Setup – More Objects
Routes – tables that figure out where other services live Required if Services are across several Instances Defined at the Database. Route in MSDB is used for arriving messages Requires endpoints to be setup See sys.routes EndPoints – allow SQL Server to communicate over the network. A Service Broker endpoint allows messages to be sent and received separate Instance in the network Provides transport security and message forwarding. Listens on a specific TCP port. Must create in Master DB, with CREATE ENDPOINT DDL Remote Service Binding – defines security credentials to initiate a conversation with a remote service Requires Certificates / Session Keys ANONYMOUS=ON - Public Role must have CONNECT privs Dialog Security and/or Transport Security See sys.remote_service_bindings

33 What’s New in 2008 Service Broker Priority
Priority can be from 1 – 10 (Defaults to 5) Set at Local / Remote Service Endpoints & Contract Can be used to change FIFO order Ex: ALTER DATABASE BrokerDB SET HONOR_BROKER_PRIORITY ON; CREATE CONTRACT Platinum ([DEFAULT] SENT BY ANY); CREATE BROKER PRIORITY PlatinumCustomer FOR CONVERSATION SET (CONTRACT_NAME = Platinum, LOCAL_SERVICE_NAME = sendQ, REMOTE_SERVICE_NAME = 'receiveQ', PRIORITY_LEVEL = 7); … BEGIN DIALOG FROM SERVICE sendQ TO SERVICE 'receiveQ' ON CONTRACT [Platinum] …

34 What’s New in 2008 ssbdiagnose – A new Command Line Utility to check SSB configuration See all parameters: ssbdiagnose -? C:\>ssbdiagnose –E -d BrokerDB CONFIGURATION FROM SERVICE sendS TO SERVICE receiveS Microsoft SQL Server Service Broker Diagnostic Utility D BrokerDB Service receiveS was not found D BrokerDB No valid certificate with a private key was found for user dbo D BrokerDB The user dbo from database BrokerDB on JGRIFFIN-2\S2008R2 cannot be mapped into this database using certificates D BrokerDB User IntUser does not have CONTROL permission on service receiveS D The Service Broker endpoint was not found An internal exception occurred: No such host is known 6 Errors, 0 Warnings

35 Summary Service Broker brings queuing and reliable messaging to SQL Server – 2005/2008 Service Broker allows messages to be passed between applications, at the database level. Applications can use a single shared SQL Server database for this purpose or distribute their work across multiple databases. Allows for 1-way or 2 way Messaging Easy to use as Service Broker handles all of the details of message passing, including: locking retries rejecting ill-formed messages ensuring “once in-order” delivery

36 Confio Software Wait-Based Performance Tools
Ignite8 - SQL Server, Oracle, DB2, Sybase Provides Help With Identifying Biggest Performance Issues Gathering All Details for Immediate Resolution Monitoring Continuously for Normal versus Abnormal Based in Colorado, worldwide customers Free trial at


Download ppt "Service Broker Overview Janis Griffin Senior DBA, Confio Software"

Similar presentations


Ads by Google