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
意味
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
syncIntervaland 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.
修正方法
-
Call sync() and read the error it throws; it names the real failure:
await db.sync(); // throws the underlying connect, HTTP or auth error
-
For 401-shaped failures, mint a fresh token and update the client config:
turso db tokens create mydb
-
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.
関連エラー
SQLITE_BUSY: database is lockedembedded replica file lockedconnection reset / interruptedConnection reset or dropped401 Unauthorized401 Unauthorized: bad or missing auth token
コードとメッセージのテキストは、 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. ライセンスの詳細 引用したエラーメッセージの文言は、各データベースベンダーに帰属します。