Skip to content
Toolkit

ADR-0159: Multi-region readiness

  • Status: Accepted
  • Date: 2026-09-09
  • Deciders: Repository owner

Promoted from the "Multi-Region Readiness ADR (P2-10)" section of docs/ARCHITECTURE.md, unchanged in substance. ARCHITECTURE.md now carries a pointer here.

Context

Single-region deployment on a VPS (systemd + Caddy). PostgreSQL 16 and Redis 7 are co-located with the API. All durable state is in PostgreSQL; Redis is a warm cache only (no durable state, loses on restart without harm).

Decision

Defer active multi-region until sustained traffic warrants it. Activate when any of the following is true:

  • p95 redirect latency (GET /l/{url}) exceeds 200 ms for a continent where more than 10 % of traffic originates
  • Database read IOPS above 70 % sustained on the primary
  • A regulatory requirement forces data residency outside the primary region

Upgrade path (in order)

  1. Cloudflare CDN / Workers — put the Caddy origin behind Cloudflare. Zero-code change. Caches static assets, absorbs DDoS, gives edge TLS termination. Enables geo-routing rules for the link redirect hot path with a Worker that rewrites requests to the nearest origin.

  2. PostgreSQL read replica — provision a streaming replica in a second region. Change DATABASE_URL to a PgBouncer-pooled read replica for all read-only queries (list handlers, public biolink, redirect lookup). Write queries continue to the primary. The repository layer already uses separate read/write paths via pgxpool; no handler changes required — wire a second pool in config and route via a ReadWriteDB wrapper.

  3. Redis cluster / global cache — replace the single Redis instance with Redis Cluster (or Upstash Global). No application code change required because all cache calls go through the Cacher interface in internal/services/cache.go.

  4. Stateless API replicas — the API binary is already stateless (JWT-based auth, no in-process session). Deploy additional replicas behind a regional load balancer and update DNS/Cloudflare to route by latency.

  5. Object storage — uploads already route through the S3-compatible StorageService (internal/services/storage.go). Enable S3 Cross-Region Replication to replicate user assets to a secondary region.

What does not need to change

  • Handler code — all I/O is interface-typed; swapping backends requires only config/DI changes.
  • Auth — JWTs are stateless; any replica can verify any token.
  • Background jobs — the job_queue persists in PostgreSQL; only the primary region runs workers to avoid double-execution.
  • Migrations — run manually from a single admin host; replicas follow via streaming replication.

Consequences

Nothing is built now; the gates above are checked against /metrics and the k6 baseline in docs/PERFORMANCE.md. Open questions to answer when activating:

  • Primary region selection for GDPR compliance (EU users → EU primary?).
  • Whether to use active-active or active-passive for the DB primary.
  • Conflict resolution strategy if active-active writes are ever introduced.

Update this record before activating any step above.

Alternatives considered

  • Active multi-region now — rejected because no gate above is met and every step costs operational surface without a traffic reason.

Source: docs/decisions/0159-multi-region-readiness.md

Library