# Can SQLite handle concurrent writes with one database per customer?

Two customers can write at the same time if each has a separate SQLite file, but two writes to the same customer’s file must take turns.

## Try writing to two SQLite files while one is locked

Open two sqlite3 shells in the same empty directory and leave Acme’s transaction open in the first shell while you place Birch’s order in the second.

### First shell

```sql
.open acme.sqlite
CREATE TABLE orders (id INTEGER PRIMARY KEY);
BEGIN IMMEDIATE;
INSERT INTO orders DEFAULT VALUES;
-- Leave the transaction open.
```

### Second shell

```sql
.open birch.sqlite
CREATE TABLE orders (id INTEGER PRIMARY KEY);
INSERT INTO orders DEFAULT VALUES;

.open acme.sqlite
.timeout 0
BEGIN IMMEDIATE;
-- Acme already has a writer. SQLite reports "database is locked".
```

## Why the second write to Acme waits

Birch’s order commits while Acme’s transaction is open. Run `COMMIT;` in the first shell, then retry `BEGIN IMMEDIATE;` in the second. It now holds Acme’s write lock. Finish with `ROLLBACK;` to release it.

| Where orders land | What limits them |
| --- | --- |
| Two orders in Acme’s file | One write transaction at a time, even for different rows. A busy timeout can let the second writer wait. WAL lets readers continue. |
| Thousands of customer files on one host | Locks are per file. Customers still share CPU, free disk space and disk I/O. |

Measure lock waits on the busiest file and disk latency on the host. Shorten long transactions. If many files saturate one host, spread customers across hosts and route each to its writer. [How SQLite handles concurrent transactions](https://sqlite.org/lang_transaction.html).

## How to restore, delete or migrate one customer’s database

- **Restore.** Restore Acme to a new path and inspect its orders. To roll back, account for newer orders, pause Acme’s writes, close its connections and revert to the checked version. Birch stays online. [Roll back one customer](/restore-one-customer-sqlite).
- **Delete.** Stop Acme’s traffic and jobs, close its connections and remove the live file. Retire replicas separately. Server and bucket backups follow retention, so include them in deletion deadlines. [Check backup retention](/run-your-litebackup-server-and-bucket).
- **Change the schema.** Try a nullable column on a few customer files first. Record the schema version in the same transaction. Keep the app working with both schemas during rollout and when opening an older restore.

## When to choose PostgreSQL for concurrent writes

If one file stays busy with short transactions and spare host capacity, PostgreSQL can run writes to different rows concurrently. Updates to the same row can still queue.

A monthly sales total across files needs a reporting copy or a query per file, and those reads can reflect different times. Shared tables simplify frequent cross-customer joins and atomic updates. [Compare SQLite and PostgreSQL](/sqlite-vs-postgres).

## Install

[Set up one database per customer](/sqlite-database-for-every-tenant-or-agent) and [choose which writes must survive disk loss](/sqlite-host-failure).
