Hot and cold state: how PgCheckpointer stores threads

In shortThe internal mechanisms of PgCheckpointer's two-layer storage model, the write path, read path, and optimistic concurrency control for thread state.

  • 6 min read
  • 9 sections
  • Updated
  • v1.0.0
  • Markdown

PgCheckpointer keeps thread state in two layers: a durable, versioned layer in PostgreSQL and a hot cache in Redis with a TTL.

This page is about thread state (the checkpointer). For facts that last across threads, see Long-term memory. For how threads and checkpointers work from the outside, see Checkpointing and threads.

The two-layer model

Layer Purpose Lifetime Guarantees
Hot cache Fast reads, reduces Postgres load TTL (default 86400 seconds / 24 hours) Best-effort; failures are logged, never raised
Durable storage Persistent, versioned history Until thread is deleted Atomic, transactional, authoritative

PostgreSQL is the source of truth. Redis is expendable: if it crashes or the cache entry expires, the next read simply falls back to Postgres.

The write path: durable first, then cache

When the graph loop checkpoints state, it calls PgCheckpointer.aput_checkpoint() and then aput_state_cache(), two sequential operations:

1. Durable write to PostgreSQL

State and messages are written in a single database transaction that:

  1. Creates the thread row if absent and locks it (SELECT ... FOR UPDATE) to serialize writers, so concurrent workers on the same thread cannot collide. When isolation is active, it also rejects a thread owned by a different user.
  2. Performs an optimistic version check: reads the highest version in the states table and, if the run recorded a version when it read the state, raises StaleStateError if the two differ because another run committed in the meantime
  3. If the check passes, inserts a new row with version = current_version + 1 and the serialized state data
  4. Inserts any new messages into the messages table
  5. Prunes states older than the state_history_limit window (default 20 rows)
  6. Commits the transaction

If PostgreSQL fails before commit, the transaction is rolled back and no state is persisted. The run’s thread is left unchanged.

2. Cache refresh to Redis

After PostgreSQL commits, the graph loop writes the same state to Redis under the key state_cache:{thread_id}:{user_id} with the configured TTL. This cache write is atomic via a Lua script with a version guard: the script only writes if the incoming version is equal to or newer than the cached one, so a stale run cannot overwrite a newer cached state.

If the Redis write fails, the failure is logged and ignored. The durable state is safe in PostgreSQL, and the next read will fall back to Postgres and refresh the cache.

The read path: cache-aware with atomic fallback

At the start of a run, the graph calls PgCheckpointer.aget_state_cache() (falling back to aget_state()) to retrieve the thread’s latest state:

  1. Redis hit: Read state_cache:{thread_id}:{user_id} from Redis and compare its embedded version (__checkpoint_version__) with the thread’s current version in Postgres (one small MAX(version) read). If they match, restore it into config['_checkpoint_version'] and return the state; if not, drop the cache entry and read from Postgres.

  2. Redis miss or error: Execute a Postgres read:

    • Query SELECT version, state_data FROM states WHERE thread_id = ? ORDER BY version DESC, state_id DESC LIMIT 1, scoped to the owning user through the threads table when isolation is active
    • Deserialize the state and extract the version
    • Populate config['_checkpoint_version'] with this version
    • Return the state
  3. Thread not found in Postgres: Return None. The graph will initialize a fresh AgentState and use the incoming messages (if any) to populate it.

  4. Cache refreshed on miss: If the Postgres read succeeded, immediately write the result back to the Redis cache (with TTL) so the next call to this thread hits the cache.

A cache hit skips loading and decoding the full state from Postgres.

Versions and optimistic concurrency control

Every call to aput_checkpoint() or aput_state() performs a compare-and-swap:

  1. The run reads a thread’s state at the start and records its version in config['_checkpoint_version']
  2. The run executes its graph
  3. At each checkpoint, the write checks if that version still matches the thread’s highest version in Postgres
  4. If another run committed a new version in the meantime, the write raises StaleStateError instead of updating

The check runs inside the transaction, after the thread row is locked and before the insert. If the check fails:

  • The transaction is rolled back (no state is persisted)
  • The cached entry is invalidated (dropped from Redis) to prevent the thread from staying stuck on stale data
  • StaleStateError is raised to the caller

This prevents lost updates: each run’s writes are serialized by version number, and concurrent runs that started from the same version cannot both commit.

History pruning and bounded growth

PgCheckpointer keeps the newest state_history_limit snapshots per thread (default 20) and deletes older ones in the same statement as each insert.

This prevents the states table from growing indefinitely. The 20-row window is enough for debugging the recent execution history but not an audit log. If you need to preserve all history, export it before it is pruned, or increase state_history_limit (at the cost of more Postgres storage).

Messages are stored separately in the messages table and are not automatically pruned by version limits (only by explicit deletion or thread cleanup).

Database schema and table structure

The PgCheckpointer.asetup() method creates these tables (in the specified schema, default public):

Table Purpose
threads Thread metadata: thread_id, thread_name, user_id, created_at, updated_at, meta, plus any tenant and extra columns you declare
states Versioned state snapshots: thread_id, version, state_data (JSONB), created_at, updated_at, meta
messages Message history: message id, thread_id, role, content, tool_calls, tool_call_id, token usage and meta columns
tool_executions Tool call ledger for replay safety: thread_id, tool_call_id, result, created_at
schema_version Tracks migrations

The states table has a unique index on (thread_id, version) to enforce one row per version per thread. Writes lock the thread’s row in threads to serialize concurrent writers.

User isolation and scoping

By default, enforce_user_isolation=True. The states and messages tables carry no user_id; ownership lives on threads, so queries are scoped through it (thread_id IN (SELECT thread_id FROM threads WHERE user_id = ?)) using the user_id in the config dict. This prevents one user from reading or writing another user’s thread, even if they know the thread_id. When the API server supplies an identity in config["user"], the identity’s reach (own, tenant or all) decides which rows a query can match.

PgCheckpointer raises a ValueError on a read or write if user_id is missing from the config. The API server fills user_id from the authenticated request (or sets it to "anonymous" if no auth is configured). Turn off isolation only for single-tenant deployments where all threads are logically shared.

For several organizations or projects in one database, declare tenant_columns (such as org_id) and extra_columns on PgCheckpointer. They are columns on threads, set once when the thread is created and read only from config["user"]. The same scoping then runs through the thread row, so a caller’s reach can match on org_id and project_id as well as user_id. Threads created before you added a tenant column hold NULL there and are hidden from callers whose reach filters on it until you backfill them. See Multi-tenant data.

  • Durability and concurrency: For handling StaleStateError, thread isolation, and optimistic concurrency patterns, see Durability and concurrency.
  • Thread lifecycle: For the config dict, thread_id, and user_id, see Checkpointing and threads.
  • Multi-tenant data: Tenant and extra columns on threads, reach and filters, see Multi-tenant data.
  • Long-term memory: The checkpointer is thread-scoped. For memories shared across threads, see Long-term memory.
  • Replay-safe tools: How the tool ledger uses durable writes to prevent tools from running twice on resume, see Replay-safe tools.
  • Set up checkpointing: Choosing and configuring a checkpointer, see Set up checkpointing.

Frequently asked questions

What happens when the Redis cache expires or Redis restarts?
Nothing is lost. PostgreSQL holds the authoritative state, so the next read misses the cache, loads the latest version from PostgreSQL and writes it back to Redis. The cache TTL defaults to 86400 seconds (24 hours).
How much history does PgCheckpointer keep?
It keeps the 20 most recent state snapshots per thread by default and prunes older ones on every durable write. Change this with the state_history_limit argument. Messages are stored in their own table.
What happens when two runs write to the same thread at the same time?
PgCheckpointer uses optimistic concurrency control. Each write performs a version check. If another run committed a new version since this run started, the write fails with StaleStateError and the cache is invalidated to prevent the thread from staying stuck on stale data.
Last updated for v1.0.0Edit this page on GitHubReport an issue