Skip to content
Console

Our first production failover drill between Tainan and Tokyo

View as Markdown

From 08:10 to 08:39 on Dec 4, 2026 (Taiwan time, UTC+8, as are all times below), we ran our first cross-region failover drill in production between our Tainan (core-tnn1) and Tokyo (core-tyo1) core nodes. The notice posted beforehand is in our service announcement. Over roughly 30 minutes, we hard-killed the push service, cut one node’s cluster network, ran both a planned switchover and a hard kill on the primary database, stopped all core services and the message broker on Tainan, and sent three live drill announcements to confirm each site could push on its own without duplicates.

Automatic failover worked across every layer, and alerts on our Osaka monitoring node fired and cleared as expected. The hard database crash also uncovered a bug that never shows up during normal operation but slows down post-crash recovery: WAL-G retried against Cloudflare R2’s immutable Bucket Lock and stalled pg_rewind. We patched and verified the fix the same morning.

Over the past few months, we upgraded the core infrastructure behind DPIP (Disaster Prevention Information Platform) for cross-region redundancy (see Architecture and geo-redundancy):

  • Metadata database: PostgreSQL runs on both Tainan and Tokyo with asynchronous streaming replication over TLS 1.3 managed by Patroni. A five-node etcd quorum elects the primary, HAProxy on each node routes writes to the current primary, and WAL segments plus daily backups are archived with immutable locks on Cloudflare R2.
  • Message cluster: NATS runs across four nodes with JetStream enabled for push delivery and cross-site coordination.
  • Push service: notify-go, written in Go, runs active-standby using a NATS KV lease; the standby answers write requests with HTTP 307 pointing to the active leader. The delivery workers dpip-server-go and aws-server-go run active-active across both sites and rely on database leases and de-duplication so users never get duplicate pushes.

An architecture diagram only proves how a system is supposed to fail over. Staging does not carry dozens of live location reports per second, real cross-sea RTT, or the full notification pipeline and production alerting stack. We picked a weekday morning just after 08:00, posted an advance notice, and injected six stages of faults directly in production while recording everything in VictoriaMetrics and Grafana.

No test earthquake or weather warnings were sent. All end-to-end delivery checks stayed strictly on the announcement topic with three drill notices covering three states: normal dual-site operation, Tokyo delivering solo with Tainan stopped, and Tainan delivering solo with Tokyo’s delivery pipeline paused.

Push servicenotify-goDatabasePatroni / PGNATS &announcementsGrafanaalerts08:17Tokyo isolated; self-demoted08:16Killed Tainan; Tokyo took over in 30 s08:13Aligned to Tainan08:36Tainan stopped; Tokyo active08:39Failback to TainanWAL-G 409 stalled pg_rewind08:25Killed primary; Tokyo up in 69.4 sSwitched to Tokyo (5 min)12Tainan down; notice 2/3 (Tokyo)08:15Notice 1/3 (dual)08:39Notice 3/3 (Tainan)PG failover firingPG down / not streamingFailback & error alerts clearedTaiwan time08:1008:1508:2008:2508:3008:3508:4008:45Fault / alert firingDegraded / stalledPlanned actionRecovered108:35 Database Patroni / PG: Tainan streaming again208:38 Database Patroni / PG: Switched back to Tainan

Four lanes from 08:10 to 08:45 showing the drill steps and monitoring alerts. notify-go: planned align to Tainan at 08:13, hard kill on Tainan at 08:16 with Tokyo taking over in 30 s, Tokyo partitioned at 08:17 and self-demoting for Tainan to take over, Tainan stopped at 08:36 and Tokyo taking over, graceful failback to Tainan at 08:39. Patroni / PG: planned switchover to Tokyo at 08:19 for 5 minutes of cross-region traffic, hard kill on Tainan primary at 08:25 with Tokyo auto-promoted in 69.4 s, WAL-G 409 stalling pg_rewind from 08:27 until Tainan resumed streaming at 08:35, planned switchback to Tainan at 08:38. NATS and announcements: notice 1/3 at 08:15 (dual-site), Tainan services and NATS stopped 08:36–08:38 for notice 2/3 (Tokyo solo), notice 3/3 at 08:39 (Tainan solo). Grafana alerts: PostgreSQL failover fired 08:20–08:38, PostgreSQL unreachable and replica not streaming fired 08:29–08:36, and failback plus high error rate alerts cleared by 08:44.

Dec 4 failover drill and Grafana alerts timeline (08:10–08:45)

Here are the measured recovery times (RTO) across all six stages:

Stage Scenario Measured RTO Outcome
Stage 0 Baseline alignment, graceful notify-go switch, notice 1/3 ~6 s Pass
Stage 1 Hard kill (SIGKILL) of Tainan notify-go 30 s Pass
Stage 2 45 s network partition on Tokyo notify-go (anti-split-brain) 30 s Pass
Stage 3A Planned Patroni switchover to Tokyo, 5 min cross-region load 7 s (63 ms no-primary gap) Pass
Stage 3B Hard kill (SIGKILL) of Tainan primary and replica rejoin 69.4 s promote, 72 s app recovery Promoted OK; caught WAL-G bug (fixed)
Stage 4–5 Tainan services + NATS down, Tokyo solo push, failback, Tainan solo push 6 s DB, ~4 s push service Pass

Push service takeover and split-brain protection

Section titled “Push service takeover and split-brain protection”

At 08:15:55, we disabled container auto-restart and sent SIGKILL to notify-go on Tainan, simulating a sudden process crash without releasing its lease.

At 08:16:25, as soon as the lease expired, Tokyo acquired leadership and finished initializing its 1,485-topic in-memory cache within the same second, giving an exact 30-second RTO. When Tainan came back up, it rejoined cleanly as standby with zero database DDL lock contention.

There was one expected side effect during those 30 seconds. Until the lease expired, Tokyo still believed Tainan was leader and replied HTTP 307 pointing clients to the dead node. Clients following that redirect timed out with HTTP 504 (after about 3.2 seconds) until Tokyo took over and traffic returned to 200/202. If we want to trim errors during that window later, we can cache peer health on the standby before redirecting or shorten the lease.

Network partition and zero split-brain (Stage 2)

Section titled “Network partition and zero split-brain (Stage 2)”

A process crash is straightforward. A network partition is trickier: the isolated leader is still alive and answering HTTP traffic, but it cannot reach the NATS consensus layer. If it keeps serving writes while the other site takes over, you end up with split-brain.

At 08:17:23, we severed Tokyo notify-go from the cluster network for 45 seconds to test our anti-split-brain guard:

Tokyo core-tyo1Former leader (isolated)Tainan core-tnn1Former standbyIsolated until 45 sSelf-demoted (503)Renew retry windowCluster network cutNetwork restoredStandby (307)Waiting for lease TTLServing trafficPromoted to leader12 s zero-overlap buffers01020304050Network cutSelf-demotionWaiting / isolatedNormal service

Two lanes in seconds from 0 to 55 s. At 0 s (08:17:23) Tokyo notify-go loses cluster connectivity. At 18 s (08:17:41) Tokyo detects failed lease renewals, self-demotes, cancels in-flight requests and returns 503. Between 18 s and 30 s is a 12-second zero-overlap buffer with no active leader. At 30 s (08:17:53) the lease expires and Tainan promotes safely. At 45 s (08:18:08) Tokyo's network returns, and 5 seconds later at 50 s (08:18:13) it rejoins as standby.

Stage 2 anti-split-brain timing during Tokyo network partition (seconds)

Tokyo stepped down on its own at the 18-second mark (08:17:41), canceled in-flight calls immediately, and returned HTTP 503. Tainan waited until the lease expired 12 seconds later (at 30 seconds, 08:17:53) before taking over. Leadership never overlapped.

Checking sum(notify_ha_leader) in VictoriaMetrics at 5-second intervals from 08:05 to 08:50 confirmed it: across the full 45-minute window, the active leader count stayed strictly at 0 or 1 and never reached 2.

Cross-region database latency under live traffic (Stage 3A)

Section titled “Cross-region database latency under live traffic (Stage 3A)”

Next, we tested the database layer. At 08:18:53, a planned Patroni switchover moved the primary from Tainan to Tokyo in 7 seconds (08:19:00, timeline 70 to 71). On Tainan, HAProxy saw no available primary for only 63 ms.

We intentionally kept notify-go running in Tainan for 5 minutes against the Tokyo primary to measure cross-region latency under real production load. Histograms from VictoriaMetrics (notify_db_query_duration_seconds) show the exact local vs. cross-region shift:

Select p50: Local (Tainan → Tainan) 0.53 ms, Cross-region (Tainan → Tokyo) 38.50 msLocal (Tainan → Tainan)0.53 msCross-region (Tainan → Tokyo)38.50 msSelect p95: Local (Tainan → Tainan) 1.33 ms, Cross-region (Tainan → Tokyo) 67.20 msLocal (Tainan → Tainan)1.33 msCross-region (Tainan → Tokyo)67.20 msUpdate p50: Local (Tainan → Tainan) 0.63 ms, Cross-region (Tainan → Tokyo) 38.50 msLocal (Tainan → Tainan)0.63 msCross-region (Tainan → Tokyo)38.50 msUpdate p95: Local (Tainan → Tainan) 2.33 ms, Cross-region (Tainan → Tokyo) 65.90 msLocal (Tainan → Tainan)2.33 msCross-region (Tainan → Tokyo)65.90 msSelect p50Lower is betterSelect p95Lower is betterUpdate p50Lower is betterUpdate p95Lower is betterEach row has its own scale

Four paired comparisons between local Tainan reads/writes (08:13–08:15) and Stage 3A cross-region traffic to the Tokyo primary (08:20–08:25), in milliseconds (lower is better). Select median rises from 0.53 ms to 38.5 ms and p95 from 1.33 ms to 67.2 ms. Update median rises from 0.63 ms to 38.5 ms and p95 from 2.33 ms to 65.9 ms. The roughly 38 ms increase matches the round-trip time between Taiwan and Tokyo.

Database query latency from Tainan notify-go: local vs. Tokyo primary

Every query paid an extra ~38 ms (the Taiwan–Tokyo round trip), and production traffic handled it without trouble:

  • In those 5 minutes, notify-go processed 26,785 device location reports with 0 new database errors.
  • The write queue stayed between 1 and 5 items for almost the entire run (peaking briefly at 78 during the switchback to Tainan, with 0 dropped across the drill).
  • A 40-page background reconciliation sweep finished cross-region in 135.2 seconds with 0 commit failures.

Across the full 30-minute drill (08:10–08:40), both notify-go nodes handled 135,233 HTTP 202 location writes and 25,390 HTTP 307 standby redirects. Deliberate fail-closed HTTP 503 responses during kills and partitions totaled 1,830, about 1.1% of all requests.

The bug the drill caught: hard primary kill meets R2 Bucket Lock (Stage 3B)

Section titled “The bug the drill caught: hard primary kill meets R2 Bucket Lock (Stage 3B)”

If we had stopped at a planned switchover, the drill would have looked spotless. We wanted to test a worst-case primary crash instead.

At 08:25:33, after switching the primary back to Tainan (timeline 72), we disabled auto-restart and sent SIGKILL directly to PostgreSQL/Patroni on Tainan.

Failover itself worked smoothly. Once the etcd lock expired, Tokyo promoted itself to primary (timeline 73), and HAProxy marked it healthy at 08:26:42 (69.4 seconds). Three seconds later (08:26:45, 72 seconds total), Tainan’s notify-go resumed writes through HAProxy on its own without restarting or triggering a false push failover.

The real problem showed up when Tainan restarted to rejoin as a standby.

At 08:27:44, we started the Tainan Patroni container again. Because Tainan had crashed as the old primary, its timeline diverged from Tokyo’s timeline 73, so Patroni had to run pg_rewind before resuming streaming replication. Watching the logs, Tainan stayed stuck in pre-rewind archiving and did not reconnect to Tokyo until 08:35:20, nearly 8 minutes late.

The logs pointed straight to the root cause:

  1. Before running pg_rewind, PostgreSQL triggered archive_command, calling WAL-G to upload two local WAL segments that had not yet been marked archived.
  2. Both segments had already reached Cloudflare R2 before the crash. Because our R2 bucket enforces a 3-day immutable Bucket Lock against deletion or tampering, R2 rejected the overwrite with HTTP 409 Conflict.
  3. WAL-G’s S3 client treated 409 Conflict as retryable and ran 16 rounds of exponential backoff per segment, blocking recovery for more than 4 minutes on each file.

This combination never happens in steady state. Normally, a primary only archives brand-new WAL segments. It only collides when a primary crashes hard, replays its last few unacknowledged WAL files on restart, and hits an immutable R2 bucket.

That same morning, we rolled out a two-part fix on both Tainan and Tokyo with zero downtime and verified it live:

  • Skip identical WAL files before uploading: added WALG_PREVENT_WAL_OVERWRITE=true so WAL-G checks if the segment already exists on R2 and compares its content; if identical, it skips the overwrite and exits cleanly with 0.
  • Bound archive_command runtime: wrapped Patroni’s archive_command in timeout 30s so an object-storage hiccup can never stall pg_rewind or startup indefinitely.

Live verification on both nodes confirmed the fix: pushing an already-archived WAL segment skipped in 0.68 seconds (exit 0), and switching to a fresh WAL segment archived cleanly with zero failures. Once unblocked, pg_rewind itself finished in 2 minutes 18 seconds and resumed TLS 1.3 streaming replication with ~47 ms lag.

Stopping Tainan’s services and NATS, plus three live announcements (Stage 4–5)

Section titled “Stopping Tainan’s services and NATS, plus three live announcements (Stage 4–5)”

With the database cluster back to two healthy nodes, we ran the final two stages.

At 08:36:04, we stopped notify-go, dpip-server-go, aws-server-go, and nats-core on Tainan at once, simulating a complete outage of Tainan’s application and messaging stack:

  • NATS kept quorum: gnatsd_varz_routes in VictoriaMetrics showed routes on the remaining three nodes (Tokyo, Taipei, and Kaohsiung) drop cleanly from 12 to 8 at 08:36:20. With 3 of 4 nodes up, quorum held and all 6 JetStream stream and KV leaders that had lived on Tainan automatically moved to the surviving nodes.
  • Tokyo took the entire push pipeline: all 15 push producer leases shifted to Tokyo, and notify-go recorded zero lease update errors.
  • Notice 2/3 (Tokyo solo): with Tainan’s services and broker offline, Tokyo delivered the second announcement push and wrote the in-app card on its own.

At 08:38, we brought Tainan back online (NATS routes returned to 12 immediately), ran a planned switchback for PostgreSQL (6 seconds, timeline 74) and notify-go (~4 seconds), then paused Tokyo’s dpip-server-go and aws-server-go to send notice 3/3 from Tainan alone. Bringing Tokyo back produced zero duplicate pushes.

Notice State tested Sending node End-to-end latency Duplicates Outcome
Drill notice 1/3 Normal dual-site (pre-drill info) Tokyo core-tyo1 1.47 s 0 Delivered, card OK
Drill notice 2/3 Tainan core services & NATS stopped Tokyo core-tyo1 (solo) 1.59 s 0 Delivered, card OK
Drill notice 3/3 Failback complete, Tokyo workers paused Tainan core-tnn1 (solo) 1.36 s 0 Delivered, card OK

Verifying monitoring and alerts with the drill

Section titled “Verifying monitoring and alerts with the drill”

When dashboards stay green week after week, it is hard to be sure alerts will actually fire when something breaks. We pulled the Grafana alert state history and VictoriaMetrics series from our Osaka monitoring node (OSA-1) and matched them against the drill timeline.

The alerts split cleanly into two groups, matching how we designed them:

Alerts that entered Alerting and auto-resolved

Section titled “Alerts that entered Alerting and auto-resolved”

Structural changes and sustained faults entered the firing state and cleared automatically once service recovered:

Alert Fired Auto-resolved Drill event
PostgreSQL failover 08:20, 08:39 08:38, 08:49 Both primary switches caught
Gateway usage write failure 08:27 08:37 Stage 3B primary crash gap
PostgreSQL unreachable (Tainan) 08:29 08:36 Stage 3B crash and rejoin window
PostgreSQL replica not streaming 08:32 08:36 Directly caught WAL-G 409 blocking pg_rewind
Tainan Nginx high error rate 08:41 08:43–08:44 Stage 4 Tainan service outage

The most valuable catch was the pair of “PostgreSQL replica not streaming” and “PostgreSQL unreachable” alerts. Even if a primary crash happened overnight with nobody watching the terminal, monitoring would alert within minutes instead of leaving the cluster running silently without a streaming standby.

Short blips detected in Pending that cleared on their own

Section titled “Short blips detected in Pending that cleared on their own”

Other rules caught the injected faults immediately in the Pending state, then returned to Normal without paging once automatic failover finished:

  • Push service: standby offline, push service disruption (cleared after Tokyo took over in 30 seconds in Stage 1), and high 5xx ratio on Tokyo during Stage 2 self-demotion.
  • Short database transition: no PostgreSQL primary (cleared after Tokyo auto-promoted in 69.4 seconds in Stage 3B), plus transient database error warnings on notify-go and api-gateway-go.
  • Message cluster: NATS node down, incomplete NATS cluster routes, and under-replicated JetStream KV across all 5 KV buckets (replicas dropped from 3 to 2 while Tainan was down and recovered to 3 on restart).

External blackbox probes (probe_success in VictoriaMetrics) showed the same pattern, registering brief dips during the Stage 1, Stage 3B, and Stage 4 transitions and returning to healthy automatically.

Those 30 minutes verified active-standby push failover, split-brain protection, cross-region database traffic, NATS quorum, and Grafana alerting under live load. Just as importantly, running a real SIGKILL against the primary surfaced the WAL-G and R2 Bucket Lock 409 retry stall before a real incident did.

Thank you to the DPIP users who received the three morning notices and shared screenshots and arrival times on Discord. Your reports helped us verify that both pushes and in-app cards arrived cleanly on real devices with no duplicates or drops.