ADR-009: PostgreSQL-Backed Session Storage
Context
BFF services (canopy-web, canopy-portal) require server-side session storage for Keycloak OIDC state, CSRF tokens, and user preferences. The choice of session backend affects reliability, scalability, compliance auditability, and operational complexity.
Options Considered
-
In-memory (MemoryStore) — simplest, zero dependencies. Sessions lost on restart. No horizontal scaling (sticky sessions required). No audit trail.
-
Redis-primary — fast, widely used for sessions. Requires Redis operational expertise. Data loss risk on eviction under memory pressure. No built-in durability guarantees without AOF/RDB persistence tuning. Not queryable for compliance audits.
-
PostgreSQL-primary with Redis cache — PostgreSQL as authoritative store, Redis as optional LRU read-through cache. Sessions survive restarts and Redis eviction. Queryable for compliance audits (IRS Pub 1075 access logging). Horizontally scalable (any replica reads from PostgreSQL). Redis provides sub-millisecond reads for active sessions.
-
Cookie-only (encrypted JWT) — no server state. Limited payload size. Cannot revoke sessions server-side. Logout requires token blocklist (re-introducing server state).
Decision
Option 3: PostgreSQL-primary with Redis LRU cache.
-
tower-sessions-sqlx-store::PostgresStoreas the authoritative session backend -
Redis 7 (Alpine) as an optional LRU cache layer (128 MB maxmemory, allkeys-lru eviction)
-
MemoryStore is banned project-wide
canopy-portal) session storage backend is narrowed to Redis-primary by the separate ADR-026 (its sessions are short-lived, server-side-revocable, opaque tokens for anonymous applicants — not worker sessions). The worker portal (canopy-web) and the PostgreSQL-primary mandate above are unchanged.
tower-sessions-sqlx-store: its worker sessions live in canopy-owned tables written one named statement at a time. PostgreSQL stays the only worker-session store.
Consequences
Positive
-
Sessions survive service restarts, Redis eviction, and Redis outages (graceful degradation to PostgreSQL-only)
-
Session data is queryable via SQL for compliance audits (who accessed what, when)
-
Horizontal scaling without sticky sessions — any service replica reads from PostgreSQL
-
Redis provides fast reads for the hot working set of active sessions
-
PostgreSQL is already a required dependency (no new infrastructure for session storage)
Amendment 1 (2026-09-17) — as built
Status unchanged (still Accepted). The Decision and Consequences above are the record of 2026-04-05; this note records where the worker portal (canopy-web) as built differs from the Constraints. In-document amendment, not an edit to the Decision.
-
SameSite=Strict, notLax, on the worker session cookie.SessionManagerLayer::with_same_site(SameSite::Strict)inservices/canopy-web/src/main.rscarries a LOAD-BEARING comment: Strict is the CSRF defense for the/v1/compositionJSON routes (Stage 3 MR2, #491), whose sub-router deliberately omits the form-submission CSRF layer the HTML BFF uses. Downgrading to Lax would remove that defense; Coding Conventions (Canopy) records the same dependency. The 8-hour sliding TTL (Expiry::OnInactivity,session_ttl_secondsdefaulting to 28800) andHttpOnlymatch the Constraint. -
The Redis session cache was never wired.
PostgresStore::new(tower-sessions-sqlx-store) is the only session store canopy-web constructs; noCachingSessionStoreor Redis tier in front of canopy-web’sPostgresStoreexists (the applicant portal’s Redis opaque-token store,services/canopy-portal/src/session.rs, is the ADR-026 carve-out). PostgreSQL is the sole worker-session store, so the "graceful degradation to PostgreSQL-only" and "fast reads for the hot working set" consequences describe an unbuilt tier. The devstackredisservice (redis:7-alpine;devstack/redis/redis.conf: 128 MB,allkeys-lru) backs the per-IP rate limiter instead (canopy_api::apply_rate_limit_redis,RATE_LIMIT_REDIS_URLon configuration-reference.adoc#baseline). The applicant portal’s Redis-primary sessions are the separate ADR-026 carve-out noted above (redis-sessions,noeviction). -
The Secure flag is
session_secure, set asCANOPY_WEBSESSION_SECURE. The knob isWebConfig.session_secure(services/canopy-web/src/config.rs), defaultedtrueinconfig/canopy-web/default.yamland overridden per ADR-012 asCANOPY_WEBSESSION_SECURE(configuration-reference.adoc#canopy-web);main.rspasses it towith_secure. No unprefixedCANOPY_SESSION_SECUREis read anywhere — the string survives only in the tracked mirror underdesign/canopy-web/uploads/; the liveservices/canopy-web/tests/session_test.rsno longer carries it (#2168).
Amendment 2 (2026-10-01) — a canopy-owned store (#2518)
Status unchanged (still Accepted). PostgreSQL stays the only worker-session store; what changes is the code that writes it. canopy-web no longer uses tower-sessions or tower-sessions-sqlx-store (plan web-session-store, #2224).
Why. That store wrote the whole session record back, unconditionally (INSERT … ON CONFLICT DO UPDATE), after every non-5xx response whose handle had ever been modified. A logout could therefore be undone by a concurrent request’s write-back, a slow request could restore a refresh token already spent, and every writer (the CSRF token, the Studio draft, /login on a signed-in browser) was a stale whole-record writer.
Decision. canopy-web owns three tables in its own database (migration 20270114000000_web_session_store.sql; canopy-web Data Model):
-
web_session: one row per live session, found by the SHA-256 of the cookie’s 256-bit random token (never the token itself), with a sliding idle deadline, a CSRF token minted with the row, a versioned worker grant and the Studio draft in their own columns; -
web_login_flow: one row per sign-in in flight, named by its own cookie, bound to the OAuthstate, taken once by the callback; -
web_session_end: why each session ended (logout,superseded,idle,operator,refresh_refused,refresh_rejected,refresh_lost), written by the same statement that deletes the row.
Every write is one named statement over its own columns (services/canopy-web/src/web_session/sql.rs): there is no upsert, no whole-record write, and nothing is written after the response. Only the callback inserts a session row, so a deleted session is final. A refresh commits its grant only over the grant version it read. Every statement runs under a 2 s bound; a store that cannot be read answers 503, never a sign-in.
Constraints, as built.
-
The session cookie is
canopy_session,HttpOnlyandSameSite=Strict(still LOAD-BEARING for/v1/composition, Amendment 1), a browser-session cookie whose real lifetime is the idle deadline. Undersession_secureall three of canopy-web’s cookies (canopy_session,canopy_login_flow,canopy_reauth) take the__Host-prefix. -
The idle deadline is
session_ttl_seconds(0 means 8 hours, floor 5 minutes), read at boot; a request slides it when less than the deadline less a minute is left. -
MemoryStorestays banned; there is no in-memory session store, in production or in tests.
Consequences. Logout is durable, provided acknowledged commits survive a failover (synchronous replication; Deployment Guide). The upgrade signs every worker out once, and leaves the old tower_sessions schema for the operator to drop.