# SQLITE_BUSY: database is locked: embedded replica file locked

> A local embedded replica file could not take or release its lock, usually because another process has the same replica file open for writing.

Source: https://sqlly.app/errors/libsql/embedded-replica-locked
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Error: `SQLITE_BUSY: database is locked`
- Name: `5`
- Category: Replicas and consistency

Error message:

```text
SQLITE_BUSY: database is locked
```

## What it means

An embedded replica is a local SQLite file that syncs from the remote database. Local reads are plain SQLite, and the sync loop writes the replicated frames into the same file, so local locking rules apply. The file allows one writer at a time. Two processes opening the same replica file, or a long local write while the replication loop applies frames, collide on the file lock and SQLite returns SQLITE_BUSY with the text `database is locked`. Turso's docs describe the layout: reads run locally from the file, and writes are sent to the cloud primary and then reflected back to the replica, so the local file is being written both by your process and by the replication loop.

## Common causes

- Two processes or app instances opened the same replica file, and both tried to write.
- A local write held the file lock while `sync()` tried to apply replicated frames.
- A crashed process left the file's lock bookkeeping in a state a new opener cannot take.
- A raw `BEGIN IMMEDIATE` in the replication-enabled libSQL build can return busy immediately instead of waiting on busy_timeout.

## How to fix it

1. Give each process its own replica file, or route writes through one process:

   ```sql
   // process A
   const dbA = createClient({ url: 'file:replica-a.db', authToken: token });
   // process B
   const dbB = createClient({ url: 'file:replica-b.db', authToken: token });
   ```
2. Set a busy timeout for the local file so SQLite waits instead of failing fast. In @libsql/client the `timeout` option covers `file:` URLs:

   ```sql
   const db = createClient({ url: 'file:replica.db', syncUrl, authToken, timeout: 5000 });
   ```
3. Keep local write transactions short and sync outside them:

   ```sql
   await db.sync();   // outside any open transaction
   ```
4. If a crashed process left the file wedged, check for stale locks by opening it read-only:

   ```sql
   sqlite3 replica.db 'PRAGMA integrity_check;'
   ```

## Reproduce it safely

```sql
Local and honest: create two clients on the same file URL and make both write in a loop; the second writer hits `LibsqlError: SQLITE_BUSY: database is locked`. Or with plain sqlite3: hold a transaction open in one shell and run `BEGIN IMMEDIATE` in another on the same file. No remote account needed; the cloud primary never sees the contention.
```

## How SQLly helps

SQLly opens the replica file as a plain SQLite database, which is exactly the second writer the error warns about: use it to inspect the file between syncs, and close the SQLly tab (or open the remote `libsql://` connection instead) while your app's writes are failing.

## Questions

### Do embedded replicas support writes?

Yes. Per Turso's docs, writes on an embedded replica are sent to the cloud primary and then reflected back to the local file; reads always run locally. That round trip still touches the local file, so local file locks matter.

### Can two processes share one embedded replica file?

Reads can coexist, but two writers on the same local file collide with SQLITE_BUSY. Give each process its own replica file, or route writes through the remote `libsql://` URL and keep the replicas read-only.

### How do I set a busy timeout for a file: database in @libsql/client?

Pass `timeout` in milliseconds to `createClient({ url: 'file:...', timeout: 5000 })`. The client applies it to every connection it opens, and it only takes effect for local SQLite databases; remote clients ignore it, and Turso Cloud does not support `PRAGMA busy_timeout` at all.

## Related errors

- [`SQLITE_BUSY`](https://sqlly.app/errors/libsql/busy): SQLITE_BUSY: the database is busy
- [`too many connections`](https://sqlly.app/errors/libsql/too-many-connections): Too many connections to one database
- [`connection reset / interrupted`](https://sqlly.app/errors/libsql/connection-interrupted): Connection reset or dropped

The code and message text are from the [libSQL / Turso documentation](https://docs.turso.tech/).
