← all systems · see it in The System
Embedded database
| boundary | Inside: the engine's code linked into the host program (its API, SQL compiler and virtual machine where it has them, access methods, pager, locks, journal or log) and the files it owns (the database file, its journal or WAL, shared-memory index, LMDB's lock region with its reader table and writer mutex, LSM tables). Outside: the host application and its other code, the operating system and file system, the storage devices, other processes that open the same file, the network (which it does not reach itself), and the people who build and use the applications. |
| nested level | A part inside a program: a library in the application's process (software L4, runtime and libraries); its files sit in mass storage reached through the operating system. A kind of database (Database), the in-process kind; some embedded engines are in turn parts of server databases (RocksDB under MySQL). |
| scale | technological |
From the walk
SQLite's own makers say it 'does not compete with client/server databases. SQLite competes with fopen()': it is a better way to save a file. Billions of copies exist, there are likely over a trillion SQLite databases in active use, and its makers' best guess is that it is the second most widely deployed software library after libz, perhaps the most deployed counting copies per device (src:sqlite-whentouse, src:gaffney-2022, src:sqlite-mostdeployed).
Definition. A database engine that runs as a library inside the application that uses it, with no server process, keeping its data durable in local files: 'the process that wants to access the database reads and writes directly from the database files on disk' (src:sqlite-serverless). The encyclopedia's category is wider (C-4583). Definition drawn from the encyclopedia entry (Wikipedia, CC BY-SA).
On this page: Properties 2 · Measures 33 · Open contentions 3 · Cross-scale patterns 21 · Valence partners 12 · Partners and parts 71 · Notes 3 · Layers 60 · Sources 32
Properties published scienceour proposal
Sourced fields, and our own readings.
| born at | 1979: dbm, by Ken Thompson, released by AT&T |
| born precision | year |
Measures (33) published science
| characteristic clock: lock hold time in SQLite | a few dozen ms (upper bound, makers' statement) |
| characteristic clock: Berkeley DB recovery after a crash, with checkpoints | 1-2 s |
| characteristic clock: fsync calls per SQLite insert on EXT4 (Android, 2013) | 2 or more within 2 fsync per ms |
| characteristic clock: Sleepycat release cadence | about 3 commercial releases per year (1999) |
| SQLite databases in active use | >1e12 databases (estimate) |
| smartphones in active use (basis of the trillion estimate) | >4.0e9 smartphones |
| SQLite compiled library size | <750 (all features); 900 KB at -Os, 1.5 MB at -O3 in the 2022 build KiB |
| SQLite source size | about 150,000 lines of C in one file |
| SQLite test code per line of library code | >600 lines |
| SQLite test code (2014) | >91 million lines (with parameterized repeats) |
| SQLite-WAL TATP throughput (cloud server) | 10,000 transactions per second |
| SSB latency: DuckDB over SQLite | 3-8x (flight 1) to 30-50x (flight 2) times faster |
| SQLite SSB speed-up from Bloom filters (Raspberry Pi) | 4.2 (2.7 on the cloud server) times |
| SQLite WAL autocheckpoint threshold | 1000 (about 4 MB) pages |
| SQLite maximum database size | 281 TB (2^48 bytes) |
| Berkeley DB maximum database size (1999) | 2^48 (256 TiB, about 281 TB) bytes |
| website traffic SQLite is said to handle | <100K (conservative); sqlite.org itself 400K-500K requests in 2015 hits per day |
| Berkeley DB code size (1999) | about 175K of text space |
| LMDB object code size (2012) | 32 KB (Berkeley DB over 1.5 MB) |
| crash vulnerabilities found by ALICE (11 applications) | 60 static (156 dynamic); LevelDB 1.10: 10, LevelDB 1.15: 6, GDBM: 5, LMDB: 1, SQLite rollback: 1, SQLite WAL: 0 vulnerabilities |
| share of injected power faults causing durability violations | 0.05-19.15 % (7 of 8 databases) |
| LMDB (LightningDB) durability violations under exhaustive power faults | 0.05 % of injected faults |
| exhaustive power-fault testing of LightningDB | just under 8 h (pattern-based: under 21 min) time |
| share of phone write requests going to SQLite files | about 90 % (Facebook and Twitter apps, Galaxy S3) |
| synchronous and random shares of phone writes | 70% synchronous; 75% random; 64% of 4 KB % of writes |
| SQLite inserts per second on a 2012 phone, before and after | 39 to 157 inserts per second |
| RocksDB write amplification, leveled compaction | 10-30 (tiered: 4-10; SSD's own: 1.1-3; software page writes: up to 100) bytes written per byte of data |
| RocksDB data held at Facebook | many hundreds PB, over 30 applications |
| RocksDB-level corruption rate | about 1 per 3 months per 100 PB; 40% already spread to replicas corruptions |
| checksum mismatches from a storage-system bug in transfer | about 17 per PB transferred |
| RocksDB writes per transaction against InnoDB (LinkBench on MySQL) | 5 % of InnoDB's |
| Debian systems regularly using libgdbm | 60 % of nearly 200,000 |
| SQLite TATP: SQLite-DELETE writes | about twice the writes of SQLite-WAL per blob update |
Open contentions (3) open contention
Where sources or readings disagree. Both stay on the map.
- C-4583 Is an 'embedded database' one built into a program with no separate server, or a wider category that includes server-based databases and those inside devices?
booked: The map keeps both readings side by side and rules on neither.
the other reading: the encyclopedia's wider category, which includes client-server architectures and several storage modes and models; 'embedded' as in embedded devices (real-time systems, consumer electronics), which the encyclopedia sets apart - C-4597 Can the LMDB database never be corrupted by a crash, as its maker says, or did testers find rare cases where it can lose data?
booked: The map keeps both readings side by side and rules on neither.
the other reading: the maker's claim: 'the database can never be corrupted by system crashes' (Chu 2012) - C-4615 Can the SQLite database, in its default setting, lose a just-saved change if the power fails, and is that a flaw or intended behaviour?
booked: The map keeps both readings side by side and rules on neither.
the other reading: the developers' reading, as Pillai et al. report it: 'not a vulnerability' (behaviour known and intended); SQLite's own account: 'full ACID guarantees' (Gaffney et al.) and 'highly resistant to corruption' (howtocorrupt)
Cross-scale patterns seen here (21) our proposal
Shapes that recur when the same kind of problem is solved at different scales, and how each shows in Embedded database. Each links to every other system that shows it.
- barrier-gates-complexity (structural)
The process wall of a server is the barrier: behind it a server can 'control database access with more precision, allowing for finer-grained locking and better concurrency' and is safe from client bugs; without it (serverless SQLite) there is one writer at a time and any process can overwrite the fi - same role, different builds (structural)
One job, keeping the stored data whole across a crash, done by unrelated builds: an undo journal (SQLite default), a redo log (Berkeley DB, LevelDB, SQLite-WAL), shadow paging with alternating meta pages (LMDB), reflink snapshots of the whole file (crash-tolerant gdbm), update-via-rename (HSQLDB). A - same shape, opposite sign (structural)
One mechanism, the pager's two-file commit, with its roles reversed: rollback mode writes the original pages to the side file and the new pages into the database; WAL mode keeps the originals in the database and appends the new pages to the side file. - templating (structural)
Modules swapped between systems and still working: the dbm interface reimplemented by ndbm, gdbm, sdbm, Berkeley DB and (Berkeley-style) LMDB; SQLite ported onto LMDB as its storage backend; RocksDB swapped in as MySQL's storage engine in place of InnoDB; one SQLite file read on any byte order. - The carry (structural)
Overflow passed on, with a wait: the WAL holds about 1000 pages and then passes its excess into the database file (the commit that triggers it is 'much slower'); an LSM level over its size target passes tables to the next level down. - The Payment to Entropy (structural)
Every hand-off down the storage stack writes more than it was given: RocksDB's leveled compaction writes 10-30 bytes per byte, tiered 4-10, the SSD itself 1.1-3; a full page written for a change under 100 bytes is up to 100; on a phone the file system journals the database's journal, so one insert b - trade one property for another (structural)
Write cost traded for read speed along a dial, with kinds settled at different points: lazier compaction (tiered, FIFO) buys write throughput and pays in reads, leveled the reverse; LMDB traded write performance for read performance; Berkeley DB runs faster single-user and without recovery; SQLite g - who opens the door (structural)
Every door is a port opened from inside ahead of time: the application links the library and calls its API; a writer must be granted the reserved lock; an LMDB reader takes a reader slot the first time a thread reads, kept in thread-local storage; the journal's fixed name is the door recovery comes - causal ceiling (partial)
A reach limit set by the build, not by means: the WAL index lives in shared memory, so every reader must be on the same machine and WAL does not work over a network file system; a RocksDB instance only ever manages one server node's storage. - lightning (partial)
A stored difference released at once: WAL pages pile up over many fast commits and a checkpoint moves them all back in one slower commit; a power fault mid-write, which changes what the next open must do. Gated by thresholds, so not a pure discharge. - nested ladder (partial)
SQLite's lock states form a ladder of stronger claims: shared (read) below reserved (intends to write) below pending (waiting for readers to clear) below exclusive (writing); each higher lock is taken only from the one below. PENDING is there against writer starvation: it lets existing readers finis - outside-to-inside (partial)
For SQLite, the database moved from outside the program to inside it: the Navy damage-control software first used an Informix database back-end in a separate server, and SQLite was 'born out of the frustration of debugging a database server running in a separate process'. The kind's oldest member, d - partner-becomes-part (partial)
Embedded engines become organs of server databases: RocksDB as MySQL's, MongoDB's and CockroachDB's storage engine; Berkeley DB as a MySQL table manager and Dynamo's backing store. The engine keeps its own releases (separate inheritance), so this is a part by linking, not by fusion. - red-queen (partial)
Move and counter-move in the competition for in-process analytics: DuckDB, built for it, beat SQLite 3-50x on SSB; SQLite's developers answered with Bloom-filter joins (4.2x); DuckDB was still 'considerably faster'. Half a turn shown (no answer from DuckDB in the sources read); form: a standoff for - same toolkit, independent builds (partial)
The same parts (B+trees, logs, two-phase locks, copy-on-write) assembled separately into Berkeley DB, SQLite and LMDB; the makers say 'the techniques for building systems like Berkeley DB have been well-known for a long time'. LMDB began from a separate append-only B-tree (Hedenfalk's), not from Ber - the whole sets the local reference (partial)
Each reader measures against its own snapshot: in WAL mode a transaction records the end mark of the last valid commit and reads only frames before it; in LMDB each reader sees the database as of its start. The committed whole (the newest meta page, the end mark) sets each reader's reference, and re - Umwelt: each system carves its own slice of one shared medium (partial)
The engine's whole world is its own files and its host's calls: it never sees the network (a remote file is an 'illusion' the OS gives it) and knows other users of its file only by their locks. The same disk holds many other files it never reads. - borrowed-then-held (surface)
Seen reversed: Berkeley DB held its own cache (and OpenLDAP added more on top); LMDB gave the job back to the operating system's page cache through a single-level store. Held, then borrowed. Crash-tolerant gdbm likewise borrows crash safety from the file system's reflink clones. - coordination-before-mapping (surface)
Processes sharing one SQLite file coordinate only through lock states on the file; none holds a model of the others ('Writers queue up'). - runaway from an unstable balance (surface)
The maker of LMDB describes the balance in garbage-collected MVCC stores: compaction must outrun writes ('>>2N operations/second'), and if it cannot, the usual answer is to deny updates. A balance point is named; the runaway itself is not measured. - symmetry breaking (name the cut) (surface)
Among equal connections, one takes the single reserved lock, and that choice sets what follows (it writes, the others wait).
Valence: what it trades, and with whom (12) published science
Each exchange with a neighbour, read from both sides: who gains, who pays, and what carries it.
- Human (Homo sapiens) via The world through the senses (sensory environment) · Embedded database gain
- Computer memory via Computer memory · Embedded database mixed
- Software via Computer memory · Embedded database gain
- Operating system via Computer memory · Embedded database gain
- Electronic mail via Computer memory · Embedded database gain
- Open-source software via Written record (durable external marks: texts, archives, canon) · Embedded database gain
- File system via Computer memory · Embedded database mixed
- Database via Computer memory · Embedded database gain
- Computer network via Computer network · Embedded database loss
- smartphone via Computer memory · Embedded database gain
- Directory service via Computer memory · Embedded database gain
- Write-ahead logging via Computer hardware · Embedded database gain
Partners and parts published science
Built from / builds (11)
- Transactions
- Query optimizer
- SQL (and other database languages)
- Hash table
- B-tree
- Write-ahead logging
- Rollback journal (undo journal)
- Shadow paging
- Log-structured merge tree
- Connections competing for the single write lock on one file
- An embedded engine against plain files for an application's local data
Sits inside / holds (4)
expressed by (24)
- Generation/Synthesis
- Recombination/Composition
- Scaffolding/Enablement (Meta-Tooling)
- Templating / Standardization
- Quality Improvement/Refinement
- Interoperability/Transduction (Bridging)
- Encapsulation & Modularity (Abstraction)
- Amplification/Scale-Up (Manufacture & Distribution)
- Evolvability/Plasticity Engineering (Design for Future Variation)
- Internal Coordination/Regulation
- Trail-Marking / Externalized Memory (Stigmergy)
- Surprise / Change-Point Detection
- Quick Harm Avoidance
- Reactive Response
- Proactive Defense
- Physical/Virtual Barrier
- Self-Monitoring
- Validation of Identity/Intention
- Validation of Effectiveness
- Resilience & Repair
- Redundancy & Diversity
- Quality Control & Error Correction
- Bridging Supply Gaps (Store & Dormancy)
- Scheduled Build-and-Dismantle (On-Demand Structure)
floor filled by (19)
- Human (Homo sapiens)
- Computer memory
- Software
- Operating system
- Electronic mail
- File system
- Flash, ROM, PROM, EPROM, EEPROM
- Hard disk drive and solid-state drive
- Transactions
- Query optimizer
- SQL (and other database languages)
- smartphone
- Directory service
- Hash table
- B-tree
- Write-ahead logging
- Rollback journal (undo journal)
- Shadow paging
- Log-structured merge tree
Kinds (13)
- Database
- dbm (and ndbm, gdbm, sdbm)
- Berkeley DB
- SQLite
- LevelDB
- RocksDB
- LMDB (Lightning Memory-Mapped Database)
- DuckDB
- Tokyo Cabinet
- Microsoft Jet (Access Database Engine)
- Extensible Storage Engine (ESE)
- Apache Derby
- HSQLDB
Notes published science
- SQLite's own makers say it 'does not compete with client/server databases. SQLite competes with fopen()': it is a better way to save a file. Billions of copies exist, there are likely over a trillion SQLite databases in active use, and its makers' best guess is that it is the second most widely deployed software library after libz, perhaps the most deployed counting copies per device (src:sqlite-whentouse, src:gaffney-2022, src:sqlite-mostdeployed).
- On a 2013 Android phone about 90% of the writes from Facebook and Twitter went to SQLite databases and their journals, and the file system was journaling the database's journal: one insert turned into two or more forced disk writes within 2 milliseconds (src:jeong-2013).
- When researchers simulated power faults at thousands of chosen moments, all 8 databases they tested, from open-source key-value stores to commercial OLTP servers, misbehaved; even LMDB, designed to need no recovery, failed in 0.05% of the faults, and its maker had written that it 'can never be corrupted by system crashes' (src:zheng-2014, src:chu-2012).
Layers
The ways it takes in its world, where they meet, and how it acts back, each from L1 (where it meets the world) up.
a log-structured key-value engine (LevelDB, RocksDB): writes to a log and an in-memory table, flushed to sorted files on SSD and merged level by level
- L1: Flash SSDs: fast, but writes wear the cells and reads and writes are asymmetric
- L2: Files on the server's file system: SSTables, the write-ahead log and the manifest
- L3: The write path: the write-ahead log and the in-memory MemTable (a skiplist), flushed to a sorted SSTable when full
- L4: The LSM tree: levels of sorted files, merged by compaction (leveled, tiered or FIFO), with block checksums and Bloom filters
- L5: The key-value interface: get, put, scans; user-defined timestamps
- L6: Embedded in a host: MySQL (MyRocks), CockroachDB, MongoDB, TiDB, Flink, Kafka Streams, caches; the host does replication, load balancing and backups
the boardroom (shared floors)
- L3: Pager and transactions: page cache, locks, rollback journal or write-ahead log, commit, rollback, checkpoint, recovery
- L4: Access methods: B-trees, hash tables, copy-on-write B+trees, LSM trees, with their checksums
- L5: The engine: the VDBE running bytecode (SQLite) or the key-value calls
- L6: The host process: the library inside the application's address space, on its threads
- L7: The application's use of its stored state
the embedded database as its makers describe it: a library in the application's own process keeping its data in local files, from the storage medium up to the application's use (reader's view)
- L1: Storage: the database file (and its journal or log beside it) as sectors on disk or flash; 'ordinary disk files', written in sectors that SQLite assumes are not written atomically
- L2: The OS interface: the virtual file system object (SQLite), shared memory and OS locks (Berkeley DB), a read-only memory map plus write() (LMDB); reads, writes, fsync and advisory locks
- L3: The pager and transactions: page cache, locks (SQLite's shared, reserved, pending, exclusive; Berkeley DB's two-phase locks), the rollback journal or write-ahead log, commit, rollback, checkpoint and crash recovery
- L4: Access methods: SQLite's table and index B-trees; Berkeley DB's B+tree, Hash and Recno; LMDB's copy-on-write B+tree; the log-structured merge tree of LevelDB and RocksDB
- L5: The engine: SQLite's SQL compiler and virtual machine (VDBE) running bytecode programs; in key-value engines, the get, put, cursor and join calls
- L6: The host process: the library linked into the application, called as functions on the application's own threads, in its own address space
- L7: The application's use: local data for one application or device: an application file format, a phone's records, web browsers and operating systems themselves (every Firefox, Chrome and Safari; every Windows 10/11 installation), a directory server's entries, mail aliases, a cache of enterprise data, a data scientist's dataset
way in: A crash or power loss in the middle of a write
- L1: Power fails or the application or OS crashes mid-transaction; the files are left partly written, and writes the device acknowledged may be missing
- L2: At the next open the engine finds a hot journal or a WAL beside the file
way in: A file reached over a network file system
- L1: The database file sits on another machine, reached through a network file system
- L2: The OS presents it as a local file; its locking may be buggy and its latency high
way in: Calls from the host application: SQL statements, or keys and values
- L1: The request in the application's own memory: an SQL statement (SQLite) or a key and a value (dbm, Berkeley DB, LevelDB, LMDB)
- L2: A plain function call into the linked library
- L3: Tokenizer and parser (SQLite): the statement read as SQL
- L4: Code generator and query planner: a bytecode program for the statement (key-value engines skip this floor)
way in: Corruption from beneath: media errors, bit flips, bad transfers
- L1: Faults below the engine: storage media corruption, CPU and memory corruption, bugs in copying files
- L2: Block checksums checked when a block is read
- L3: File checksums when files are moved; handoff checksums on WAL writes
way in: Hostile input: malicious SQL or a database file from another security domain
- L1: Untrusted SQL text, or a database file that something in another security domain could have written
- L2: Inputs arrive through the same API and file reads, against limits the application may have lowered
- L3: The file checked on open (integrity_check or quick_check) and refused if bad; the DEFENSIVE flag keeps SQL from writing the file's internals
- L4: Statements screened as they are compiled: an authorizer callback refuses classes of action
way in: Other connections, threads and processes sharing the file, seen through their locks
- L1: Other users of the same file: their shared, reserved, pending or exclusive locks; in WAL mode the shared-memory index; in LMDB the reader table
- L2: The lock request granted or refused by the operating system's file locks: POSIX advisory locks on Unix, LockFile/LockFileEx/UnlockFile byte-range locks on Windows (LMDB: a writer mutex in shared memory)
way in: The database file's own pages read back from storage
- L1: Sectors of the database file on disk or flash (whatever any process last wrote there)
- L2: Read through the OS interface (VFS read; LMDB and read-only Berkeley DB through a memory map)
way in: The developer's choices: options, journal mode, page and cache size
- L1: Decisions written into the host program: which features, how much concurrency and recovery
- L2: Passed as compile-time options, open flags and pragmas (journal_mode; WAL autocheckpoint)
way in: The engine's own growth: WAL pages since the last checkpoint, LSM level sizes
- L1: Its own files growing: the WAL accumulating pages; each LSM level filling
- L2: Compared with thresholds: 1000 WAL pages at commit; each level's size target
way out: Backup: a consistent copy of a live database
- L1: A snapshot file, bit-wise identical to the source as of the start of the copy
- L3: The online backup API copies pages, locking the source only while reading them
way out: Checkpoint and compaction: logged changes written back, old files merged and dropped
- L1: Files rewritten; the WAL overwritten from its start; flushed logs and old tables deleted
- L3: WAL pages copied back into the database file; Berkeley DB checkpoints put committed log changes on the data disk
- L4: LSM compaction merges a level's tables into the next level, removing dead data
way out: Commit: changes made durable on storage
- L1: The bits reach the medium, if the device does not acknowledge early
- L2: fsync through the OS; each fsync on EXT4 commits a file-system journal transaction of its own
- L3: The journal (or log) is forced to stable storage before the changes are applied, or the changes are appended to the WAL
way out: Errors reported: corruption caught by a checksum, handed to the application
- L4: A checksum mismatch found on read or transfer
- L6: The application discards the corrupt replica and copies in a good one
way out: Refusal and retry: the lock not granted, a deadlock victim rolled back
- L2: SQLite: the write attempt fails at once with SQLITE_BUSY
- L3: A deadlock victim chosen and rolled back; a second writer refused while one holds the write lock
- L6: The caller is told and may restart
way out: Rollback: the original state put back
- L1: The file as it was before the transaction
- L2: The rolled-back pages synced before the journal is deleted, truncated or zeroed
- L3: Original pages restored from the journal, or uncommitted logged changes undone
way out: Rows and values handed back to the caller
- L1: The value is in the application's memory
- L5: The VDBE extracts columns and emits a result row
- L6: Returned in the caller's own address space (LMDB: pointers into the map, no copy)