Reputation: 893
I have to design a system of clients connecting to WCF service(s) to perform read and write operations on a database, and also get notifications.
I was told to use CQRS pattern.
For the sake of an example, clients will connect to a service to perform operations like Get List Of Products and Update Product. They will also be able to do something like Accept Shipment and Reject Shipment (which might cause a race between clients who is the one to do it first). Only one client can 'accept' a shipment or 'reject' a shipment.
So I read a little bit about CQRS and understood it decouples the reads from the writes (using commands). However, I am not sure about a few major issues if I use CQRS :
If I use CQRS pattern on the WCF service - can I count on things being done synchronously on the database ? I am a bit confused, cause I don't want the service to be single threaded (to support future scalability), but on the other hand - how do I make sure that write operations on the service are performed in the right order ? or even read operations ? Does the CQRS pattern guarantee ordered processing ? (someone told me here that CQRS pattern uses an 'update' queue to update requests for offline processing).
Does using CQRS eliminate concurrency problems ?
Should I still use 'TransactionScope' in all my command handlers that interact with the database ?
I have spent more than a week in trying to understand how to implement the notifications service for the clients, with no luck. I have this design:
'Product Service' will be a CQRS service, but I have trouble with the notifications service. The client may send a command to the product service to be notified about products of category X. This command will update the request in the database. Let's say for now that the notification service polls the database every 15 minutes, and checks which user wanted to be polled on which category and then sends new products to users that requested to be notified on those product's categories. What happens now if a user changes a category for a product and 20 other users already see this product in their notification window ? I need some way to detect that the product is no longer of that category, and to send them a notification like 'remove that product from your view'. This doesn't sound so much like notifications. It sounds more like 'Request a CONSTANT RELEVANT view of a database table, and every change should be reflected to the client's screen'. How do I do this kind notification service ??
Upvotes: 3
Views: 1726
Reputation: 9945
Upvotes: 0
Reputation: 51654
This may not be an answer to your specific questions, but might be helpful in evaluating if, why, and in what parts of yor application you can (or maybe should) use CQRS at all?
More specifically: CQRS is neither a silver bullet that should be applied no matter what, nor is it an overarching architecture style across the whole of your application.
CQRS can provide many advantages when applied to single and well specified Bounded Contexts (see Domain Driven Design by Eric Evans).
Questions you or your team should ask first:
Long story short: Don't try to force a style upon your whole application. Identify BCs and for each use the simplest solution that meets the requirements. This can very well be CQRS but for one or two BCs within your application at most.
Concentrate the effort on the parts of your application that are most complex and provide a real advantage if formalized and implemented using CQRS.
Upvotes: 2