MCPcopy Create free account
hub / github.com/danthegoodman1/gRPSQLite

github.com/danthegoodman1/gRPSQLite @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
254 symbols 409 edges 20 files 6 documented · 2% ● updated 34d ago★ 1226 open issues

Browse by type

⨍ Functions 179◇ Types & classes 75
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

gRPSQLite

A SQLite VFS that stores databases behind a small gRPC service, so any datastore can hold SQLite databases. Stock SQLite runs on it unchanged, each transaction reaches the server as one atomic commit, and read-only replicas read consistent versions.

Give every user, tenant or agent its own SQLite database, kept in FoundationDB, Postgres, an object store or whatever you already run. Your server stores pages and leases; the VFS handles SQLite's locking, journaling and caching.

[!WARNING] This is early software. A deterministic simulator checks the guarantees below, but gRPSQLite has not run in production.

Contents

What sets it apart

  • Your storage, stock SQLite. The server is a plain gRPC service in any language, over any store. SQLite needs no patches: link the VFS in, or load it at runtime. See Writing a server.
  • One request per commit. The VFS buffers a transaction's pages, including those SQLite spills mid-transaction, and sends them as one Commit. A crash at any point leaves the last committed state. See Transactions.
  • Consistent replicas. On a store that reads as of a version, any number of read-only clients each read one committed version per transaction. See Replicas.
  • Caches that never change an answer. A writer's cache serves reads without requests while it holds the lease, and survives restarts. A replica's cache follows commits with one ChangedPages call. Every cached page is checked against its checksum before use. See Page cache.
  • Checked by simulation. A deterministic simulator drives SQLite, the VFS and the reference server through crashes, lost responses, lease takeovers and damaged caches, and checks every answer against a model. 100,000 seeds run in under three minutes. See Simulator.

Performance

Request counts come from the simulator, so they are exact and repeat run to run. Wall times run over gRPC on loopback with 1 ms added to every response, on one Intel Core Ultra 9 285. The database holds 20,000 rows of 200 bytes in 4 KiB pages, and SQLite caches 50 pages.

Requests Writer Writer, page cache Replica Replica, page cache
Insert 20,000 rows in one transaction 3
500 point lookups 478 0 978 500
50 range scans of 1,000 rows 398 0 448 50
Full scan 36 0 37 1
500 point lookups while a writer commits 50 times 1,245 619
Seconds at 1 ms a request Writer Writer, page cache Replica Replica, page cache
Insert 20,000 rows in one transaction 0.018
500 point lookups 1.041 0.001 2.122 1.077
50 range scans of 1,000 rows 0.879 0.006 0.987 0.115
Full scan 0.087 0.002 0.088 0.005

Clients without a page cache start cold. Clients with one run each workload once to warm it.

  • A commit is one request, whatever its size. The 20,000-row insert sent two commits: one for CREATE TABLE and one for the INSERT.
  • A writer's cache needs no requests. While the writer holds the lease, no one else can change the database. After a restart, one header read confirms the whole cache: a full scan took 3 requests instead of 36.
  • Scans read ahead. Sequential page reads double each fetch, up to 1 MiB. Without read-ahead, the full scan took 1,056 requests and the range scans 2,662.
  • A replica pays one request per read transaction. It must learn the latest version, so 500 single-row lookups cost at least 500 requests. With a cache, everything else comes from local disk, plus one ChangedPages call after each commit.
  • A replica's cache needs ChangedPages. Against a server without it, the cached replica sends as many requests as the uncached one. Each read then carries the cached page's checksum, and the server answers "unchanged" instead of sending the bytes again.
  • Every figure is round trips. At 1 ms each, 500 lookups take a second. Put the server near its clients.

cargo run --release -p grpsqlite-sim --bin bench -- --latency-ms 1 reruns both tables.

Quick start

Linux with Rust, a C compiler, libclang (libclang-dev on Debian and Ubuntu), curl, openssl and unzip. Start the in-memory reference server, then build a SQLite 3.54.1 shell with gRPSQLite as its default VFS:

cargo run --release -p grpsqlite-memory &   # listens on 127.0.0.1:50051
scripts/build-sqlite
target/sqlite/sqlite3-grpsqlite main.db
CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO users(name) VALUES ('Alice'), ('Bob');
SELECT * FROM users;

Quit and reopen main.db: the rows live on the server. Open it read-only for a replica:

target/sqlite/sqlite3-grpsqlite -readonly main.db 'SELECT count(*) FROM users'

scripts/smoke runs examples/*.sql against servers with and without atomic commits.

Guarantees

  • Atomic commits. A transaction reaches the server whole or not at all. A client crash or failure at any point leaves the server at the last committed state. Atomicity is per database: a transaction across attached databases commits each one separately.
  • Durable commits. COMMIT succeeds only after the server acknowledges every page of the transaction. A failed COMMIT has an unknown outcome, and the next transaction reads whichever state the server holds.
  • Single writer. At most one client holds a database's write lease, and the server rejects writes without it. A client that loses its lease stops writing and stops trusting its caches.
  • Consistent replicas. On a server with point-in-time reads, every read-only transaction reads exactly one committed version.
  • Real SQLite. Stock SQLite works, linked in or loaded at runtime, with page sizes 512 to 65536, rollback-journal modes, ATTACH, and several databases and connections per process.
  • Invisible caches. Local caching changes latency, never results.

How it works

SQLite reads and writes its files through a VFS. The gRPSQLite VFS keeps each database's state in one place per process, holds its lease, and turns SQLite's file calls into requests:

sequenceDiagram
  participant Q as SQLite
  participant V as gRPSQLite VFS
  participant S as Your server
  participant D as Your store
  Q->>V: open main.db
  V->>S: Open, read-write
  S-->>V: session, lease, capabilities
  Note over V,S: heartbeats keep the lease alive
  Q->>V: read page 1
  V->>S: Read
  S->>D: get
  Q->>V: write pages, spill the cache
  V->>V: buffer the transaction in memory
  Q->>V: sync, at COMMIT
  V->>S: Commit: every page, with the lease
  S->>D: one transaction, if the lease is current
  S-->>V: the new version
  V-->>Q: COMMIT succeeds

Temporary files, statement journals and, on servers with atomic commits, rollback journals stay in client memory. Only the files that must survive a client crash go to the server.

Transactions

The VFS buffers every write and truncation of the main database from a transaction's first write. Reads inside the transaction see the buffer. At SQLITE_FCNTL_SYNC, which SQLite sends at every commit even with PRAGMA synchronous=OFF, the VFS sends the buffer as one Commit. It discards the buffer when the transaction rolls back, when SQLite drops below a reserved lock, on close, and after an I/O error.

SQLite's own batch-atomic path would do this only in builds with SQLITE_ENABLE_BATCH_ATOMIC_WRITE, and it lets cache spills reach the file early. Owning the boundary makes every build atomic.

The lease holder keeps the database header and file size in memory and updates them from its own commits, so a transaction's first read needs no request.

Leases

A read-write open acquires the database's lease, and a background task heartbeats it. The server checks the lease inside the same transaction that applies a commit. When a heartbeat fails, or none succeeds within the lease's TTL, the VFS fails every operation on the database with SQLITE_IOERR and drops its caches until the application reopens it. A reopen acquires the lease again.

Several connections to one database in a process share the lease, and an in-process lock table enforces SQLite's lock levels among them. Rollback journals belong to the session that wrote them, so a connection opened after a lease loss never plays back the broken session's journal over another writer's commits.

Replicas

A read-only open needs a server with atomic commits and point-in-time reads. Each response carries an opaque version. The first response in a read transaction fixes the version, the VFS sends it with every later read, and SQLite's unlock releases it. Each read also returns the file's size at that version, so a transaction needs no separate size request.

SQLite notices commits through the header's change counter, which every commit through an atomic server updates. Exclusive locking mode stops those updates, so the VFS refuses PRAGMA locking_mode=exclusive on servers with atomic commits; the lease already makes the client the only writer.

Page cache

With GRPSQLITE_CACHE_DIR and GRPSQLITE_CACHE_MAX_BYTES set, each database caches pages on local disk, one file per page, evicting the least recently used.

  • Writer. While it holds the lease, no other client can change the database, so cached pages need no request, and commits update the cache. At a clean close the cache records the header's change counter. When the database next opens, the first header read either confirms every cached page or empties the cache.
  • Replica. The cache records the version its pages are current at. When a read transaction starts at a newer version, one ChangedPages call lists what changed, the cache drops those pages, and the transaction reads the rest locally. On servers without ChangedPages, each read sends the cached page's XXH3-64 checksum and the server answers "unchanged" instead of sending the bytes.

Every page is checked against its checksum before use, so a damaged cache costs a request, never a wrong answer. A lock file keeps two processes from sharing one database's cache; the second runs without one.

Servers without atomic commits

Without atomic_commit, SQLite's own crash recovery protects transactions. The rollback journal or WAL lives on the server, each Write applies alone, and after a crash the next writer replays the journal. Use journal_mode=delete, truncate or persist, or WAL with exclusive locking, set before the first read:

PRAGMA locking_mode=exclusive;
PRAGMA journal_mode=wal;

The VFS has no shared memory, so in normal locking mode SQLite keeps the database out of WAL, and a connection that opens a WAL database without exclusive locking fails with SQLITE_CANTOPEN.

SQLite treats a failed WAL write as one that never happened, yet the frames may have reached the server and completed a commit. After a failed WAL write, the VFS fails every operation until the database reopens, and the reopen rebuilds SQLite's WAL index from the server.

A transaction that writes several attached databases needs a server with atomic commits: SQLite's super-journal would otherwise live in client memory, and a crash would lose it.

Operator guide

Using the VFS

The VFS is the grpsqlite crate in crates/vfs.

Linked into SQLite. cargo build --release -p grpsqlite --features static builds target/release/libgrpsqlite.a. Compile SQLite with -DSQLITE_EXTRA_INIT=grpsqlite_extra_init and link the library: SQLite then registers gRPSQLite as its default VFS while it initializes. scripts/build-sqlite does this for the shell.

Loaded at runtime. cargo build --release -p grpsqlite --features dynamic builds target/release/libgrpsqlite.so. Loading it registers gRPSQLite as the default VFS:

.load target/release/libgrpsqlite

From Rust. With the static feature, grpsqlite::register_static registers a Core under any name, next to SQLite's own VFSes. Core takes any Backend; GrpcBackend is the gRPC client. crates/sim/src/harness.rs shows the pattern with rusqlite.

Configuration

Variable Default Meaning
GRPSQLITE_URL http://localhost:50051 The server.
GRPSQLITE_CLIENT_TOKEN empty Sent with every Open.
GRPSQLITE_CONNECT_TIMEOUT_MS 10000 Connection timeout.
GRPSQLITE_RPC_TIMEOUT_MS 30000 Deadline for each request.
GRPSQLITE_KEEPALIVE_INTERVAL_MS 10000 HTTP/2 keepalive interval.
GRPSQLITE_KEEPALIVE_TIMEOUT_MS 20000 HTTP/2 keepalive timeout.
GRPSQLITE_CACHE_DIR unset Local page cache directory; set with GRPSQLITE_CACHE_MAX_BYTES.
GRPSQLITE_CACHE_MAX_BYTES unset Most bytes each open database may cache.
GRPSQLITE_LOG warn off, error, warn, info, debug or trace, sent to sqlite3_log.

An invalid setting makes every open fail with SQLITE_CANTOPEN and logs the reason; the host process keeps running. The VFS connects on the first open, so a missing server fails that open rather than the process.

Server capabilities

A server declares what it supports in its Open response.

| Capability | Set it when your store can | What

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 131
Class 65
Function 48
Enum 7
Interface 3

Languages

Rust84%
C++16%
C1%

Modules by API surface

sqlite_vfs/src/sqlite-plugin/src/vfs.rs52 symbols
sqlite_vfs/src/sqlite-plugin/src/mock.rs40 symbols
sqlite_vfs/src/sqlite-plugin/sqlite3/sqlite3.h39 symbols
sqlite_vfs/src/lib.rs28 symbols
examples/versioned_memory_server.rs18 symbols
sqlite_vfs/src/sqlite-plugin/src/flags.rs17 symbols
examples/memory_server_with_checksums.rs14 symbols
examples/memory_server_no_atomic.rs13 symbols
examples/memory_server.rs13 symbols
sqlite_vfs/src/sqlite-plugin/src/logger.rs5 symbols
sqlite_vfs/src/handle.rs4 symbols
src/lib.rs2 symbols

For agents

$ claude mcp add gRPSQLite \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page