Why SQLite?
SQLite is widely accepted as one the most used databases due to it lightweight design and flexibility.
In essence, Tableland is a trustless, serverless database network and protocol. This network consists of isolated servers (nodes/validators) that serve database queries to localized databases. With SQLite, there are a few advantages to call out:
- Running a Tableland validator is a single process, so a node doesn't need to run multiple services to participate in the network.
- Extra monitoring required for the database since it’s part of the main validator process.
- SQLite requires fewer resources compared to a non-embedded database, making it cheaper and easier to run.
- There is a ton of tooling out there to help validators fine tune their setup, without complex code or SQL migration steps.
Criteria
Ultimately, choosing a database for any application can be hard, comes with tradeoffs, and is often done "by trend." One should always start by enumerating application and/or network requirements. SQLite is the only database that meets the following criteria:
-
Portable data format: Databases should be easy to load/unload from the network by users and the protocol itself, and to/from cold storage (Filecoin, etc.).
- SQLite databases are a single file. Note that technically, there are two in Write-Ahead Log ("WAL") mode (i.e., what Tableland validator nodes default to), but in the absence of connections, there is only one file per database. This makes them highly portable because they can be treated like a normal file.
- SQLite databases have a well-defined, cross-platform, and stable file format. Not only is this great for portability, but this file format is self-describing across platforms and even makes for a great archive format.
-
Durable: Making backups of databases should be easy and not require complex workflows, operating systems, or format considerations.
- SQLite databases are easy to replicate via simple file copying (databases are self-contained files) or tooling like Litestream and LiteFS (see below for descriptions of these projects).
-
Performant / scalable: Client/server databases add significant latency to queries, and SQLite has no network. With correct tuning, it’s much faster than Postgres—the most popular and performant open-source RDBMS.
- Below highlights benchmarking network latency for point query performance, comparing SQLite and various Postgres setups (using AWS). The same trends are also demonstrated when queries are ran in parallel.
- Being an embedded database, SQLite sets up Tableland validators to (in the future) offer very fast compute over data without the need to touch a network. This amounts to the serverless and trustless experience for the target developers of the protocol. For reference, Cloudflare is also onto this approach with SQLite and their web Workers.
- While SQLite supports concurrent reads to a single database, it does (currently) suffer from a single-writer problem and is, therefore, only recommended for applications that are read heavy and have write needs ≤ 100 query/s. However, in practice, these constraints are acceptable for 99% of applications. Tableland validators can be hosting hundreds or thousands of distinct databases, namespaced by application (with optionality for tenant isolation), serving writes concurrently between them. SQLite recently shipped a limited notion of write concurrency, but truly scalable concurrent writes to a single database is an active area of development. See here for one approach.
- There are a number of options for horizontal scaling individual SQLite databases:
- Table sharding via smart database design (parent/child tables to circumvent the 281 terabytes database size limit).
- Consistent hashing schemes like those used to power the Kademlia algorithm.
- SQLite responds well to vertical scaling (adding more CPU and RAM).
-
Full SQL spec coverage: Contrary to popular belief, SQLite is no toy. It boasts wide coverage of the SQL spec.
-
Easy to admin / maintain: This is an especially important for a decentralized network run by many independent operators and should minimize the number of trip wires.
- SQLite just works. Very little configuration or maintenance is needed.
- The Tableland validator embeds SQLite and its minimal configuration. There is no RDBMS to deal with—just a single binary. There’s not extra monitoring required for the database since it’s part of the main validator process.
- SQLite doesn’t ship user management overhead or other bulky features that are not needed in Tableland.
- SQLite has no external dependencies.
- SQLite is some of the most tested open-source code in the world.
- SQLite requires fewer resources compared to a non-embedded database, making it cheaper and easier to run.
-
Fast and easy local app development and testing: SQLite is a library that can be run in-memory, which allows for rapid local Tableland app development and testing before pushing to the paid network.
- Tableland developer tooling offers a Hardhat-like development environment that includes a local EVM and Tableland network, which wouldn't be possible without SQLite. Slow development and testing loops lead to unhappy developers.
- There are a number of great GUIs for SQLite that aid in local development, like Beekeeper.
-
Broad OS support (server, browser, mobile): SQLite is the worlds most deployed database. It runs everywhere: Linux-based servers, Windows, iOs, Android, Cloudflare’s D1, and even in the browser (it will replace Web SQL).
- This is important, as Tableland aims to marry local application databases (which are almost always SQLite) and private hosted databases (like D1), with remote Tableland databases to create a hyper flexible data layer for web3 applications.
-
Simple, modular and extensible:
- SQLite has a simple and modular design. As Tableland grows, we need the flexibility of experimenting and quickly prototyping new language features or ideas.
- SQLite provides a native way of intercepting the file system API via VFS, which is provides flexibility as Tableland grows. Others have explored different FUSE-based approaches.
-
Big and growing community of open-source developers: SQLite the most ubiquitous database in the world, used by billions of devices a day, and exciting open-source projects around SQLite pop up every day. Some highlights include:
- Litestream, streaming replication for SQLite.
- LiteFS, a successor to Litestream that adds replication at the level of transactions using a FUSE abstraction.
- rqlite, a lightweight, distributed relational database built on SQLite.
- dqlite, fast, embedded, persistent SQL database with Raft consensus.
- mvSQLite, MVCC SQLite that come with scalable read and write concurrency via SQLite’s OS interface, VFS.
- BedrockDB, private blockchain-based distributed SQLite built by Expensify, used by millions of users.
- AergoLite, SQLite with blockchain.
- libSQL, an open source, open contribution fork of SQLite, looking at WASM user-defined functions, and other exciting features.
- D1 tooling (workers-qb, d1-console).
- Too many fun browser / WASM related projects to mention—here’s one.
- New and improved drivers—here’s one.
-
Permissive license: Tableland aims to be a user-owned “public good” layer of the internet. We need a license that permits this type of use. SQLite is public domain.
-
People love it: There are more SQLite mentions on Twitter than Postgres!
SQLite usage & web3
Due to the points described above, there are a number of general use cases in which developers use SQLite today, including:
- Embedded devices and the IoT
- Edge computing
- Application file format (i.e., on-disk)
- Websites (with, potentially, millions of requests per day)
- Data analysis
- Cache for enterprise data
- Server-side database
- Data transfer format
- File archive and/or data container
- Replacement for ad hoc disk files
- Internal or temporary databases
- Education or training related (demos, testing, etc.)
- Experimental SQL language extensions
Note there are some scenarios in which SQLite isn't optimal. But, with Tableland's decentralized infrastructure, each node running a local instance of SQLite can provide fault tolerance and data redundancy, which can help to ensure data availability and durability. By streaming database creates/writes from a blockchain's event logs, each node can maintain a consistent and ordered copy of the database, which can help to prevent data inconsistency issues that might arise from concurrent updates. At a layer above the core database engine, other features around sharding, replication, triggers, etc. can help meet scalability needs and are actively being researched.
Next steps
Aside from these generalized SQLite applications, check out the use cases page for how developers use Tableland's web3-enabled SQLite!