When many developers think of Command-Query Responsibility Segregation (CQRS), they envision separate datastores for reads and writes, linked together by some kind of event-based synchronization mechanism. The reality is that for many domains this is overkill, while for others it still doesn’t solve fundamental concurrency issues.
22. Regular logic starts to choke
begin transaction
var quantity = select Q from Inventory
where Id = @ProdId
if (quantity >= quantityRequested)
update Inventory set Q =
quantity – quantityRequested
where Id = @ProdId
commit transaction
29. Add snapshotting for compaction
ProductID Delta TimeStamp
5 +235 18:03:06 6-3-2015
begin transaction
select @q = sum(Delta),
TimeStamp <= 5.min.ago
from Inventory where Id = @ProdId
delete from Inventory where Id = @ProdId
and TimeStamp <= 5.min.ago
insert into Inventory @ProdId, @q, now()
commit transaction
Yes, yes, all the security guys are having a fit right now, but you will have firewalls and intrusion detection systems and the network level between them.
But we’ll get back to this on the topic of architecture.
For the love of all that is good and holy – stop worrying about layers.
Value objects – money, measurements (length, temperature), CC expiry date
Each software project is like a different custom meal you’re preparing.
Multiple users operating on the same data at the same time
Multiple users operating on the same data at the same time
How do we keep the quantity consistent when multiple users are doing this in parallel?
We need to lock the record even at the time of the query.
Multiple users operating on the same data at the same time
But if the business still tries to enforce high consistency – it won’t work.
The way Amazon emails you back afterwards saying – sorry, your order will be delayed.
So – do we really need to check inventory at the instant that somebody orders?
But once this happens, you can greatly simplify your architecture back down.
Come to our booth and get these access cards to get 2 days of video training from my Advanced Distributed Systems Design course.