5

In my project I receive a transaction from the client then process it and return a status back to the client via WCF. As you know I have to somehow save the transaction for recovery purposes and persistence.

I am thinking of using MSMQ for that purpose. When the transaction starts I will "save" it on MSMQ (database save will occur in a batch for performance reasons).

Is MSMQ good for that? Do you know of better way to create persistence? What is the best way to "backup" the transaction and keep high performance?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
guyl
  • 2,158
  • 4
  • 32
  • 58

3 Answers3

5

When picking a technology I think it's helpful to consider not only can the technology meet your needs, but also whether it was designed to meet your needs. By this I mean that you should choose the best option rather than just the first option that seems good enough. You could probably solve this problem with logging or text files or various other means, but that doesn't mean you should.

My order of preference in this situation would be

  1. database
  2. MSMQ
  3. everything else

If it isn't possible to save transactions to database for whatever reason then MSMQ can probably help you here. It should perform better than a opening a database connection & committing yet provides a 'good' persistence layer. The downside is that it's more code and another point of failure for your application (not that it will fail if written properly, but more code means more places for bugs).

You can throw your transactions into a queue very easily using something like this

private string queuePath = @".\Private$\myQueue";
MessageQueue queue = new MessageQueue(queuePath);

Message message = new Messge();
message.Id = "messageId";
message.Body = "my content";

queue.Send(message, transaction);
transaction.Complete();
queue.Close();

and then retrieve them later through querying properties: MSMQ querying for a specific message. There's a lot of other functionality out of the box but keep it simple.

Some relevent questions:

Community
  • 1
  • 1
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
3

I think you are looking for SQL Server Service Broker. Everything that 10 years ago we did with MSMQ we now are doing with the Service broker. It works well.

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • Service Broker is great but relies on database availability & response time, which may be something OP is trying to work around. – Kirk Broadhurst Nov 06 '11 at 22:00
  • That applies to any transactional queuing system so it's not relevant to a specific product. – Deleted Nov 06 '11 at 22:37
  • @ChrisSmith Was that directed at me? Use mentioning so I get notified :) In this case MSMQ would run on the webserver itself so there's no 'availability' concern - if the webserver is online, so is the message store, whereas database typically runs on another machine. And response time is great because MSMQ runs locally, while database might be somewhere halfway around the world. – Kirk Broadhurst Nov 06 '11 at 23:49
  • @KirkBroadhurst: sorry missed that out :) So what happens when you need two web servers? Communication is inevitable if you need to scale. – Deleted Nov 06 '11 at 23:55
  • @ChrisSmith That's true - we don't really know the full picture. It might be sufficient for each webserver to have it's own queue - as long as the transaction is retained 'somewhere' it may suffice. As I said in my answer, I'd always prefer to use the database as first port of call whenever possible. – Kirk Broadhurst Nov 07 '11 at 00:06
  • @KirkBroadhurst: Indeed I agree. I think NServiceBus kind of bridges the gap here but I won't go further into that as it's 00:08 here and I really should be in bed :) – Deleted Nov 07 '11 at 00:09
  • How can you interoperate between .net code and SQL Service Broker? – tom redfern Nov 07 '11 at 08:52
  • Could you please answer http://stackoverflow.com/questions/9702379/queuing-in-oneway-wcf-messages-using-windows-service-and-sql-server ? – LCJ Mar 14 '12 at 13:10
2

A popular approach used by database and file systems is called Write-Ahead Logging. This is not however trivial to implement. You can find details here...

Wikipedia: Write-Ahead Logging

Phil Wright
  • 22,580
  • 14
  • 83
  • 137
  • Could you please answer http://stackoverflow.com/questions/9702379/queuing-in-oneway-wcf-messages-using-windows-service-and-sql-server ? – LCJ Mar 14 '12 at 13:10