Where should user accounts live, and where should sessions and tokens go?
Applications like this: any app with sign-in. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.
| Dimension | This application |
|---|---|
| Writes | Low for accounts. High for sessions: every login, and often every request touches a session. |
| Reads | Every authenticated request reads a session or validates a token. |
| Read to write | Very high on sessions. |
| Consistency | Strict for accounts (a unique email). Loose for sessions: losing one means one more login. |
| Latency | Session lookups must take about a millisecond, since every request pays it. |
| Growth | Accounts grow slowly. Sessions churn and expire. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.
| Data | Where it goes | Why |
|---|---|---|
| Accounts, credentials, roles | PostgreSQL or MySQL | A unique constraint on email, and transactions for sign-up and role changes. |
| Sessions | Redis or Valkey, 30-minute TTL | Lookups by key in memory, and expiry is built in. Losing them costs a login, not data. |
| Refresh tokens | Redis or Valkey, 7-day TTL, rotated on use | A refresh token outlives the session it renews, so it gets its own, longer TTL. Losing it costs a login, not data. |
| Login attempts and rate limits | Redis counters with a TTL | A counter per user or per IP that expires on its own. |
| Audit of logins | An append-only table or log, partitioned by time | Kept for security reviews, rarely read. |
How each family of store behaves under traffic: Database families.
CREATE TABLE users (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL,
password_hash text NOT NULL, -- argon2 or bcrypt, never the password
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX ON users (lower(email));
# Redis
SET session:9f2c... '{"user_id":42}' EX 1800
SET refresh:9f2c... '...' EX 604800 -- 7 days, rotated on use
SET login_fail:42 0 EX 900 NX
INCR login_fail:42
Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.
| What happens | What you see | What I do |
|---|---|---|
| Sessions in the main database | A delete job for expired sessions causes bloat and I/O spikes | Move sessions to a store with built-in expiry. |
| Redis without a memory limit | Redis runs out of memory, or swaps | Set maxmemory and an eviction policy that removes keys with a TTL first (volatile-lru). If nothing has a TTL left, writes fail instead of evicting, so keep this Redis for sessions only. |
| Case-sensitive email lookups | Duplicate accounts for one person | Unique index on lower(email). |
Cost. Sessions are small. A modest Redis node holds millions of them. The waste here is usually a large database instance sized for session traffic that a cache would absorb.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational, plus managed Redis or Valkey |
| When it grows | The same |
For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.
The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.