Plan: move sessions from Redis to Postgres

We move login sessions out of Redis and into a sessions table in Postgres, so we can drop the Redis instance. The move runs behind the sessions_pg flag with a dual-write period. While both stores remain current, turning the flag off restores Redis reads. After Redis writes stop, rollback requires restoring and resynchronizing Redis; the flag alone is no longer enough.

Migration order

  1. Add the sessions table and its index on expires_at. No app changes yet.
  2. Ship dual-write: every new or refreshed session goes to Redis and Postgres. Reads stay on Redis.
  3. Wait 14 days, the max session lifetime, so every live session exists in both stores.
  4. Turn on sessions_pg for reads, 10% of users first, then everyone.
  5. Stop writing to Redis. Remove the Redis client and the instance a week later.

Do not flip reads before step 3 is done. Sessions created before dual-write only exist in Redis.

Risks

RiskSeverityMitigation
Reads flipped before old sessions are copied, users get logged outHighGate step 4 on the 14-day wait, not on a date
Expired rows pile up in sessionsMediumNightly delete where expires_at < now()
Extra write load on the primary during dual-writeMediumWatch the chart below before going past 10%
Primary writes/sec Today Dual-write 10% reads 100% reads
Primary writes/sec from the staging load test: today 400, dual-write 550, 10% reads 900, 100% reads 1,300. The red bar is over our 1,000 writes/sec budget, so step 4 needs a bigger primary or batched session refreshes before it goes past 10%.

Open questions