carlos / platform Public

spec: stateless replicas at production scale, rev 1 (#892)

Big copies, not many: one copy per box (11-01 §5.4 carried to
production), web and workers as several processes inside each copy,
and four slices: CARLOS_MEMORY_MIB now, then Plan 4 Task 4's fleet
sizes, a drain swap for copies too big to overlap, and a three-box
drill with a sizing guide, after Plan 4 merges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8bfc752bbac2cee625927d01fc56ad2ec614b37e parent 01d6ca7
1 file changed, +267 −0
  • docs/superpowers/specs/2026-11-11-stateless-replicas-production-scale-design.md +267 −0
diff --git a/docs/superpowers/specs/2026-11-11-stateless-replicas-production-scale-design.md b/docs/superpowers/specs/2026-11-11-stateless-replicas-production-scale-design.md
new file mode 100644
index 000000000..e87d8ee7c
--- /dev/null
+++ b/docs/superpowers/specs/2026-11-11-stateless-replicas-production-scale-design.md
@@ -0,0 +1,267 @@
+# Stateless replicas at production scale: big copies, not many
+
+Rev 1, 2026-11-11. Discussion #892, a follow-up to #511. Builds on the
+stateless replicas design (`docs/superpowers/specs/2026-11-01-stateless-replicas-design.md`,
+"the 11-01 design" below) and changes none of its decisions. Brainstorm:
+Paul asked for #892 to be taken to done on 2026-11-11 without a live
+session, so every call here is the author's, marked, and listed in §9 for
+Paul to confirm or overturn. Boundary agreed with the #511 Plan 4
+controller the same day (§7).
+
+## 1. Intent
+
+The 11-01 design runs an app as one copy per fleet box. Its first user's
+staging needs one instance per app, two copies and at most 2 GiB per copy,
+and v0.35.0 ships exactly that.
+
+The same user's production is a Rails estate that today runs on another
+platform as many small processes. Use neutral names (shop, example.com)
+in everything this design produces; the shape is:
+
+| App | Web processes | Background workers |
+|-----|---------------|--------------------|
+| A (largest) | 20, 1.5 GiB requested, 2 GiB limit, 5 threads, one process each | 10 at 2 GiB, plus 3 mailer workers at 1 GiB |
+| B | 16 at 1 GiB | 6 at 2 GiB, no limit |
+| C | 3 at 1 GiB | 1 at 400 MiB |
+| D | 3 at 1 GiB | 2 at 1 GiB |
+
+Its production fleet will be three boxes with 8 vCPU and 32 GiB each, one
+per availability zone. #892 asked for three things to make that fit:
+
+1. separate worker instances, or worker-only copies, because one worker
+ alone wants about 2 GiB;
+2. copy counts well above 2;
+3. a memory and CPU size per process type.
+
+This design answers all three without giving up "a copy means a box".
+
+## 2. The decision: scale a copy up, keep copies to boxes (author's call)
+
+The 11-01 design §5.4 already chose this for one box ("one copy (more Puma
+workers, more Sidekiq threads) uses the hardware", Paul, option 1 of two).
+At production scale it holds, and it is cheaper than every alternative:
+
+- **Copies stay at most one per box.** On a three-box fleet an app runs 3
+ copies, not 20. The anti-goal stands (11-01 §4): a copy count always
+ means "survives that many boxes minus one". Hub routing (Plan 3) spreads
+ across three upstreams; nothing about it changes with the count.
+- **A copy is several processes of each kind.** The 20 one-process web pods
+ become, per box, one web process in Puma's cluster mode with about 7
+ workers (`WEB_CONCURRENCY`) of 5 threads each: 21 workers across the
+ fleet. The 10 workers become a Sidekiq swarm, or several worker entries
+ in `processes.json` (up to `release.MaxWorkers`, 4), on every copy.
+ Preloaded Puma workers share their boot memory, which one process per pod
+ cannot. These are the app's own process managers, configured through its
+ environment; the platform adds nothing to run them (§4 S1 gives them
+ their size).
+- **Web and workers stay in one copy** (11-01 §5.5 unchanged: workers run
+ on every copy). With copies up to most of a box (§4 S2) a 2 GiB worker
+ no longer has to share 2 GiB with web.
+
+Rejected:
+
+- **Worker-only instances** (a second instance of the app that runs only
+ its workers). The 11-01 design §6.6 runs the release command once per
+ instance per build. Two instances of one app would run the migrations
+ twice per deploy, at the same time on different boxes; Rails then fails
+ one of them on its migration lock, and that instance's deploy stops.
+ Fixing it needs an order between instances (one runs release, the other
+ waits for its result), which is a new cross-instance protocol for no
+ gain the per-copy processes don't already give.
+- **Worker-only copies** (a copy runs a chosen subset of the processes).
+ It puts a web copy and a worker copy of one app on one box, which is the
+ packing the 11-01 design refused (§3, §4), and it makes the copy count
+ stop meaning boxes. It comes back, if ever, with autoscaling (11-01 §9).
+- **Many small copies per box.** The same anti-goal, plus a database pool,
+ a Rails boot and a hub upstream per copy.
+
+## 3. Does the estate fit? Worked numbers
+
+A 32 GiB box reports about 31.2 GiB (t4g.xlarge's 16 GiB reports 15.6).
+The fleet budget for one copy (Plan 4 Task 4, §4 S2) is the total less
+max(1 GiB, total/8): about 27.3 GiB.
+
+At the other platform's requests, each box would carry a third of every
+app:
+
+| App | Web per box | Workers per box | Per box |
+|-----|-------------|-----------------|---------|
+| A | 20 x 1.5 / 3 = 10.0 GiB | (10 x 2 + 3 x 1) / 3 = 7.7 GiB | 17.7 GiB |
+| B | 16 x 1 / 3 = 5.3 GiB | 6 x 2 / 3 = 4.0 GiB | 9.3 GiB |
+| C | 1.0 GiB | 0.1 GiB | 1.1 GiB |
+| D | 1.0 GiB | 0.7 GiB | 1.7 GiB |
+| All | | | 29.8 GiB |
+
+So at request level the estate does not fit three 32 GiB boxes, and there
+is no room left for a deploy that starts a new copy beside the old one.
+Two facts move it: requests are not resident memory (the same apps on
+staging measured 0.1 to 1.2 GiB resident per app), and Puma's preloaded
+workers share their boot memory. Whether it fits is a measurement, not
+something this design can settle. **Before cutover, the operator measures
+each app's resident memory per process at production load**, and sizes
+the fleet from that (§9 Q5). The docs give this arithmetic (§4 S4).
+
+What the design must guarantee is that a big copy can deploy at all: see
+§4 S3.
+
+## 4. What gets built, in order
+
+Four slices, each its own plan. S1 is independent of #511 Plan 4 and
+starts now. S2 to S4 touch Plan 4's code (sizes, swaps, the floor), so
+their plans are written after Plan 4 merges (stopping rule 4).
+
+### S1. Every process learns its size (now)
+
+Each rootfs process gets `CARLOS_MEMORY_MIB`: the memory limit, in MiB,
+that the instance runs at on this start. Web, workers and release see the
+same number, because they share it (rootfs spec: "All of an instance's
+processes share one memory and CPU limit"), and so do a `carlos run` and
+an interactive session. It is agent-owned like
+`CARLOS_HOST`: set on every launch, whatever the instance's files say.
+
+With it, one `processes.json` and one Puma config serve every size: for
+example `workers ENV.fetch("WEB_CONCURRENCY") { [ENV["CARLOS_MEMORY_MIB"].to_i / 1024, 1].max }`.
+A Sidekiq swarm's count and its per-process memory restart limit derive
+the same way. A size change takes effect at the next start, as sizes do
+today, and the variable follows it.
+
+CPU is not added: Puma and concurrent-ruby already count the CPUs the
+process can use.
+
+Author's call on the name and unit. MiB, not bytes, because every app
+divides it by a process size, and it matches the size table's own figures.
+
+### S2. Larger sizes on owned fleets (after Plan 4 merges)
+
+Plan 4 Task 4 as reviewed on `stateless-replicas-4` (its text and rulings
+R6, R11, R12; R17 deferred it from v0.35.0 and the Plan 4 controller
+handed it to #892 on 2026-11-11):
+
+- new named sizes, for copies on an owned fleet only;
+- a copy may use its box's memory less max(1 GiB, total/8);
+- existing sizes keep the quarter rule everywhere;
+- the new sizes need the replica floor on every destination and the
+ console;
+- an overlap reserves its memory before it starts, and a refusal leaves
+ the old build serving.
+
+One change to its table (author's call): add 4g, 8g, 16g and 32g between
+Task 4's 6g, 12g, 24g and 48g, so a 32 GiB box (27.3 GiB budget) is not
+left choosing between 12g and 24g for its largest app. CPU follows the
+same 200 percent per GiB. The rows above 48g stay as Task 4 wrote them.
+
+No check sums the sizes of every app's copies on one box. A size is a
+limit, not a reservation, today and here; the size door's reply already
+shows each box's committed total beside its memory, and the docs say to
+keep it under the budget (S4). Refusing over-commit is §9 Q4.
+
+### S3. A copy too big to overlap swaps by drain (after S2)
+
+Raised by the Plan 4 controller. Today a deploy starts the new build
+beside the old one on the same box, then moves traffic (the overlap swap),
+and 11-01 §6.4 does the same per copy, one box at a time. A copy sized to
+most of its box cannot have a second copy beside it, so with S2 alone
+every deploy of a big copy would stop at the memory check.
+
+The drain swap replaces the overlap for those copies:
+
+1. The box takes its copy out of the hub's set (its health answer stops
+ counting it as up) and waits, for a bounded time the plan sets, until
+ the hub has stopped sending and its requests in flight have finished.
+2. It stops the old copy: web, then the workers, proven gone as today.
+3. It starts the new build and, once web is healthy, puts the copy back in
+ the hub's set. Its workers start after web, as today.
+4. The next box's permit (Plan 4) follows only then.
+
+Release still runs once, before any box swaps (11-01 §5.3). While one box
+swaps, the other copies carry the traffic, so the docs say to size an app
+so that its copies less one carry its peak, as on any platform that rolls
+one machine at a time. Websockets on the swapping copy drop and reconnect
+(11-01 §5.6, accepted).
+
+Which swap a copy uses (author's call): the drain swap when the instance
+has copies above 1 and its size is a fleet size (S2); the overlap swap
+otherwise, exactly as today. That is fixed by the instance's settings, so
+a deploy never changes mode because of how full a box happens to be. A
+copy that has no other copy serving (copies 1, or the others down) is
+never drained: an overlap that does not fit is refused and the old build
+keeps serving, as Task 4 says, and the operator can ship `--swap hold`.
+
+S3 ships in the same release as S2 and behind the same floor, so a box
+that could be handed a fleet size always knows the drain swap.
+
+### S4. Three boxes, and the guide (after S3)
+
+- The 11-01 drill (§5.7) on three boxes: spread within 28 to 38 percent
+ per box, one box hard-stopped ("2 of 3", no failed reads among requests
+ not yet answered), its return, and a deploy under load with drain swaps:
+ release once, one box at a time, no failed reads. It runs in a #421
+ window booked with the release controller, as Plan 4's drill does.
+- A production sizing page in the docs: §2's process model (Puma cluster
+ mode, a Sidekiq swarm, worker entries), §3's arithmetic with the
+ measure-first rule, the budget, "copies less one carry the peak", one
+ app deployed at a time, and the hub as the one box that is not yet
+ redundant (11-01 §3). Through copy review.
+
+## 5. Per-process sizes (#892 item 3): inside the copy
+
+The copy's size covers all its processes. Bounds per process are the
+app's own process managers' (Puma's worker restarts, a Sidekiq swarm's
+memory limit), set from `CARLOS_MEMORY_MIB` (S1). That matches how the
+other platform's limit worked per pod, without a platform field per
+process type.
+
+A platform limit per process (each worker unit with its own memory limit,
+a share of the copy's) is not in this design. It would need a new policy
+field, a root-side change to the worker units, and a census review, for a
+case (one worker starving web inside a copy) the app's managers already
+cover. It comes back if the drill or production shows the need (§9 Q3).
+
+## 6. Constraints
+
+- 11-01 §7 applies whole: copy review with no em dashes, the census for any
+ change to the root binary, a provider-neutral core, no new per-box or
+ per-copy bucket polling, the release floor, dev first.
+- S1's variable is set by the agent from the size it already computes for
+ the start; it reads nothing new.
+- Neutral names in code, copy, tests and comments.
+- Region HA guards (Plan 4 D11) apply to S2 to S4: they touch the activator
+ and placement, so they read region HA spec §25 and §7.5 first and run the
+ grouproll and region HA boundary tests.
+
+## 7. Boundary with #511 (agreed 2026-11-11 with the Plan 4 controller)
+
+- #892 does not touch `stateless-replicas-4` or any file Plan 4 lists until
+ Plan 4 merges.
+- Plan 4 Task 4 is #892's S2, built after Plan 4 merges.
+- S1 is independent: Plan 4 does not touch the rootfs environment.
+- S3's drain swap changes Plan 4's swap code, so it is built after Plan 4
+ merges.
+
+## 8. Not in this design
+
+- Worker-only instances and worker-only copies (§2).
+- Copies above the fleet's box count, or two copies on one box.
+- Platform limits per process (§5).
+- Hub redundancy and autoscaling (11-01 §3 and §9 unchanged).
+
+## 9. For Paul
+
+Q1. Scale by size and keep one copy per box (§2), rather than worker
+instances or worker-only copies. Recommended; it is §5.4's choice carried
+to production.
+
+Q2. The drain swap for copies above 1 at fleet sizes (S3), chosen by
+settings rather than by how full the box is. Recommended.
+
+Q3. No platform limit per process for now (§5). Recommended; the
+alternative is a per-worker memory share at root, about one plan's work.
+
+Q4. Allow over-commit of sizes across apps on one box (S2), showing the
+committed total rather than refusing. Recommended, as it is today.
+
+Q5. The production fleet's size is the operator's to measure (§3): at
+request level the estate needs about 29.8 GiB per box against a 27.3 GiB
+budget. This is information for the deployment, not a platform change.
+
+Q6. The S2 table adds 4g, 8g, 16g and 32g to Task 4's tiers.