Session store migration: Redis → Postgres
Generated by Claude Code · 2026-09-28 · Awaiting review
Background
Session data lives in a single Redis instance with persistence turned off. Two restarts last week each dropped about 40,000 logins. This plan moves sessions to the existing Postgres cluster and keeps Redis as a read cache.
Options
| Option | Durability | Read P95 | Effort |
|---|---|---|---|
| A: Enable Redis AOF | Per second | 0.8 ms | Small |
| B: Move to Postgres | Per transaction | 2.4 ms | Medium |
| C: B + Redis read cache | Per transaction | 0.9 ms | Medium |
Recommend C: the same durability as B, with read latency close to today.
Steps
- Create the
sessionstable with an index onuser_id - Dual-write: new sessions go to both Redis and Postgres
- Backfill existing sessions (about 1.2 million rows)
- Switch reads to Postgres; Redis becomes a cache
- Watch for 7 days, then drop the dependency on Redis persistence
Data flow
Backfill script
-- Backfill unexpired sessions only
INSERT INTO sessions (id, user_id, data, expires_at)
SELECT id, user_id, data, expires_at
FROM staging_sessions
WHERE expires_at > now()
ON CONFLICT (id) DO NOTHING;
Risks
Warning
If dual writes conflict during the backfill, Postgres wins. Freeze changes to the session schema before the backfill starts.
Rollback
Reads are controlled by the SESSION_READ_FROM_PG flag. Turn it off to fall back to Redis. No redeploy needed.