NobbZ

NobbZ

How to have commands constrained on both ends with commanded

I’m playing with the thought to implement some same game using event sourcing to learn more about it.

Therefore I looked into the documentation of commanded and watched a talk about it. From the documentation and the talk it got pretty clear to me how I should model things that are constrained on one end like a bank withdrawal.

But how could one model transactions that are constrained on both ends?

In the context of the game the first thing came to my mind was a player dropping an item into a chest. The player needs to have the item in his inventory and it needs to fit in the receiving chest.

Currently, the only way I see after reading through commandeds documentation was to do it like this:

  1. create a command that moves the item from the inventory to the chest.
  2. check if the inventory contains the item in the execute function
  3. create the event to move it into the chest
  4. create the command that puts the item in the chest from the process manager
  5. check if there is enough space in the chest and for this example it is not
  6. Fire an event that rolls back the transaction
  7. get that one back into the process manager

But is this atomic? Or could there be a race and the player picked already up another item and doesn’t have room left in his inventory anymore?

Are there possibilities I currently do not see because I have no experience in this field?

Is there a way to get my flow from above atomic? (or is it already?)

Thanks in advance!

Most Liked

slashdotdash

slashdotdash

Aggregate are used to enforce business invariants, but you also want to keep them as small as possible to allow concurrency of operations. Much like the trade-offs involved with deciding how to separate GenServer processes.

When you have inter-aggregate dependencies you have to use eventual consistency as you outlined, which can lead to race conditions. To alleviate this, a command will be validated up front by querying a read model to ensure that it is likely to succeed. In your example you would check the player’s inventory and receiving chest before attempting to drop the item. This limits failures to small race conditions which must be handled by executing a compensating command to undo the event that has already occured, but you’ve now determined is invalid - much like a reverse transaction in accounting. These operations are not atomic, but are possibly long-running sagas.

The reason for this approach is because limiting the size of aggregates and using eventual consistency allows infinite scaling (in theory at least) because you can distribute the aggregate process amongst any number of nodes and you are not relying on distributed transactions (which don’t scale). This is explained in more details in Pat Helland’s position papaer “Life beyond Distributed Transactions: an Apostate’s Opinion”.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement