Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How do you implement conversation groups for multiple users using their own instance of the user applications but sending related messages to the service broker queue? Any good example of this kind of implementation? I had imagined that I could set the conversation group ID myself, guaranteeing that certain groups of messages from various users will be related, but it seems that this value is going to be automatically generated as a UNIQUEIDENTIFIERS... Is this even the best way to do it?

I would like to make sure to catch and lock related messages where two users "on the same team" for instance are bidding on the same item together, so that the server application logic can properly process those related messages together (basically only the first one will be accepted and the second one will be rejected until the user's view can be updated so they know that the other user changed the bid already).

flow direction is from users A, B, C sending xml messages to the queue, then a dequeuing service which sends the xml to an external web service. The messages we are sending are structured xml messages which will modify orders on different items on the external web service after dequeuing.

Thank you!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You cannot use conversation groups to exclude application instances, if that's what you're trying to do. If Instance A needs to receive messages from the queue only for Instance A and Instance B needs to receive messages only for Instance B then instance A needs to use queue A and instance B needs to use queue B. Conversation group can be used only when Instance A and Instance B can both process any messages, but you want to exclude them from processing correlated messages concurrently. Every time I've seen someone trying to pre-generate conversation group IDs, the idea was always bad.

Update

The initiator cannot control the conversation group of the destination (target). Even if A and B were in the same conversation group at the initiator site, this does not mean they would be the same at the target site since conversation group is a local concept and doe snot travel with the message. If you want the messages sent by A and B to belong to the same conversation group on the target side, then the dialog has to be started in the 'reversed' order: the destination begins the dialog(s) and places them in a single conversation group, then the target(s) start sending messages on this dialog(s). So conversations act like an 'invitation' to send messages, the logical 'target' acts as the actual 'initiator'.

Update #2.

this is how it would work, in theory: say you have 10000 products. To do the 'reverse' pattern, when a user want to participate in the application it needs to 'join' in. So he starts a conversation with the server and send a message 'I want to join' (lts call this 'conversation 0'). The server processes this message by starting a conversation with this new user for each product. Now the user has 10000 conversations on which it can send 'bids'. For product A it uses conversation 1, for product B conversation 2 etc. When user B wants to join in it also starts a new conversation with the server and sends an 'I want to join' message. The server responds by starting 10000 conversation with this new user, again one for each product, and it makes sure each one is in the corresponding group for the product, so conversation for product A with user 1 is in the same group as conversation for product a for user 2.

Now obviously anyone will figure that this scheme is flawed: it requires number_of_products X number_of_user conversations, it makes adding and removing products a pain (must maintain all those user conversations!) and so on. One alternative is to multiplex products per conversation. Say the server only starts 10 conversations with each user and the user uses the conversation corresponding to the last digit of the product id (so product 1 goes to conversation 1, but so does product 11 or 101). This is more viable, it requires far fewer conversations, and requires no special conversation management when products are added or removed. You may consider that is a downside that now the server locks not all messages for product 1, but also all messages for product 11, all for 101 etc, but consider this: it only matters if you have more processing threads on the server than the number of conversations per user. If you have 5 threads, then it doesn't matter that you locked 1, 11, 101, there still are messages to process for the other 4 threads. Only if you'd have 11 threads it would matter, since the 11th thread has nothing to process.

Now I'm not advocating to deploy exactly this, I'm just pointing out some possibilities. In most cases the CG locking would be per user, not per product, and adding this extra dimension of using CG locking to avoid concurrency problems on each product is a little unorthodox.

And don't forget that the only construct that guarantees order in SSB is a conversation. So if users have to send bids for A on conversation 1 followed by bids for B on conversation 2 then there is no guarantee that the bid for A is going to be processed after the bid for B is processed. The only guarantee is that if user 1 sends two bids for A, they will be processed in the order sent.

Also, if two different users send bids for product A then there is no guarantee of the order of processing of these bids. However, if the bids for product A end up on the same CG then there is a guarantee that only one 'processor thread' will see both bid from user 1 and bid for user 2, but be careful because there is no guarantee that the bids are presented in the RECEIVE result set in the order they were received. RECEIVE only guarantees that:

  • all messages in the result set are from the same CG
  • the messages belonging to a conversation are in order

but the order of conversations in the result is basically random (is the driven by the conversationhandle ordering, a GUID).

Before I forget: remeber there is the also MOVE CONVERSATION but relying on MOVE (rather than starting conversations directly in the correct CG) is very very very deadlock prone.

share|improve this answer
    
pregenerated conversation group ideas bad... I see... what are your thoughts on the other direction? Flow direction is mainly instances A, B, and C the users sending items to the queue, and I was trying to think of how to create a conversation group that corresponded to some same item which users A and B were modifying together, or some other item which users A, B, and C happened to be modifying together at the same time. I'd let only the first one to get into the conv. group to go on and be processed by the dequeuing service, and return an "item locked while being modified" to the rest. –  fa1c0n3r May 24 '12 at 12:19
    
Regarding your update, thanks... That makes sense. Now, at the logical target which will act as the real initiator, I would need some classification logic to determine which messages are supposed to be in the same conversation group. Would you think it is a good idea if I query the xml in the message body at this point? Basically each message corresponds to an order modification on a product (such as an improvement on the bidding price), and if two messages on the queue are modifying the same product, I need them to be in the same group. Then we dequeue & send to external web service. –  fa1c0n3r May 24 '12 at 13:36
1  
See new update (a small novel...) –  Remus Rusanu May 24 '12 at 14:09
    
wow it is a novel. your multiplexing solution is really interesting, and practical (could almost be a chapter of some ecommerce/market software architecture book by itself :))... will take me a little time to think about how to best use all of this. Thanks again. –  fa1c0n3r May 24 '12 at 15:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.