Module 3 · Persistence & memory
Checkpointers: SQLite, Postgres, Redis
5 min read
So far, our graphs forgot everything at the end of each run. In production that is unacceptable: a conversation continues over several messages, a human approval can take hours, a server restarts. Persistence is what turns a graph into a reliable system. In LangGraph, it rests on one component: the checkpointer.
What a checkpointer does
A checkpointer saves a checkpoint — a complete snapshot of the graph's state — at every step of the run. Each checkpoint contains:
- the state values at that moment;
- the nodes to run next (
next); - metadata: step number, origin (
input,loop,update), writes performed; - the pending writes of tasks already completed in an unfinished step;
- any pending interrupts.
Checkpoints are grouped by thread — an execution thread identified by a thread_id you choose (next lesson). A checkpointer is plugged in at compile time:
graph = builder.compile(checkpointer=checkpointer)
config = {"configurable": {"thread_id": "conversation-42"}}
graph.invoke(inputs, config)
That simple addition unlocks every capability of the following modules: conversation memory, crash recovery, human interrupts, time-travel. Without a checkpointer, interrupt() cannot work: there would be nothing to resume.
Implementations
| Checkpointer | Package | Use |
|---|---|---|
InMemorySaver | langgraph (included) | Tests, prototypes. Everything is lost when the process stops |
SqliteSaver / AsyncSqliteSaver | langgraph-checkpoint-sqlite | Local development, single-machine tools, demos |
PostgresSaver / AsyncPostgresSaver | langgraph-checkpoint-postgres | Production: durable, concurrent, backed up |
RedisSaver / AsyncRedisSaver | langgraph-checkpoint-redis | High-load production, with native data expiration |
They all implement the same interface: your graph doesn't change a line when you switch from one to another. The Async… versions are used with ainvoke / astream, i.e. in any async server.
SQLite: local persistence
import sqlite3
from langgraph.checkpoint.sqlite import SqliteSaver
connection = sqlite3.connect("relay-demo.sqlite", check_same_thread=False)
graph = builder.compile(checkpointer=SqliteSaver(connection))
The examples/04_persistence.py file runs a small graph with this checkpointer. Run it twice: on the second run, the runs counter is 2. The state survived the process stopping.
SqliteSaver.from_conn_string("file.sqlite") is a handy alternative: a context manager that opens and closes the connection for you.
PostgreSQL: production
Relay's server uses Postgres as soon as a database URL is provided:
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
async with AsyncPostgresSaver.from_conn_string(url) as checkpointer:
await checkpointer.setup() # creates the tables on first start (idempotent)
graph = build_graph(checkpointer=checkpointer)
...
Three things to watch in production:
setup()creates and migrates the tables. Call it at startup (it's idempotent), or make it part of your deployment procedure if your application isn't allowed to create tables.- A connection pool. Every step of every run writes to the database. Under load, use a pool (
psycopg_pool.AsyncConnectionPool) shared by the whole application rather than a single connection. - Volume. Each step produces a checkpoint. A ten-step agent per message, a thousand conversations a day: the table grows fast. Plan a retention policy (see below).
Durability mode
Writing a checkpoint at every step costs latency. LangGraph offers three modes, set per run:
| Mode | When the state is written | Trade-off |
|---|---|---|
"async" (default) | While the next step runs | Good performance; a very small window of loss on a hard crash |
"sync" | Before moving to the next step | Maximum durability; slightly slower |
"exit" | Only at the end of the run | Fastest; no mid-run resumption possible |
await graph.ainvoke(inputs, config, durability="sync")
Relay uses "sync" in its server: an approved refund must never be lost between two steps. For batch processing, where you can simply rerun everything, "exit" is a reasonable choice.
What can be stored in the state
The checkpointer serializes the state. The default serializer handles common Python types, LangChain messages, dataclasses, Pydantic models and datetimes. It cannot (and should not) save a database connection, an HTTP client or a function — a reminder of lesson 1.3's rule: the state holds data, not objects.
If the state contains sensitive data (customer messages, personal information), you can encrypt checkpoints at rest with an encrypting serializer, for example EncryptedSerializer.from_pycryptodome_aes(), which reads its key from an environment variable.
Retention and deletion
Checkpoints are personal data as soon as they contain conversations: they are subject to your obligations (GDPR in Europe). Plan from the start:
- deleting a thread at a customer's request:
checkpointer.delete_thread(thread_id); - retention: delete threads inactive for N days, or keep only the last checkpoint of finished conversations (
prune(thread_ids, strategy="keep_latest")on checkpointers that offer it); - native expiration when the storage supports it (Redis, or the TTL configuration of checkpointers and stores that support it).
Key takeaways
- A checkpointer saves the complete state at every step, grouped by thread.
- Without a checkpointer: no conversation memory, no resumption, no
interrupt().InMemorySaverfor tests, SQLite locally, Postgres (or Redis) in production — same interface.durabilitysets the performance/safety trade-off:"sync"for critical operations.- The state is serialized: data only, possibly encrypted; plan retention and deletion.
Practice
- Run
examples/04_persistence.pythree times and open the SQLite file with a SQL client: how many rows per run? - Start a local PostgreSQL (with Docker, for example), then launch Relay's server with
RELAY_DATABASE_URL: check that a conversation survives a server restart. - Write a
forget_customer(customer_id)function that deletes all of a customer's threads. What information do you need to do it?