Blobasaur: Turning SQLite Files into a Distributed Blob Store
A chart layout is an awkward thing to store at scale. It may be a few kilobytes of settings or several megabytes of drawings and indicators. Users expect to change it repeatedly, reopen it immediately, and find it on a different machine. Yet it is usually just an opaque JSON document to the service that stores it. A relational query engine is more than the application needs; a pile of files leaves the application to rebuild transactions, expiry, and indexing.
Zerodha encountered this problem in Kite, its trading platform. Its roughly 20 million customers can customize charts, templates, drawings, and entire workspaces. That creates bursty traffic with frequent overwrites, particularly when markets move. The company had used CouchDB, then SSDB, a disk-backed Redis-protocol store. SSDB served for years, but its unmaintained code and single-node design became a limit. Payloads above 1 MB had at one point been capped because their latency degraded.
The replacement is Blobasaur, an open-source Rust server built around many SQLite databases. Its central idea is modest: SQLite remains an excellent local storage engine; routing and concurrency are handled outside each database file. Zerodha’s engineering account gives unusually useful numbers and describes the failures found during the migration. Those details make Blobasaur more interesting than a simple “SQLite can be fast” demonstration.
Start with the workload, not the database
The requirements were narrow. Existing Go and Python services used Redis commands such as GET, SET, HGET, HSET, DEL, and EXPIRE. They needed key and hash CRUD, TTLs, compression, and low latency for both small settings and multi-megabyte layouts. Reads and writes each made up about half of traffic. Complex queries, Lua scripts, and Redis’s wider data structures were outside the target.
That boundary matters. Zerodha also evaluated ScyllaDB, PostgreSQL BYTEA, YugabyteDB, RocksDB, Ceph, SeaweedFS, S3/MinIO, and raw files. Each could store bytes, but the surrounding costs differed. SeaweedFS’s append-before-compaction behavior was a concern for overwrite-heavy keys, and it lacked the per-blob TTL the application wanted. Object storage brought per-request costs and coarse expiry for this particular access pattern. Raw files would require atomic write handling, batching, directory and file-descriptor management, and a separate expiry index. The team’s comparisons are workload-specific, not universal benchmarks against those systems.
SQLite offered a transactional file format, recovery, an ordinary B-tree index for expiration, and tooling operators already understand. The SQLite filesystem benchmark says small blobs can be faster in a database than in separate files, but that result has a size boundary. Blobasaur exploits a property of its own data: repetitive JSON compresses sharply before it reaches SQLite. The system is not claiming every large binary object belongs in SQLite.
Rust supplied a single deployable server with predictable buffer ownership around compression and SQLite calls. The existing Redis wire protocol reduced application changes. Zerodha could move the storage service without rewriting every caller’s command model, although cluster-aware clients were needed at cutover.
Two levels of routing
The word distributed covers two separate moves here. Across machines, Blobasaur implements Redis Cluster routing. A key’s CRC16 hash assigns it to one of 16,384 slots. Each machine owns a configured slot range; a request that arrives at the wrong owner receives a MOVED response, so a cluster-aware client can retry at the right node. Within the chosen machine, multi-probe consistent hashing assigns the key to a SQLite database file, called a shard.
The two hashes solve different bottlenecks. More machines add capacity for the overall keyspace. More files on one machine add independent SQLite write locks. SQLite’s write-ahead logging mode allows readers to proceed alongside a writer, but only one writer can hold a given database file’s write transaction at a time. A single large file would therefore serialize about half of this workload. Splitting keys over files creates several write lanes without changing SQLite’s lock rules.
This is partitioning, not replicated consensus. The current design has static slot assignments; live slot rebalancing and automatic failover are not implemented. Adding a machine therefore involves operational planning. Changing the number of files on a node is also an offline migration: stop the server, run the built-in shard migrate command with verification, update the shard count, and restart. Consistent hashing limits how much data moves. With simple hash % n, going from four to five shards can remap about 80% of keys; a consistent-hash design aims to move roughly the new shard’s 20% share.
Make each file a small write service
Every shard has a bounded write queue and a dedicated writer task. Incoming writes are routed to that queue. When the writer is free, it drains pending operations and commits them in one BEGIN IMMEDIATE transaction. Grouping requests amortizes transaction and WAL work; the queue bounds the memory that can accumulate when demand outruns storage. Reads use a pool of read-only connections to the same file and can run alongside its writer under WAL mode.
Zerodha compared one shard with sixteen on a shared 16-core laptop. The test used 64 connections, 100,000 keys, 64 KiB JSON payloads, synchronous writes, and a batch size of 50. It kept total cache and read connections equal and put the data on tmpfs to isolate locking and CPU effects from disk variation. For HSET only, throughput rose from 7,740 to 25,324 operations per second, about 3.3 times; p99 moved from 10.7 to 8.3 ms. For a half-read, half-write mix, throughput rose from 14,227 to 29,538 operations per second. Read-only throughput barely changed, from 67,128 to 70,884 operations per second, because WAL readers were already concurrent. These are controlled local measurements, not a prediction for every deployment or disk.
The schema is deliberately plain. Ordinary SET keys live in a blobs table with a primary key, data BLOB, timestamps, optional expiry, and version. Redis-style hash namespaces such as HSET prefs user42 ... use separate blobs_<namespace> tables. A partial index on expires_at includes only rows with TTLs. A read checks expiry immediately; a background task later removes expired rows. This separates the user-visible meaning of expiry from the pace of physical cleanup.
Compression changes the size of the problem
Blobasaur supports zstd, lz4, gzip, and brotli; Zerodha uses zstd. In one hour of production HSET traffic, its average client payload was 63.6 KiB and the average stored value was 5.5 KiB, around an elevenfold reduction. At p99, 935 KiB became 59.3 KiB. Compression costs CPU, but for these repetitive JSON documents it also means fewer SQLite pages, less WAL traffic, and more useful values in the page cache. With 4 KiB pages, the average payload’s rough data-page count falls from sixteen to two before row and index overhead.
That arithmetic explains why the choice of SQLite worked despite multi-megabyte inputs. The input size is not usually the stored size. It also marks a limit: already-compressed images or random binary data would not produce the same ratios and could change the CPU, cache, and disk balance. The benchmark’s 64 KiB JSON compressed about ninefold; its 25,324 writes per second correspond to roughly 1.66 GB/s of uncompressed JSON processed by the benchmark system, not 1.66 GB/s of bytes persisted to disk.
The durability contract is a product decision
Zerodha stores UI preferences here, not orders or balances. Its shards use WAL with PRAGMA synchronous = NORMAL. SQLite keeps the database consistent, but a power loss can lose recent committed transactions that were not synced. SQLite’s pragma documentation makes this distinction explicit: consistency and survival of the last acknowledged write are different guarantees. A lost chart tweak is undesirable, but its impact differs radically from losing a trade.
Blobasaur also has an optional async_write mode that acknowledges a request when it enters the in-memory queue, before the writer commits it. That shortens the response path but widens the loss window on a crash. It creates a subtler correctness obligation too: an immediate GET after SET must see the queued value, and a GET after queued DEL must not resurrect the old SQLite row. Blobasaur keeps a pending map in front of SQLite reads, including tombstones for deletes. Zerodha found paths such as existence and TTL checks that initially bypassed this map and fixed them so every relevant read observes pending writes. A graceful shutdown drains accepted operations, subject to a configured timeout; sudden process loss is still governed by the async durability trade-off.
Expiry and disk space need their own lanes
The first production surprise was file size. Deleting millions of rows did not shrink SQLite files because freed pages normally remain on a freelist for later reuse. Blobasaur uses incremental auto-vacuum and a maintenance command with a bounded reclaim amount, then a truncating WAL checkpoint to return space. SQLite’s auto-vacuum documentation explains why the setting must be planned when the database is created or introduced through a full VACUUM migration.
The second surprise was p99 latency. A TTL cleanup worker originally opened its own write connection and issued a large deletion. It contended with the shard’s main writer, pushing normal write p99 from under 5 ms to occasional 30–50 ms spikes. Zerodha moved cleanup through the same writer and capped passes at 1,000 rows by default. Expired values were already hidden by read-time checks, so physical deletion could be spread over small batches without changing what clients saw. This is a useful general pattern: maintenance jobs are part of a storage engine’s workload and must share its scheduling policy.
Migration and production results
The replacement happened in stages. New Kite Terminal workspaces went to Blobasaur first. Existing services then dual-wrote to SSDB and Blobasaur while a backfill moved historical keys. Reads preferred Blobasaur and fell back to SSDB on a miss or error. After a month of dual writes without discrepancies, Zerodha switched the clients to the three-node Blobasaur cluster and retired SSDB. That sequence supplied a comparison window before the old store disappeared.
The reported production cluster has three nodes, each with 4 vCPUs and 8 GB of RAM. Across a full trading session, overall command p99 stayed around 2 ms per node, with p50 between 250 and 550 microseconds. By command, HSET p99 was around 2 ms, HGET around 1.4 ms, and HDEL mostly below 1 ms. These are measurements from Zerodha’s own workload and deployment, not a blanket promise for Blobasaur users.
The larger lesson is about system boundaries. Blobasaur did not ask SQLite to become a multi-writer network database. It kept transactions and recovery inside small local files, placed write scheduling around each file, and placed cluster routing above the nodes. Compression made the blobs fit that design. The remaining limits—static slots, offline resharding, no automatic failover, and explicit durability choices—are as important as the speed figures when deciding whether the design fits another service.