# Backup and restore

> What 10xGraph persists, how to back up the Postgres tables that hold threads and state, and how to restore or roll back a running deployment safely.

Source: https://10xgraph.com/docs/server/backup-and-restore
Last updated: 2026-11-25

## What is durable and what is not

10xGraph persists agent conversations and execution state across two storage layers, each serving a different purpose.

For how durable state supports restarts and multiple replicas, see [scaling and reliability](/docs/production/scaling).

| Layer | Contents | Durable | Back up |
| --- | --- | --- | --- |
| **Postgres** | Threads, state, messages, tool-execution ledger, schema version | Yes | **Yes** |
| **Redis** | Checkpointer hot cache of recent state, default TTL 24h. If it is also your API cache store: token revocations, rate-limit counters, run locks, idempotency state | Cache: no. Cache store: revocations are lost on a flush | Cache: no. Cache store: snapshot it if you need to keep revocations |
| **Vector store** (PgVectorStore, Qdrant) | Long-term memories | PgVectorStore: yes, in your Postgres (back it up like any table). Qdrant: in that system | Yes, with the database's or that system's own tooling |
| **Media store** | Uploaded files | Depends on backend | Yes, if local disk |

## The cache and the version guard

Redis is a read-through cache in front of Postgres, not a second source of truth. It holds the most recent state for each thread and expires entries after 24 hours by default. If Redis disappears, the next read refills it from Postgres: no data loss, only latency.

However, restoring Postgres from an older snapshot while Redis and workers are still running is unsafe. Each state row has a per-thread `version` that increases on every write, and writes are compare-and-swap: a write that expected a version the database no longer has is rejected with `StaleStateError` (HTTP 409 from the API). Reads of the latest state come from Postgres, and the checkpointer drops the cached entry when it hits a conflict. Even so, a run that started before the restore can fail, and cached entries can still reflect pre-restore state until they expire.

Flush the checkpointer cache before restoring. The checkpointer writes its cache keys under `state_cache:` and `generic_cache:` (with the `redis_key_prefix` option in front, if you set one). Delete only those, so nothing else in Redis is touched:

```bash
redis-cli -u "$REDIS_URL" --scan --pattern 'state_cache:*' | xargs -r redis-cli -u "$REDIS_URL" DEL
redis-cli -u "$REDIS_URL" --scan --pattern 'generic_cache:*' | xargs -r redis-cli -u "$REDIS_URL" DEL
```

If you set `redis_key_prefix`, put it in front of each pattern.

`FLUSHDB` also works, but it clears the whole Redis database selected by the URL. When the API server uses the same Redis as its cache store, that database also holds token revocations, rate-limit counters, run locks and idempotency state. A flush therefore un-revokes every revoked token and user (a revoked token is accepted again until it expires) and resets rate limits. Only run `FLUSHDB` on a database dedicated to the checkpointer cache. If you did flush a shared one, revoke the affected tokens and users again with `revoke_user` / `revoke_token`, or restore the cache store data from your own Redis snapshot.

## Tables and schema

10xGraph stores all data in five tables owned by `PgCheckpointer`. With the default `public` schema, they are:

| Table | Purpose | Key columns |
| --- | --- | --- |
| `threads` | Thread metadata and ownership | `thread_id` (PK), `user_id` (indexed) |
| `states` | Serialized graph state snapshots | `thread_id`, `version` (unique together) |
| `messages` | Conversation history | `thread_id` (indexed), `message_id` (PK) |
| `tool_executions` | Idempotency ledger | `(thread_id, tool_call_id)` (composite PK) |
| `schema_version` | Schema migration history | `version` (PK), `applied_at` |

The `version` column in `states` is the linchpin of optimistic concurrency control. It is a per-thread counter that increases on every state write. Any two concurrent writes to the same thread will have seen different versions as their expected baseline, so one will collide on the unique `(thread_id, version)` constraint and fail. The losing write is rejected with `StaleStateError`.

### Schema versioning

The schema version is stored in the `schema_version` table. If you pass a custom `schema=` to `PgCheckpointer`, the tables are schema-qualified (e.g., `myschema.threads`), and your backup must target that schema:

```bash
pg_dump "$DATABASE_URL" --format=custom \
  --schema myschema --exclude-schema public \
  --file=custom-schema.dump
```

Schema migrations apply automatically on the first startup of an upgraded server. Always back up before upgrading so you can roll back if a migration causes problems.

### User isolation

The `threads` table carries a `user_id` column (`states`, `messages` and `tool_executions` reference their thread through `thread_id`). By default, `PgCheckpointer` enforces user isolation: a request cannot read or delete another user's threads even if they know the `thread_id`.

If you have disabled user isolation (`enforce_user_isolation=False`), the `user_id` column still exists but is ignored for access control. Backup and restore procedures are unaffected.

---

## Back up

### Routine backups

Take full backups regularly using the custom format, which supports parallel restore and per-table restoration:

```bash
pg_dump "$DATABASE_URL" \
  --format=custom \
  --file="10xgraph-$(date +%Y%m%d-%H%M).dump"
```

If your database is shared with other applications, back up only the tables 10xGraph owns to avoid unnecessary data:

```bash
pg_dump "$DATABASE_URL" --format=custom \
  --table=threads \
  --table=states \
  --table=messages \
  --table=tool_executions \
  --table=schema_version \
  --file=10xgraph-tables-$(date +%Y%m%d-%H%M).dump
```

Always verify a backup is readable before you trust it:

```bash
pg_restore --list 10xgraph-tables.dump | head
```

### Before a release upgrade

Always back up before applying a schema migration. Migrations run automatically on the first startup of a new version:

```bash
# Full dump before upgrading
pg_dump "$DATABASE_URL" --format=custom > 10xgraph-pre-upgrade.dump

# Then upgrade your server
pip install --upgrade "10xgraph[pg_checkpoint]"
10xgraph dev --no-open
```

If something goes wrong during migration, you can restore from the pre-upgrade dump. See [Upgrade to 1.0](/docs/project/upgrade-to-1.0#database-migration).

---

## Restore

### Full restoration

Restoring production data under live traffic corrupts state. Ensure all workers are stopped before you restore.

Runs in flight hold the state version they read. If you restore Postgres to a point before that version was written, their next write fails the optimistic concurrency check, and cached entries may still reflect pre-restore state.

This is the safe restore procedure:

```bash
# 1. Stop all traffic. Scale workers to zero instead of draining gracefully.
kubectl scale deployment/my-agent --replicas=0

# 2. Clear the checkpointer cache so it cannot serve stale versions.
#    Do not use FLUSHDB on a Redis shared with the cache store: it also clears
#    token revocations and rate-limit counters.
redis-cli -u "$REDIS_URL" --scan --pattern 'state_cache:*' | xargs -r redis-cli -u "$REDIS_URL" DEL
redis-cli -u "$REDIS_URL" --scan --pattern 'generic_cache:*' | xargs -r redis-cli -u "$REDIS_URL" DEL

# 3. Restore the dump.
pg_restore --clean --if-exists --no-owner \
  --dbname "$DATABASE_URL" 10xgraph-tables.dump

# 4. Verify the schema version matches the code you are running.
psql "$DATABASE_URL" -c "SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;"

# 5. Bring up one worker and smoke-test an existing thread.
kubectl scale deployment/my-agent --replicas=1
# Test: invoke an existing thread and confirm it continues the conversation

# 6. Scale up once confident.
kubectl scale deployment/my-agent --replicas=3
```

The `--clean` flag drops tables before restoring, preventing primary key collisions if you restore over partial data. `--if-exists` suppresses errors if tables do not exist yet (safe idempotency).

### Restore a single thread

Full restores are often overkill. To recover a single conversation from a backup:

1. Restore the dump into a scratch database:

```bash
createdb 10xgraph_scratch
pg_restore --no-owner --dbname 10xgraph_scratch 10xgraph-tables.dump
```

2. Export the thread and its history to CSV:

```sql
-- From the scratch database
\copy (SELECT * FROM threads WHERE thread_id = 'thr_abc123') TO 'threads.csv' CSV HEADER
\copy (SELECT * FROM states WHERE thread_id = 'thr_abc123') TO 'states.csv' CSV HEADER
\copy (SELECT * FROM messages WHERE thread_id = 'thr_abc123') TO 'messages.csv' CSV HEADER
```

3. Import into production, **only if the thread is not currently running**:

```bash
psql "$DATABASE_URL" <<EOF
-- Import from the CSV files
\copy threads FROM 'threads.csv' CSV HEADER
\copy states FROM 'states.csv' CSV HEADER
\copy messages FROM 'messages.csv' CSV HEADER
EOF
```

Keep the `version` column intact. Editing or rewriting it defeats the concurrency guard and can cause future writes to fail mysteriously. If you must restore a specific version, use `UPDATE` and ensure the new version is higher than any version in production for that thread.

---

## Retention and data deletion

### Delete a user's data

Threads carry a `user_id` field indicating the owner. The `states`, `messages` and `tool_executions` tables reference `threads` with `ON DELETE CASCADE`, so deleting the thread rows removes everything else:

```sql
DELETE FROM threads WHERE user_id = 'user-123';
```

Then handle related data outside Postgres:

1. **Redis:** Deleting a thread through the checkpointer clears its cache entry. If you delete rows with SQL, remove the cached keys yourself (the `state_cache:` and `generic_cache:` keys), not the whole database.
2. **Vector store:** Delete the user's memories with `store.aforget_memory(...)` (or `POST /v1/store/memories/forget`), or with that system's own tooling for Qdrant.
3. **Media store:** Delete uploaded files from cloud storage or local disk.

These deletions are not transactional with Postgres, so implement a retry strategy for media deletion.

### State history pruning

By default, `PgCheckpointer` keeps the 20 most recent state snapshots per thread and deletes older ones on write. This is the `state_history_limit` parameter. Older snapshots are dropped to prevent unbounded table growth but kept long enough for typical retries and concurrency conflicts.

To change the limit (e.g., keep 50 states):

```python
checkpointer = PgCheckpointer(
    postgres_dsn="postgresql://...",
    redis_url="redis://...",
    state_history_limit=50
)
```

Large state_history_limit values trade storage for more history available during retries. The table will grow proportionally.

---

## Test your restore

Test at least once per quarter or before any major release.

1. Restore the latest backup into a scratch database (not production).
2. Point a staging server at the scratch database.
3. Invoke an existing thread and send a follow-up message.
4. Verify the response includes context from before the restore (the model references earlier messages).

If step 4 fails, your backup is incomplete. Usually, the `messages` table was excluded from a table-scoped dump.

---

## Related

- [Configuring checkpointing](/docs/guides/set-up-checkpointing)
- [Durability and concurrency](/docs/guides/durability-and-concurrency)
- [Deploy on Kubernetes](/docs/server/kubernetes)
- [Production checklist](/docs/server/production-checklist)
- [Environment variables](/docs/reference/api-cli/environment)

## Frequently asked questions

### What happens if I restore an old Postgres backup with Redis still running?

The Redis cache can hold state newer than the restored database. Postgres is the source of truth and a failed version check invalidates the cache entry, but clear the checkpointer cache keys before restoring so no stale entries survive.

### Can I restore a single thread without a full restore?

Yes. Restore the dump into a scratch database, then copy that thread's rows from the thread tables into production using SQL.

### How often should I test my restore process?

At least quarterly, or before any schema-changing release.
