carlos / platform Public

design: stateless replicas rev 1 (several copies of one app on a fleet)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev a04aaaae4b6dd277b8258dbe965586cffc0f990b parent 65420a4
1 file changed, +308 −0
  • docs/superpowers/specs/2026-11-01-stateless-replicas-design.md +308 −0

CI cancelled — superseded by a newer push — run details

Intent

Attributed to this commit: 37.2 g CO₂e (4,547,577 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-stateless-replicas, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/docs/superpowers/specs/2026-11-01-stateless-replicas-design.md b/docs/superpowers/specs/2026-11-01-stateless-replicas-design.md
new file mode 100644
index 000000000..449917bce
--- /dev/null
+++ b/docs/superpowers/specs/2026-11-01-stateless-replicas-design.md
@@ -0,0 +1,308 @@
+# Stateless replicas: several copies of one app on a fleet
+
+Rev 1, 2026-11-01. Brainstormed with Paul in the session that closed rootfs
+apps Plan 6. Discussion: to be filed on amadan carlos/platform; related
+#366 (rootfs apps), #457 (managed fleets), region HA (spec
+2026-10-26-region-ha-without-claims-design.md).
+
+## 1. Intent
+
+Today every CARLOS instance runs on exactly one box. If that box dies, the
+app is down until it comes back or moves. And one app can only use what one
+copy on one box can do.
+
+This design lets an app that keeps no state on its box run as several
+copies, each on a different box of a fleet. The hub spreads requests across
+the copies, and when a box dies the other copies carry on. Paul wants two
+things from it, both his words: "if it dies, the whole app goes down" is the
+problem, and handling more traffic matters too. When a box dies, "ideally
+someone wouldn't notice".
+
+It is the prerequisite Paul set (2026-10-24, rootfs spec §13 Q1) for moving
+a Rails app off Kubernetes. It is also region HA's "part 2".
+
+### The first user
+
+A single-tenant deployment adds a fleet of two boxes in eu-west-1 (Ireland),
+in different availability zones. It runs its Rails apps on them side by
+side, as rootfs apps:
+
+- The apps keep their data outside the platform: MySQL or Postgres and
+ Redis in the same region, reached through the operator's egress allow
+ (#328).
+- Background jobs run in Sidekiq Enterprise. Its periodic jobs elect a
+ leader through Redis, so each fires once however many Sidekiq processes
+ run.
+- Migrations run in a release hook on Kubernetes today.
+- Some apps use Action Cable, with Redis as the broadcast adapter.
+
+Use neutral names in everything this design produces (shop, example.com):
+no tenant names in code, copy, tests or comments.
+
+## 2. Goals
+
+1. An app declared as keeping no local database can run as N copies, each
+ on a different box of its fleet (`--copies N`).
+2. The hub spreads requests across the copies that are up, and stops
+ sending to a copy within seconds of its box dying.
+3. Losing one fleet box loses no reads, and the app keeps serving from the
+ other copies with nobody touching anything.
+4. A deploy runs the release command once, then switches the copies one
+ box at a time, so one copy always serves.
+5. Every copy runs the app's declared workers.
+6. People can see how many copies are running and where.
+
+## 3. Non-goals for the first version
+
+Each of these is wanted later. None is part of this build.
+
+- **Hub redundancy.** Traffic to a fleet still enters through the hub (the
+ console's box). A dead hub makes every copy unreachable. Paul,
+ 2026-11-01: "hub redundancy is what we'll want, so for first version this
+ is fine". The first version survives losing a fleet box, not the hub.
+- **Autoscaling.** No load-based scaling, and nothing that starts or stops
+ machines. Nothing in the platform launches EC2 today. Paul finds
+ autoscaling up and down "highly appealing"; it comes after copies work.
+ `--copies` is the number autoscaling will later move between a minimum
+ and a maximum.
+- **Carloku's shared regions and platform boxes.** Copies run on fleets
+ only. Shared regions need plans, caps and billing to count copies, an
+ outside load balancer (region HA, mostly unbuilt), and rootfs is not
+ allowed on shared boxes.
+- **Several copies of one app on one box** ("packing"). See §5.4.
+- **Apps with a local SQLite database.** They stay single-writer with a
+ lease, as today.
+- **Moving websockets gently between copies.** A dropped websocket
+ reconnects (§5.6).
+- **More than one region per app.**
+
+## 4. Anti-goals: failures even if it technically works
+
+- **Two copies on one box.** It looks like high availability without being
+ it (Paul, 2026-10-24). A copy count must always mean "survives that many
+ boxes minus one".
+- **Two copies of an app that keeps a local database.** Each would get its
+ own SQLite file and they would quietly diverge.
+- **A release command running twice at once,** or a copy serving a new
+ build before its release command succeeded.
+- **A copy reported or routed as up when its box is down,** or a hub that
+ keeps sending a share of requests into a dead box.
+- **New per-box polling of the bucket.** S3 requests cost money (about $3
+ per app per month from box polling today). Ride existing reads and
+ reports; don't add a poll per copy.
+
+## 5. What Paul decided
+
+### 5.1 Where it runs first
+
+On a fleet, starting with the two-box fleet in §1. Rootfs apps already run
+on fleets, so that deployment needs no single-tenant setting for this. The
+console's managed-fleets machinery (1b: box choice, assignment, drain,
+relabel on box loss) is the base for placement.
+
+### 5.2 The number of copies is set per app
+
+- `carlos apps set shop --copies 2`. The default is 1, which is today's
+ behaviour, so nothing changes until an app opts in.
+- Never more copies than the fleet has boxes able to run them. A larger
+ number is refused when it is set.
+- When a box is lost, the app runs short ("1 of 2 copies running") until
+ the box is back or replaced. It never puts two copies on one box to make
+ up the number.
+- An app may go above 1 only if its serving release declares that it
+ keeps no local database (§6.1).
+
+### 5.3 Release runs once per deploy
+
+Like the Kubernetes release hook it replaces:
+- It runs once, on one box, from the new build, before any copy switches
+ to that build.
+- If it fails, the deploy stops and every copy keeps serving the old
+ build.
+- After it succeeds, the copies switch one box at a time, each with the
+ overlap swap an instance gets today. For a short time old and new code
+ serve side by side, which the app's migrations must already allow (they
+ do on Kubernetes).
+
+### 5.4 One copy per box, and a copy may grow to fit its box
+
+Today an instance gets at most `xlarge` (2 GiB), and a size above
+`standard` is refused when it is more than a quarter of the smallest box's
+memory. On a fleet the customer owns, a copy may be sized up to most of its
+box, so one copy (more Puma workers, more Sidekiq threads) uses the
+hardware. Paul chose this over packing several copies onto one box (option
+1 of two, 2026-11-01): one Rails boot, preloaded workers sharing memory, one
+database pool per box, and copies keep meaning boxes.
+
+### 5.5 Workers run on every copy
+
+Every copy runs the app's declared workers. Sidekiq Enterprise's leader
+election keeps periodic jobs firing once. The platform does not pick a
+worker box.
+
+### 5.6 Websockets drop and reconnect
+
+When a box dies or a copy switches during a deploy, its websockets drop.
+The Action Cable client reconnects and lands on another copy within
+seconds. Paul: "Seems fine." Broadcasts reach every copy through the app's
+Redis adapter; that is the app's concern, not the platform's.
+
+### 5.7 The drill that says it's done
+
+On the first user's two-box fleet (or a dev fleet built the same way),
+under steady traffic to a replicated Rails app:
+
+1. **Spread.** Both boxes serve close to half the requests, measured at the
+ hub and in each box's own request records.
+2. **A box dies.** Stop one box's machine outright, not a graceful drain.
+ Reads don't fail. The app shows "1 of 2 copies". Workers keep running on
+ the survivor. Websockets reconnect within seconds.
+3. **The box returns.** Traffic spreads evenly again with nobody touching
+ anything.
+4. **A deploy under load.**
+ - The release command runs exactly once.
+ - The copies switch one at a time with no failed requests.
+ - A failing release leaves every copy on the old build.
+
+## 6. How it works
+
+Decided above, or the author's calls (marked). The builder settles the rest
+(§8).
+
+### 6.1 Declaring "no local database"
+
+A release declares that the app keeps no state on its box. Author's call:
+it lives in the release manifest, set at ship time (for rootfs apps beside
+the processes file, for example `carlos ship --stateless`), so it travels
+with the build. The builder names the flag and field.
+
+A release that declares it:
+- gets no SQLite database, no `CARLOS_DB`, no placeholder file, no restore,
+ and no Litestream;
+- takes no serving lease.
+
+Binary apps that keep all their state in S3 with compare-and-swap use the
+same declaration (author's call). The drill covers Rails only.
+
+Every place that relies on the lease today needs an answer for these
+apps. The recon found these:
+- a wake's lease, restore and replicate (`internal/activator/activator.go`
+ doWake);
+- the rootfs replica-missing refusal (`rootfs_service.go`) and the
+ placeholder database (`rootfsdb.go`);
+- the release command and workers, which run under the lease and stop when
+ it is lost (`rootfsrelease.go`, `rootfsworkers.go`);
+- secret escrow and instance-key minting (Durability 3a, #477,
+ `errEscrowNoLease`);
+- fleet bindings, which the lease winner writes.
+
+Author's call: one-writer work that is not serving keeps a lease of its own,
+a *coordinator* lease per app instance with no Litestream behind it. These
+are the release command, escrow and key minting, and binding writes. One
+copy holds it at a time; serving never waits on it.
+
+### 6.2 One instance, several boxes
+
+Author's call: a replicated app keeps one instance record (one host), and
+its fleet assignment becomes a set of boxes instead of one. Today 1b writes
+one `FleetBox` per record. Placement:
+- picks N distinct, ready, non-draining boxes;
+- uses 1b's box checks (freshness, label floor, rootfs-ready);
+- moves a lost or draining box's copy to a spare box when the fleet has
+ one, or leaves the app running short.
+
+Only the boxes in the set run a copy. The start rule for labelled rows
+(`startrule.go`) does not apply to stateless rows, which have no local copy
+to set aside.
+
+### 6.3 The hub spreads requests and skips a dead copy
+
+Today the hub forwards a fleet app's host down one box's tunnel channel
+(`fleetChannelFor`, `internal/edge/edge.go`), and a dead box gives 503 with
+no failover. For a replicated app the hub:
+- holds the set of copies that are up;
+- sends each request to one of them, round-robin;
+- drops a copy from the set within seconds of its tunnel closing or its
+ health check failing, and adds it back once it passes again.
+
+The health check is the one a single instance already has: web answers
+`GET /` with a status below 500. The edge's multi-upstream routes
+(`internal/edge/prober.go`, `multiProxy`) already do probing, round-robin,
+ejection and retry for fixed addresses. They are the starting point;
+they are not wired to platform instances today.
+
+Retries (author's call, which Paul did not object to): a request is retried
+on another copy only when that is safe. That means GET and HEAD, or a
+request that never reached the app (the tunnel or dial failed first). Any
+other request caught on a dying box returns an error rather than risking
+running twice.
+
+### 6.4 Deploys
+
+1. The coordinator copy (§6.1) runs the release command from the new build
+ (§5.3).
+2. On success, each box switches its copy with the existing overlap swap,
+ one box at a time. The next box starts only once the previous copy is
+ healthy on the new build.
+3. On a failed release, nothing switches.
+
+Workers follow each copy's swap as they do today (Plan 3 Group W). A deploy
+with one copy left (the other box lost) switches that copy alone.
+
+### 6.5 Seeing it
+
+- `carlos instances list` shows one line per copy: its box, build, state
+ and health.
+- The app shows "2 of 2 copies running", or "1 of 2" while short. This is
+ copy; it goes through Paul's copy review.
+- Logs and metrics carry the box (`carlos logs` already tags rootfs lines
+ with the process), so copies can be told apart.
+- `carlos run` and interactive sessions open on one copy: a healthy one the
+ platform picks, or the one named with a flag (author's call).
+
+## 7. Constraints
+
+- **Copy:** every user-facing string goes through Paul's copy review before
+ it ships, with no em dashes.
+- **Census:** any change to the root binary needs the census
+ (`make census-card`, an independent reviewer, recorded on #280).
+- **Provider-neutral core:** nothing here may assume AWS outside an
+ adapter.
+- **Requests:** no new per-box or per-copy bucket polling (§4).
+- **Releases:** a release floor in the manner of 1b's label floor
+ (`FleetBoxLabelFloor`). Copies above 1 are refused until every box in
+ the fleet and the console run a release that understands them, so an
+ old box is never handed a copy it can't run.
+- **Order:** dev first, on a two-box dev fleet, then the first user's
+ fleet, which is a deployment its operator rolls.
+- **Coordination:** region HA (the session doing HA planning) and managed
+ fleets phase 2 both touch placement and health. Agree who owns the
+ shared pieces (box choice, health answers, forwarding) before the plan,
+ so they are built once.
+
+## 8. Left to the builder
+
+- Names: the flag, the manifest field, the record field for the box set,
+ and how the coordinator lease is keyed.
+- Where the hub's copy set lives, and how it learns a copy's health
+ (active probes over the tunnel, the boxes' existing status reports, or
+ both), within §4's request rule.
+- How fast "within seconds" is, as long as the drill passes. Pick concrete
+ probe intervals and fail and rise counts.
+- How the console marks "release done for build X" so the boxes switch
+ after it, and how a box that was down during a deploy catches up.
+- The size limits on fleets (§5.4): which tiers or rules, and the headroom
+ left for the box itself.
+- Whether `--copies` is refused or clamped when a box is detached, and
+ exactly what the short-running state looks like in the console (copy
+ review).
+- How the drill measures "close to half" (a sensible tolerance).
+
+## 9. Later phases (order is the author's suggestion)
+
+1. This design on fleets (the first user, then any fleet).
+2. Hub redundancy, so a dead hub is survivable.
+3. Autoscaling of copies between a minimum and a maximum across existing
+ boxes, then launching and retiring machines (with managed fleets phase 2
+ and region HA's G5).
+4. Carloku's shared regions, with plans, caps and billing counting copies.