Install
openclaw skills install bookforge-session-state-location-selectorRoute session state storage to the right location — Client Session State (cookies, JWT, hidden fields, URL parameters), Server Session State (in-memory or Redis session store), or Database Session State (SQL/NoSQL session table) — based on six dimensions: bandwidth cost, security sensitivity, clustering and failover needs, responsiveness, cancellation requirements, and development effort. Use when designing session management for a new web application, debugging sticky-session or node-pinning scaling problems, deciding between JWT vs server session vs database session, choosing a shared session store for a clustered or elastic deployment, handling shopping carts, multi-step forms, auth context, or edit-in-progress across HTTP requests, or auditing for session bloat or unsigned client session state. Applies to stateless session design, distributed session architecture, and HTTP session management in any language or framework. Relevant keywords: session state, session storage, session location, sticky sessions, JWT vs session, shared session store, stateless session, session management, HTTP session, distributed session, session cookie, server session, database session, Redis session, node-pinning, session scaling, client-side session, server-side session, session bloat.
openclaw skills install bookforge-session-state-location-selectorUse this skill when your web application must remember user state across HTTP requests — shopping carts, multi-step forms, auth context, edits-in-progress, wizard progress, or any data that belongs to a specific user interaction but is not yet committed to the database of record.
The skill routes each state concern to one of three storage locations: Client Session State (stored on the client and sent with each request), Server Session State (held in server memory or an external cache), or Database Session State (persisted as rows in a durable store). Different concerns within the same application can legitimately use different locations.
Typical triggers: scaling problems caused by sticky sessions, JWT vs server-session debate, shopping cart durability requirements, auth token design, migrating from a single server to a cluster.
NOT for: record data persistence (use data-source-pattern-selector), web presentation patterns (use web-presentation-pattern-selector), or transaction concurrency (use offline-concurrency-strategy-selector).
Prerequisites: none. Works with a description of the session data and deployment topology.
Before routing, gather the following. Ask if not provided.
Required:
Observable from codebase:
application.yml, settings.py, config/initializers/session_store.rb, web.config)JSESSIONID routing, ip_hash, sticky_cookie)HttpSession.setAttribute calls for size estimationhttpOnly, secure, sameSite, signed/encrypted flags)Defaults if unknown:
Sufficiency check: proceed when you know the state concerns, their sizes, and the deployment topology.
WHY: Session state is a compromise forced by inherently stateful business transactions. Some apparent session state can be eliminated entirely — pushed into client-initiated request parameters, derived from the database on each request, or encoded in the URL. Stateless server objects can be pooled: 10 objects can serve 100 concurrent users at 10% activity. Eliminating session state is always preferable to choosing its location.
For each state concern, ask:
If any concern can be made stateless: note it as "eliminate — stateless redesign" and remove it from further routing.
WHY: The three session state patterns make different trade-offs across six dimensions. Evaluating each concern on all six dimensions before routing prevents "default to Server SS" anti-patterns and surfaces the actual constraints that drive the decision.
For each remaining state concern, score it on:
| Dimension | Client SS | Server SS | Database SS |
|---|---|---|---|
| Bandwidth | Costly — full state sent per request | Free — session ID only | Free — session ID only |
| Security | Risky — client can inspect and tamper | Safe — server-side only | Safe — server-side only |
| Clustering / Failover | Excellent — fully stateless | Poor without external store | Good — shared DB |
| Responsiveness | Fast — no server lookup | Fast — in-memory | Slower — DB roundtrip |
| Cancellation | Easy — client stops submitting | Clear server-side object | Delete rows by session ID |
| Dev Effort | Low for tiny state; grows rapidly | Low for single server; high for cluster | High — schema design + cleanup |
Mark each dimension as a constraint (hard requirement), preference (nice to have), or not relevant for this concern.
WHY: Different state concerns have different dimension profiles. Auth tokens are small and security-sensitive but benefit from stateless serving. Shopping carts are medium-sized and need durability. Edit-in-progress may be large and complex. Routing each concern independently produces a better overall design than choosing one location for all session state.
Route to Client Session State when:
Route to Server Session State when:
Route to Database Session State when:
Mixing rule: assigning different concerns to different locations is not only allowed but common and recommended. Examples: session ID → Client SS (required), auth JWT → Client SS (signed cookie), shopping cart → Database SS (durability), UI preferences → Server SS (fast, low risk of loss).
WHY: Each location has a distinct failure mode that must be addressed in implementation. These are non-optional implementation constraints, not optional enhancements.
For Client Session State:
HttpOnly, Secure, and SameSite attributes on session cookies.For Server Session State on a cluster:
For Database Session State:
WHY: Session anti-patterns are common and have serious consequences — scaling failures (node-pinning), bandwidth or memory exhaustion (session bloat), and security vulnerabilities (unsigned client state). Catching them here prevents incidents.
Node-Pinning (sticky sessions as a scaling crutch):
ip_hash, sticky cookie, JSESSIONID routing) with no external session store.Session Bloat:
Unsigned/Unencrypted Client Session State:
encrypted_cookie_store, Django signed cookies, Express cookie-session with secret).Server Session State on a Cluster Without External Store:
WHY: A written record of the routing decisions and rationale enables team alignment, review, and future change without re-litigating the decision from scratch.
Write a session state design record (see Outputs).
## Session State Design: [System Name]
### Deployment Context
- Topology: [single server | fixed cluster | elastic]
- Framework / runtime: [e.g., Express + Node.js, Django, Spring Boot]
- Current session infrastructure: [e.g., HttpSession in-memory, Redis, none]
### Statelessness Opportunities
- [Concern A]: [Can be eliminated — re-derive from DB on each request]
- [Concern B]: [Must remain stateful — mid-transaction edit]
### Routing Decisions
| Concern | Location | Rationale | Size | Security Controls |
|---|---|---|---|---|
| Session ID | Client SS | Always client — just one token | ~32 bytes | HttpOnly, Secure, SameSite=Strict |
| Auth context (roles, user ID) | Client SS | Signed JWT in HttpOnly cookie — stateless, cluster-friendly | ~300 bytes | HMAC-signed + encrypted |
| Shopping cart | Database SS | Durability + cluster support; cancelable by deleting rows | Variable | Server-side only |
| Multi-step form progress | Server SS | Large object, short-lived, Redis external store | ~10KB | External Redis store |
### Implementation Notes
- [Concern]: [specific implementation sketch]
- Auth JWT: use short-lived access token (15 min) + HttpOnly refresh token cookie; validate on every request
- Shopping cart: `cart_items` table with `session_id` FK; cleanup daemon runs every 30 min deleting rows with last_activity > 2h
### Anti-Pattern Status
- Node-pinning: [NOT PRESENT | PRESENT — fix required: ...]
- Session bloat: [NOT PRESENT | PRESENT — fix required: ...]
- Unsigned Client SS: [NOT PRESENT | PRESENT — fix required: ...]
### Open Questions
- [Any unresolved decisions or constraints that need clarification]
1. Stateless by default, stateful only where the business transaction requires it. Server objects that hold no inter-request state can be pooled and freely load-balanced. Session state forces one-to-one affinity or external coordination. Start by questioning whether the state is genuinely necessary.
2. Client Session State scales perfectly but costs bandwidth and security diligence. Every byte stored on the client is sent on every request. Works well for a session ID or a signed JWT. Becomes a liability for shopping cart contents or form state measured in kilobytes. The security contract is firm: sign everything, encrypt anything sensitive, re-validate everything on return.
3. Server Session State is simple but requires an external store for any clustered deployment. In-memory Server Session State is the simplest programming model. It breaks the moment there is more than one server node. The fix (external Redis/Memcached store) is straightforward but must be done proactively — retrofitting it after a scaling incident is painful. Never accept sticky sessions as a substitute for a shared store.
4. Database Session State is the most durable and cluster-friendly but has real costs. Every request pays a database roundtrip. Schema design must carefully separate pending session rows from committed record data, or queries for record data will accidentally include in-progress session data. A background cleanup mechanism for abandoned sessions is not optional.
5. Mix patterns per concern, not per application. A session ID belongs in Client SS (always). Auth context likely belongs in Client SS (small, signed JWT). A shopping cart belongs in Database SS (durable, cancelable). Edit-in-progress on a large domain object belongs in Server SS (complex, short-lived). Forcing all session state into one location to avoid mixing is a false simplification.
6. The six dimensions are constraints, not preferences. Clustering/failover is not optional for elastic deployments. Security controls are not optional for sensitive data. Size limits are not optional for Client SS. Evaluate all six before routing — skipping a dimension is how node-pinning and session bloat enter production.
Scenario: Online retailer with variable traffic (3x on sale days). Three-node cluster behind a load balancer with auto-scaling.
Trigger: New feature: multi-item shopping cart that must survive page refresh and browser close. Auth is already in-place using a session cookie.
Process:
session_id column to cart_items table. Implement background cleanup for abandoned carts (TTL 48h).Output: Design record routing cart to Postgres cart_items table with session_id FK. Auth cookie stays Client SS. No node-pinning. Cleanup daemon specified.
Scenario: Line-of-business app for insurance agents. Single application server (no clustering). Agents edit complex policies across 6 wizard steps over 10–20 minutes. Policies have 80+ fields and nested objects.
Trigger: Current approach stores the entire partially-edited policy as a Java serialized object in HttpSession. Server runs out of memory under load.
Process:
session_id field) is preferable — survives server restart, eliminates memory pressure, makes partial edits queryable (admin can see in-progress edits). Client SS is ruled out (200KB far exceeds cookie limits).is_pending flag and session_id column to policy and policy_line tables. All "committed policies" queries add WHERE session_id IS NULL. Cleanup daemon deletes rows with session_id IS NOT NULL AND last_activity < NOW() - INTERVAL '4 hours'.Output: Design record recommending Database SS for policy editor state. Session-bloat anti-pattern resolved. Migration path from current HttpSession noted.
Scenario: React SPA with a Node.js/Express API backend. Team is debating JWT in localStorage vs JWT in HttpOnly cookie vs server-side session.
Trigger: Security review flagged localStorage JWT as vulnerable to XSS. Team wants guidance on the session state trade-off.
Process:
Output: Design record: access JWT in HttpOnly cookie (Client SS). Refresh token revocation list in Redis (Server SS with external store). No localStorage. Anti-pattern (localStorage JWT) flagged and resolved.
references/six-dimension-scorecard.md — Full scoring table with worked examples for all three patternsreferences/anti-pattern-detection-checklist.md — Node-pinning, session-bloat, unsigned Client SS: detection criteria and fixesreferences/pending-data-schema-patterns.md — Pending field, pending tables, and session ID column approaches for Database SSreferences/modern-session-store-map.md — Fowler's patterns mapped to Redis, Postgres, DynamoDB, JWT, and framework-native session storesThis skill is licensed under CC-BY-SA-4.0. Source: BookForge — Patterns of Enterprise Application Architecture by Martin Fowler, David Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, Randy Stafford.
Install related skills from ClawhHub:
clawhub install bookforge-web-presentation-pattern-selectorclawhub install bookforge-enterprise-architecture-pattern-stack-selectorOr install the full book set from GitHub: bookforge-skills