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

  1. In-memory (MemoryStore) — simplest, zero dependencies. Sessions lost on restart. No horizontal scaling (sticky sessions required). No audit trail.

  2. 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.

  3. 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.

  4. 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::PostgresStore as the authoritative session backend

  • Redis 7 (Alpine) as an optional LRU cache layer (128 MB maxmemory, allkeys-lru eviction)

  • MemoryStore is banned project-wide

NOTE
Amended by ADR-026. The applicant portal (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.
NOTE
Amended by Amendment 2 (2026-10-01, #2518). canopy-web no longer uses 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)

Negative

  • Write path is slower than Redis-only (PostgreSQL round-trip on session create/update)

  • Two systems to operate (PostgreSQL + Redis) instead of one

  • Redis cache invalidation on session revocation requires explicit delete

Constraints

  • canopy-web: 8-hour TTL, SameSite=Lax (Keycloak OIDC redirect compatibility)

  • canopy-portal: 30-minute TTL, SameSite=Strict (no cross-site auth flow)

  • Both: HttpOnly, Secure=configurable via CANOPY_SESSION_SECURE

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, not Lax, on the worker session cookie. SessionManagerLayer::with_same_site(SameSite::Strict) in services/canopy-web/src/main.rs carries a LOAD-BEARING comment: Strict is the CSRF defense for the /v1/composition JSON 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_seconds defaulting to 28800) and HttpOnly match the Constraint.

  • The Redis session cache was never wired. PostgresStore::new (tower-sessions-sqlx-store) is the only session store canopy-web constructs; no CachingSessionStore or Redis tier in front of canopy-web’s PostgresStore exists (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 devstack redis service (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_URL on 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 as CANOPY_WEBSESSION_SECURE. The knob is WebConfig.session_secure (services/canopy-web/src/config.rs), defaulted true in config/canopy-web/default.yaml and overridden per ADR-012 as CANOPY_WEBSESSION_SECURE (configuration-reference.adoc#canopy-web); main.rs passes it to with_secure. No unprefixed CANOPY_SESSION_SECURE is read anywhere — the string survives only in the tracked mirror under design/canopy-web/uploads/; the live services/canopy-web/tests/session_test.rs no 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 OAuth state, 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, HttpOnly and SameSite=Strict (still LOAD-BEARING for /v1/composition, Amendment 1), a browser-session cookie whose real lifetime is the idle deadline. Under session_secure all 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.

  • MemoryStore stays 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.

Edit this page · default