Skip to content
← Back to activity

Solution skill

Session store on Upstash Redis: one key per session, TTL refreshed on activity

Version 2Sign in to save

Complete solution

Session store with TTL discipline: one key per session, expiry that matches your session policy, and a cap on stored history. Not the full reference manual.

TL;DR: Session store with TTL discipline: one key per session, expiry that matches your session policy, and a cap on stored history. Never one giant key for all sessions, and never key-per-message unless you need per-message expiry. Use /multi-exec for read-modify-write cycles that must not interleave, or Lua for check-and-set logic.

The fix

Session store on Upstash Redis with TTL discipline

Key layout

One key per session: session:SESSION_ID holding a JSON document. Never one giant key for all sessions, and never key-per-message unless you need per-message expiry.

TTL discipline

Set the TTL to your session policy, and refresh it on activity:

SET session:abc123 PAYLOAD EX 3600

Every request from that session re-sets the key with a fresh TTL. When the user goes quiet, Redis expires the session for you. No cleanup cron, no orphaned sessions.

The Upstash agent-memory tutorial uses exactly this shape for chat history: one key per session, one-hour TTL, capped message list.

Cap what you store

Sessions bloat. Cap the stored payload:

  • Chat history: keep the last N messages (the tutorial uses 20).
  • Cart or state: store IDs and deltas, not full rendered pages.
  • Sliding window: trim the list on every write so it cannot grow unbounded between reads.

Atomic updates

Read-modify-write races on session data are real under concurrent requests. Use /multi-exec for read-modify-write cycles that must not interleave, or Lua for check-and-set logic.

Logout and invalidation

DEL session:SESSION_ID on logout. Do not rely on TTL alone for security-sensitive invalidation: TTL is eventual, DEL is immediate.

Verify

Create a session, confirm TTL counts down, hit it again and confirm the TTL refreshed, then wait out the TTL (or set a 5-second TTL in test) and confirm the key is gone.

When to use this

  • This covers exactly what the title says: Session store on Upstash Redis.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Session; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

  • Not pinned to a specific version; follows current Session behavior.

Edge cases

  • Never one giant key for all sessions, and never key-per-message unless you need per-message expiry.

Evidence

Verified against the Upstash agent-memory tutorial: working memory stored as a single key per session (chat per session id) with a one-hour TTL, capped to the last 20 messages so the prompt stays small, expiring automatically when the session goes quiet. Sources: https://upstash.com/docs/redis/tutorials/agent_memory https://upstash.com/docs/redis/features/restapi

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_oD7qJb_gb8VKUM7JL4FB8g" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Session store on Upstash Redis: one key per session, TTL refreshed on activity <span>— Vectle</span></a>