# Restarting Every Container Under Traffic: Mieru's Deploy Drill

7 min read

I’m building Mieru, privacy-first web analytics for small sites: a 1 KB script, no cookies, one dashboard page. The backend runs on one server with Docker Compose. Caddy terminates TLS in front of two ingest replicas, ingest writes events to a Redis stream, a writer moves them into ClickHouse, and a worker and an API sit next to them. The code is public at github.com/AntonBurduzha/mieru.

Events lost during a deploy are easy to miss in an analytics product. Nothing errors, and the numbers just come out a little low. The architecture doc said a deploy loses nothing, and I wanted that measured. So task T15 added a drill: a bash script that restarts every app container while it sends traffic, then counts what came out the other end.

The deploy being tested

ops/deploy.sh restarts the app containers one at a time, each ready before the next:

  1. writer, then worker, so the stream’s reader is new before any new ingest writes to it;
  2. ingest-1, then a wait for its /health/ready plus 10 seconds;
  3. ingest-2;
  4. api.

Caddy, Redis, Postgres and ClickHouse keep running. The api has one replica, so the dashboard is unavailable for a few seconds while it restarts. Ingest has two, so one can take events while the other restarts.

The drill runs from my Mac with ops/drills/deploy-under-traffic.sh. Against the server it runs the real deploy.sh over SSH. With --local it targets the production Compose stack rehearsed on my Mac, with Caddy’s local certificates, and replays the same restart order without git or migrations.

What “under traffic” means

Each event is one curl to ingest’s public URL, as a page view with its own path:

/drill/<run>/<n>

<run> is a UTC timestamp plus the drill’s PID, and <n> counts up from 1. The events go to a site of their own, deploy-drill.invalid (.invalid is a reserved domain), so they never mix with a real site’s data. Each request carries a desktop Chrome user agent, because ingest drops curl’s own user agent as a bot.

The sender starts one background curl every 1/rate seconds. The default rate is 20 a second, and in practice it gets about 17, since every event is its own process. Each curl appends one line to sent.log:

<seq> <second it started> <status> <curl exit code> <x-mieru-dropped>

The traffic starts 5 seconds before the deploy and keeps going 5 seconds after it returns. Since every line has the second it started, and the deploy’s own log uses the same clock, the report can say how many events went out during the deploy.

Before any of that, a preflight finds or creates the drill site, and sends a probe event every 3 seconds until ingest accepts one. That proves the URL, TLS, the bot filter and the site all work before the real run starts.

How events are counted

After the traffic stops, the drill polls ClickHouse every 2 seconds, for up to 90 seconds, until there is a row for every event ingest accepted:

ops/drills/deploy-under-traffic.sh
count_stored() {
ch --param_site="$SITE" --param_prefix="$PREFIX" <<'SQL'
SELECT count(), uniqExact(pathname)
FROM mieru.events
WHERE site_id = {site:UInt32} AND startsWith(pathname, {prefix:String})
SQL
}

A run passes only when four numbers match: events sent, events answered 202 with no X-Mieru-Dropped header, count() and uniqExact(pathname). If count() is lower, events were lost. Since every event has its own path, if uniqExact(pathname) is lower than count(), something was stored twice. On a mismatch, the drill lists the first 20 missing and duplicated paths, plus the stream’s XLEN and pending count.

Exit code 0 is a pass and 1 is a loss. Exit 2 means the drill couldn’t run at all: bad arguments, a missing tool, SSH, the probe, a failed deploy, or a ClickHouse it couldn’t read. I didn’t want a broken drill to look like a lost event.

The passing local run

On 2026-11-30 I ran it against the rehearsed stack on my Mac. One of the passing runs:

events sent 748, 595 of them during the deploy (34s)
answered 202 748
stored 748 rows, 748 distinct paths
PASS: all 748 events were answered 202 and stored exactly once

Three things in the design kept those 595 events safe while their containers restarted.

The writer restart. While the writer is down, events wait in the Redis stream. On SIGTERM the writer finishes and acknowledges its current batch, and the new writer reads whatever is still pending first. It acknowledges a batch only after the insert into ClickHouse, and the insert carries a dedup token built from the batch’s stream ids:

apps/backend/src/mieru/writer/inserter.py
async def insert_rows(client: AsyncClient, rows: Sequence[EventRow], *, token: str) -> None:
"""Insert the rows as one block. ClickHouse skips a block whose token it already stored."""
data = [[getattr(row, column) for column in COLUMNS] for row in rows]
await client.insert(
TABLE, data, column_names=COLUMNS, settings={"insert_deduplication_token": token}
)

So a batch retried after a crash isn’t stored twice.

Caddy’s retries. Caddy health-checks both ingest replicas and retries a request that can’t connect on the other one:

ops/caddy/Caddyfile
# comments and shared settings left out
reverse_proxy ingest-1:8001 ingest-2:8001 {
lb_policy round_robin
lb_try_duration 5s
lb_try_interval 100ms
fail_duration 5s
health_uri /health
health_interval 2s
health_timeout 1s
}

The pause between replicas. The 10 seconds after ingest-1 comes back is longer than fail_duration (5 s) plus one health interval (2 s), so Caddy has ingest-1 back in rotation before ingest-2 goes down.

Breaking it on purpose

I had never seen the check fail, so a pass by itself didn’t prove much. With --local, the DRILL_DEPLOY_CMD variable replaces the rolling restart with any command, and the spec’s acceptance criteria used it to break the deploy twice:

Terminal window
p='docker compose -f compose.prod.yaml --env-file ops/rehearsal.env'
DRILL_DEPLOY_CMD="$p stop ingest-1 ingest-2 && sleep 8 && $p start ingest-1 ingest-2" \
ops/drills/deploy-under-traffic.sh --local
DRILL_DEPLOY_CMD="$p stop writer && sleep 5 && $p exec -T redis redis-cli XTRIM events MAXLEN 0 && $p start writer" \
ops/drills/deploy-under-traffic.sh --local

Both ingest replicas stopped for 8 seconds. 334 sent, 216 answered 202, 118 failed: 116 with 503 and 2 with 502. Exit 1. With no replica up, Caddy keeps retrying for its 5 seconds, then gives up with a 503.

The writer stopped and the stream trimmed. XTRIM events MAXLEN 0 empties the Redis stream while nothing is reading it. 270 sent, and all 270 answered 202. Only 178 reached ClickHouse, so 92 were missing. Exit 1, with the missing paths listed.

The second run is the reason the drill counts rows. Every request got a success response, and a third of the events still disappeared. A drill that only checked status codes would have passed it.

Then the server

The local runs proved the drill works. The criterion that mattered was a redeploy of production under 20 events a second, which I ran myself from the Mac.

The first server run failed with exit 1, though the deploy itself lost nothing. 903 sent, 0 failed, and every one of the 798 accepted events was stored once. The other 105 got a 202 with X-Mieru-Dropped: unknown_site. The run had just created the drill site, and each ingest replica reloads its sites every 30 seconds on its own clock. The probe reached the replica that already knew the site, and round robin sent every other event to the one that didn’t yet.

So the preflight was wrong. The fix reads the site’s age from Postgres and, while it’s younger than 35 seconds, waits out the rest before sending anything. The second server run, after the fix:

events sent 904, 745 of them during the deploy (42s)
answered 202 904
failed 0
dropped 0
stored 904 rows, 904 distinct paths
PASS: all 904 events were answered 202 and stored exactly once

What it doesn’t prove

It’s a correctness drill and makes no claim about load. One client sends at most 20 events a second, about 17 in practice, from one address. Load tests with k6 are planned after the MVP.

One gap is still open. Caddy retries a POST only when the connection fails. A request sent on a keep-alive connection that a stopping replica has just closed can still fail, and one run at this rate may not hit that window. Fixing it means touching the Caddyfile or ingest, which was outside T15’s allowed files.

The script is ops/drills/deploy-under-traffic.sh, and the whole story, both server runs included, is in the T15 spec.


More Posts