Jaa


Service Broker Routing

This topic describes the details of how Service Broker routes messages. For an overview, see Routes.

For most applications, a simple approach to Service Broker routing works well. In each database that contains a service, specify a route for the external services that the service communicates with. However, Service Broker provides a sophisticated routing system for handling cases where an application needs more complex behavior. For examples that illustrate the routing process, see Service Broker Routing Examples.

Routing Process Description

SQL Server maintains two distinct levels of routing information. Each database contains a local routing table, sys.routes, for conversations begun in that database. For conversations that originate within the SQL Server instance, SQL Server searches the routing table in the database that created the conversation. For conversations that arrive from outside the instance, SQL Server searches msdb.sys.routes.

The basic matching process is identical whether the conversation originates within the instance or outside the instance. The process ignores routes that have expired. The routing process consists of three distinct steps:

  1. Finding matching routes. Service Broker finds a set of possible routes by matching the service name and the service broker identifier.
  2. Choosing a route. Service Broker chooses a route from among the set of possible routes.
  3. Locating the destination service. When the route chosen specifies 'LOCAL' as the network address, Service Broker locates the service within the instance. If the service does not exist in the instance, Service Broker may return to step 2 and choose another route.

Once a message has been sent from the initiator to the target and the initiator receives an acknowledgement message from the target, the initiator uses the service broker identifier in the acknowledgement messages to route subsequent messages to the same target. Service Broker handles acknowledgement messages; the process is transparent to an application that uses Service Broker. For more information about acknowledgement messages, see Service Broker Communication Protocols.

Reply Messages From a Target Service

When a message arriving from outside of the instance is from a target service, SQL Server checks to see if the current instance contains the service broker identifier in the message. If so, then the message is delivered within the current instance as described in the "Locating the Destination Service" section later in this topic. Otherwise, SQL Server follows the standard matching process.

Finding Matching Routes

The following procedure describes how SQL Server matches routes. At each step, if one or more routes match, the matching process ends, and Service Broker chooses one of the following matching routes:

  1. If the conversation specifies a service broker identifier, find a route with an exact match for both the service name and the service broker identifier.
  2. Find an exact match for the service name among routes that do not specify a service broker identifier.
  3. If the conversation does not specify a service broker identifier, find an exact match for the service name among routes that specify a service broker identifier. If the routing table contains routes that match the service name and have different service broker identifiers, arbitrarily pick a service broker identifier and match only the routes that use that service broker identifier.
  4. If a route to a dynamic routing service is present, and no request for a route to the service is pending, mark the conversation delayed and request routing information from that service.
  5. Find a route that specifies neither the service name nor the service broker identifier.
  6. If the conversation specifies a service broker identifier and if the instance contains one or more databases that contain services with names that match the name specified in the conversation, route the conversation as though the routing table contained a route with the service name and the network address 'LOCAL'.
  7. Mark the conversation delayed.

When a conversation is marked delayed, Service Broker performs the matching process again after a time-out period. Notice that failure to find a matching route is not considered an error.

Choosing a Route

If the matching process finds more than one matching route, Service Broker chooses one route from among the matching routes. For this purpose, routes that have the same service broker identifier, service name, and network address are considered to be identical. Service Broker uses the following procedure to choose the exact route. At each step, the process continues at the next step if there are no routes that match the address specification for the step.

  1. Choose one route from among the routes that specify a mirror address.
  2. Choose one route from among the routes that specify 'LOCAL' as the network address. If this SQL Server instance does not contain a service that matches the name specified in the conversation, continue at step 3.
  3. Choose one route from among the routes that specify a network address.
  4. Choose one route from among the routes that specify 'TRANSPORT' as the network address.

If broker forwarding is not active, Service Broker drops the message if the conversation does not originate in the current instance and the address of the route chosen is not 'LOCAL'.

Locating the Destination Service

As described earlier, Service Broker delivers messages to a service in the current instance when the matching route specifies 'LOCAL' as the network address. For messages that originate outside the instance, the route must be in msdb.sys.routes. For messages that originate within the instance, the matching route must be in the sys.routes table for the database that initiates the conversation.

Once Service Broker determines that the service for the message is in the current instance, Service Broker must locate the service within the instance. When a service broker identifier for the conversation exists in either the conversation or the route, Service Broker delivers messages to the database identified by the service broker identifier.

Otherwise, Service Broker locates the service by first searching for the service name in the database that contains the conversation, then searching for the service name in the other databases within the instance. Service Broker delivers the message to the first service located. Notice, however, that the order in which Service Broker searches the other databases in an instance is unspecified, and is not guaranteed to be consistent from conversation to conversation. This means that if more than one copy of the target service exists in the instance, Service Broker randomly picks the service to target.

Other Considerations

For improved reliability, Service Broker routing contains safeguards against routing loops. Service Broker routing is aware of database mirroring, and can transparently redirect conversations to the active partner of a mirrored database.

Routing Loops

Service Broker message forwarding tracks the number of times that a message has been forwarded to protect against endless routing loops. For more information, see Service Broker Message Forwarding.

If the matching route contains a network address that resolves to the current instance, SQL Server treats the conversation as though the conversation originated outside of the instance. Service Broker routes messages for the conversation using the routes in msdb.sys.routes. Routing for these messages is identical to routing for messages from outside the instance. In particular, message forwarding must be active for Service Broker to forward the message to a network address other than 'LOCAL'.

Mirror Addresses

Routes with mirror addresses have the highest precedence when choosing a route from among the initial set of matching routes. However, Service Broker does not give special consideration to mirror addresses when finding matching routes for a conversation.

When Service Broker chooses a route that specifies a mirror address, and Service Broker has not previously delivered a message using the route, Service Broker sends a request to both addresses to determine which instance is currently the principal. Once Service Broker identifies the principal, Service Broker sends all messages that use the route to the principal without contacting the mirror instance. If the principal is unreachable, or that instance indicates that it is no longer the principal, Service Broker sends messages to the other address for the pair if the SQL Server instance at the other address indicates that it is the new principal.

In cases where Service Broker cannot reach the principal but the partner does not claim to be new principal, Service Broker does not send messages to the partner. Service Broker then retries the principal address and the partner address until either the principal is reachable, or the partner indicates that it is now the principal. By taking this approach, Service Broker reliably delivers messages in cases where the principal and partner can communicate, but the instance sending the message cannot reach the principal.

See Also

Concepts

Service Broker Message Forwarding
Service Broker Communication Protocols

Other Resources

sys.routes (Transact-SQL)
CREATE ROUTE (Transact-SQL)
ALTER ROUTE (Transact-SQL)
DROP ROUTE (Transact-SQL)

Help and Information

Getting SQL Server 2005 Assistance