Route one aggregate id's commands to one node on a cluster; failover applies no command twice #8
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Purpose (feature): on a cluster, all commands for one aggregate id run on one node; when that node is lost the id moves to a live node and no command is applied twice or lost. One node behaves exactly as today.
Refs martha-ai/mcl-martha#1 (A3 package 5 of the clustered mcl-martha path).
Raf, 2026-12-02: "Run the whole path (Recommended)" and "Three for failover (Recommended)".
Today
evoq_aggregate_registryuses pg (scopeevoq_pg), butlookup/1takes only a pid on this node (find_alive_local/1). A pid on another node is skipped.evoq_aggregate:called/2answers{error, noconnection}(or the reason it died with). The router reports that as a failure and does not retry it.evoq_idempotency) is ETS on the entry node. It cannot tell a second node that the dead node already appended a command.Design
evoq_nodesinevoq_pg. pg drops a member when its node goes down, so the group is the set of live evoq nodes.erlang:phash2({Node, AggregateId}). Losing a node moves only that node's ids. Adding one moves only the ids it now wins. One node: the owner is alwaysnode(), so the code path is today's (no remote call).get_or_start/3: owner is this node, so today's path. Otherwise it asks the owner's registry,{evoq_aggregate_registry, Owner}, and gets the owner's pid. The router calls that pid as it does now (gen_server calls cross nodes). A forwarded start is served on the receiving node without asking again, so a request makes at most one hop, even while two nodes see membership differently.noconnection,nodedown, a{nodedown, _}exit) is treated likenot_runningtoday: retried withinretry_attempts, samecommand_id, through the registry, which now picks a live owner.causation_id(= the command'scommand_id, already written in every event's metadata) of the last N events of its stream. N is one defined default with an app-env override. It reads them at load with one backward read, so a snapshot does not hide them, and adds each append's id. A command whosecommand_idis in that set is not executed again. It answers{ok, Version, []}, the sanctioned "already done" no-op. If the dead owner's append commits only after the new owner has loaded, the new owner's append meetswrong_expected_version. It reloads, so it now sees the id, and the retry is answered as already done.wrong_expected_versionand re-routes. This is the correctness floor today as well. Routing removes the steady-state double runner; it does not replace the store's check.Threat model (outside attacker only; insider and crew findings are notes)
evoq_aggregate_registryor an aggregate pid. Nothing new listens and nothing new leaves the cluster.phash2is unkeyed, so an attacker who picks ids could put them all on one node. That is no worse than today, where one node runs them all. Note only.evoq_decision_registry) has the same local-only lookup. It is not in this package.idempotency_keywith a freshcommand_idthrough another entry node still misses that node's ETS cache, as it would hit a cold cache after a restart today. The durable window covers the samecommand_id.Done-when
command_idappears once as acausation_idin the stream, none missing) (H1, H2).node(), the remote branch skipped, node loss not retried, the causation check removed.Design change while building (appended; the design above stays as first written):
Item 5, "not applied twice", is narrower than written. The aggregate does not remember command ids on every load, and it does not answer every repeated
command_idas done. evoq's ownevoq_idempotency_scope_testsshowed why: two different commands may share acommand_id(evoq#51's deposit/withdraw case), and the second must run.What ships instead:
owner_lostonce its owner node went away.owner_lost_lookback, default 100) and answers a command that the stream names as acausation_idwith{ok, Version, [], State}.PR: #9.
Landed on main at
9ee5943(PR #9, Jupiter PASS r=1, runs 6565-6568 green). Done-when:one_id_runs_on_one_node_test, three_nodes) (H3).a_halted_owner_…,a_stopped_owner_…) (H1, H2).{ok, 0, []}and appends nothing (an_owner_that_appended_and_died_…) (H1).every_node_names_the_same_owner_test, two_nodes). Losing a node moves only its ids (losing_a_node_moves_only_its_ids_test).standalone_owner_is_this_node_test_shows this node owns every id, so no remote call is made, anda_command_that_is_not_an_owner_loss_retry_runs_as_alwaysshows no extra read (H4).node(), remote branch skipped, node loss not retried,owner_lostnot marked, causation check removed.Jupiter's two notes (look-back window, shared
command_id) are in #10.