NobbZ

NobbZ

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!

Showing Posts 1 to 1

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”.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews