ryanrborn

ryanrborn

GenServer State Management in Elixir: A Production Order Book

A long-running GenServer holds orders, positions, strategy state, and operational flags for a system trading real capital. The post walks the actual state struct, the three categories of data inside it, the persistence model (write to the database before the broker, push heavy work to Oban, never persist candles), and the design principles that fell out of running it in production — including why mid-roll restarts refuse to resume rather than guess.

Most Liked

Vidar

Vidar

Considering all the books about state machines I do not think our various short comments will be able to capture the full extent of state machines, their differences, or the every precise terminology as used in their many applications.

My naive understanding is that Moore machines have outputs depending only on state, with the next state depending on current state and inputs. In Mealy machines the outputs both on current state and inputs, and so do the next state. Personally I thought they could be converted back and forth, and the practical reason to choose Moore was more about avoiding electrical glitches when used with hardware. According to Wikipedia though not all Mealy machines can be converted to Moore machines, but I leave that rabbit hole for someone else to drown in.

I’ve mostly used state machines with electronic sensors, buttons, joysticks etc as inputs and controlling motors, relays and UI as outputs. The values of these are variables where the inputs reflect their current state, while for the outputs reflect what state I want them to get to. They are not states in the finite state machine terms, but they are nevertheless stateful variables which together with the FSM state describe the systems overall current state.

I agree different terminology for these different kind of states would be clarifying, and that in this setting states of inputs and outputs would better referred to as just variables. Nevertheless those variables do state though. If I got a latching button that is either on or off then that is holding state, and usually referred to as such. I don’t think you can convince me inputs or outputs do not represent state.

derek-zhou

derek-zhou

strategy, market_data_source, account_manager — each is a map with a module key and additional context.

I suggest to flatten them into the parent struct, use tuples, or use new structs. A map is a bad choice because with a map I have to write the keys and I will make typos.

trading?, rolling?, portfolio_heat_warned, portfolio_heat_blocked — state machine flags that control what
the GenServer is allowed to do.

Boolean flags are bad choices to encode state machine states (sooner you will find some combinations are illegal). Ideally, they should use enum types but since Elixir does not have a true enum type I’d just use atoms.

mudasobwa

mudasobwa

Creator of Cure

Either we call if FSM, or we have flags, tertium non datur. State machine is not an object having state field. There should be a single source of truth and any flags spread the responsibility inevitably resulting in a diverged inconsistent state sooner or later.

FSM has transitions and the transition callback in the only place where the decision might have happened.

Where Next?

Popular in Blog Posts Top

tmartin8080
Sharing some articles I’ve written recently while working in Elixir. Some are how-to guides and others for reference. Will update this ...
New
blackode
https://medium.com/blackode This article comprises of two biggest changes of ExUnit. Arguments in the failure report Running mix tes...
New
Paradox
Testing is an important part of any modern piece of software. But writing tests can quickly become an exercise in frustration, with tons ...
New
AstonJ
I think <span class="hashtag-icon-placeholder"></span>liveview is going to be a big topic in the foreseeable future, so wondering whether...
New
brainlid
Phoenix LiveView v0.20.4 offers a hidden gem with its handy little JS.toggle_class/1 feature. This addition simplifies toggling classes f...
New
danschultzer
How to set up the Content Security Policy header with Phoenix LiveView and support inline styles and scripts with CSP nonces.
New
ragamuf
Does the world need another How to create a blog article? Maybe not. But then again, creating something out of nothing is what we love....
New
brainlid
This post asks if we can remove Alpine from the PETAL stack. Can we do everything we need with just LiveView? Also, let’s explore an area...
New
rlopzc
Use the new log handlers to plug Slack or any other provider into your logging system. https://rlopzc.com/posts/integrate-slack-into-the...
New
PragTob
Elixir is a great language, but some behavior can be unintuitive, confusing and in the worst case lead to bugs. So, I took a look at 10 E...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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

We're in Beta

About us Mission Statement