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

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement