🇺🇸 Business Driven Concurrency Model

Designing a multi service architecture might give you some headaches when it comes to concurrency. In this post I will describe the most famous technical concurrency models and propose a different view on them, so we can design better our systems.

Follow bellow these concurrency models, their benefits and challenges.

Pessimist

This concurrency model proposes that no one can change an object while it is being held by a process, meaning that all subsequent transactions will wait on the line until they get to their job.

This approach in some cases is bad because you hold your data, cannot scale, and sometimes happens that a given process decides to go on holidays while holding the line for other processes, forcing someone else manually solving the issue by killing the transaction.

Optimistic

This model proposes that given two or more transactions on the same object, either the first one wins or the last one wins. There is a down side on this model that in any cases somebody will lose data.

There are two strategies for the optimistic concurrency model:

Business

All of the above concurrency models have its own challenges, but we need a model focused on the business that deals with both worlds: optimism and pessimism.

In order to you understand this proposal, let me first show you a transaction example:

1: Begin Transaction
2: Read Entity A
3: Read Entity B
4: Update Entity C with data from A and B
5: Commit Transaction

Looking at this example we see that once we read data, there is no way we can guarantee, in a scalable system, that the state of C will be fully consistent with A and B, because A and B might be in a total different state that doesn’t align with C at the end of the transaction.

The remaining question is: Does this inconsistency matters to the business ?

Most of the times we assume that if we put transactions all around, then everything will be consistent. This is given to the fact that as ORM’s were hugely introduced in our softwares, developers started saying:

Ah! that’s fine, I don’t need to worry about the database, the ORM will take care of it for me. Let me focus on the business!

Which is not absolutely true. In fact, we need to worry about the business, and if you are in a collaborative domain, where somebody can change the state of the entity that you are about to use, then you need to think in concurrency, which is part of your business and very much related to how your database works.

So, in order to have a business driven concurrency model, first rule:

But this is not enough, you still need to code in such way that you save your business from the issues that every choice might give to you.

And the way to address this issue you have to split your transaction in little pieces of transactions, doing one thing at a time with one piece of data at a time.

1: Receive a message (command/event)
1: Get a Domain Object
2: Change the state of the Domain Object
3: Commit your transaction (possibly sending domain events)

It is really important to not touch multiple domain objects, because every time you access another domain object within a single transaction, more probability to have concurrency issues is added. If you need to change more state across your domain objects, you might want to have a look at domain events.

In essence: Relationship between your domain objects, will give you concurrency issues. So, really think carefully on your service boundaries, because they will move apart unnecessary domain object relationships and by design, most of your challenges will be gone.

This approach save us in several cases, but in a higher level there is another case in which we need to think further: Sometimes the business requires that in a given process only one transaction wins, but not both. Here is an example:

In a given collaborative domain where a customer placed an order, and your business want to be proactive on shipping by having multiple shipping providers in case of delay. The following racing condition can happen:

1: User places an order
2: Request shipping to provider #1
4: Provider #1 is taking too long, request shipping to provider #2
5: Provider #2 answer right away that order is shipped
6: Request cancel shipping to provider #1
7: At the same time, provider #1 answers that the order is shipped

In this scenario you have a racing condition and both providers will delivery this order. There is no way you can run from this concurrency issue without finding a business reason and address it.

There are a few possible solutions that you might take:

Considering the list above, you can notice that we were talking about a technical racing condition between requests, and now that we tried to find a business reason, we started talking about: returning policy. Based on this, the domain expert is able to decide what rule to follow in such scenarios.

If I’d give you a suggestion, not everything is solved by a technical decision. Sometimes you need to find a business reason for the issue and address it as part of your business.

If you are in a single user system, you don’t need to worry about it, but must scenarios, this is not the case.

Finally, don’t underestimate concurrency on you software, because there’s business hidden on it.

Follow me on Twitter @_arleypadua.

This article is also available in Portuguese.

Software Engineer and passionate about distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store