# 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 Risks Open questions

## 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

| Risk | Severity | Mitigation |
| --- | --- | --- |
| Reads flipped before old sessions are copied, users get logged out | High | Gate step 4 on the 14-day wait, not on a date |
| Expired rows pile up in `sessions` | Medium | Nightly delete where `expires_at < now()` |
| Extra write load on the primary during dual-write | Medium | Watch the chart below before going past 10% |

Primary writes/sec Today Dual-write 10% reads 100% reads

## Open questions

*   Do we keep "remember me" sessions at 14 days, or is that the moment to shorten them?
*   Who owns the nightly cleanup job, the API team or infra?
*   Is the 10% read cohort random, or internal accounts first?
