// libSQL error reference

replica out of date: stale reads from a failing replica

Sync from the primary is failing, so db.sync() throws the underlying error and reads keep returning the last frames the replica applied.

错误
replica out of date
类别
Replicas and consistency
错误信息
replica out of date with the primary

它的意思

An embedded replica only advances when a sync succeeds. When db.sync() fails, the replica stops taking new frames and reads keep answering from the last state that was applied: stale reads that fail no check of their own. The failure itself surfaces through sync() as whatever the underlying client error is, for example a connection failure or SERVER_ERROR with an HTTP status when the token is rejected. Turso's docs describe the flow: reads run locally from the file, and writes are sent to the cloud primary and then reflected back to the replica, so a sync gap sits between your writes and what your reads return.

常见原因

  • The network path to the primary dropped, and every sync since has failed.
  • The auth token used for sync expired or was invalidated, so the sync request answers 401.
  • Syncs run rarely (no syncInterval and no manual calls), so reads lag behind the primary without any failure.

Find the cause

Run `db.sync()` and read the thrown error: a socket or DNS failure points at the network, `SERVER_ERROR` with 401 at the token, `SQLITE_BUSY` at a local writer holding the replica file. If sync succeeds but reads still look old, write a known value through the primary and read it back through the replica: the difference is the sync gap.

如何修复它

  1. Call sync() and read the error it throws; it names the real failure:

    await db.sync();  // throws the underlying connect, HTTP or auth error
  2. For 401-shaped failures, mint a fresh token and update the client config:

    turso db tokens create mydb
  3. When a decision needs current data, read that one query through the remote URL instead of the replica:

    const primary = createClient({ url: 'libsql://mydb-myorg.turso.io', authToken });
    const row = await primary.execute('SELECT ...');

Other ways to handle it

Reads that must always be current can go through the remote libsql:// URL; keep the replica for the fast local reads that tolerate a sync gap.

安全地复现它

Local and honest: run `turso dev --db-file scratch.db`, point an embedded replica at it (a `file:` URL with `syncUrl` set to the dev server), sync once, then kill the dev server and call `db.sync()` again; it fails with a connection error while reads keep working. Restart the dev server and sync again to watch the replica catch up.

问题

What happens to reads when an embedded replica sync fails?

They keep working locally and answer from the last frames that were applied, so nothing throws on read. The staleness is only visible by comparison, which is why the failing sync() call is the signal to watch.

Does @libsql/client sync automatically?

Yes when you configure syncInterval; otherwise the replica syncs when your code calls db.sync(). A long interval or a missing call means reads lag by design, and failures surface wherever sync runs.

相关错误

代码和消息文本来自 libSQL / Turso documentation。浏览完整的 libSQL error list 或者 搜索每个数据库错误.

// 错误更少,修复更快

Work on libSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for libSQL and the other databases you use.

本页文字和数据: CC BY 4.0,请注明出处 SQLly (sqlly.app)。 代码和 SQL 片段: MIT. 许可详情 引用的错误消息文本归其数据库厂商所有。