sebadob/hiqlite
حول
Hiqlite - highly-available, embeddable, raft-based SQLite + cache
البيانات والتحليلاتما يمكنك فعله
- full Raft cluster setup
- everything a Raft is expected to do (thanks to openraft)
- persistent storage for Raft logs and SQLite state machine
- "magic" auto setup, no need to do any manual init or management for the Raft
النمو المقاس للنجوم
- خلال 24 ساعة
- ٠
- خلال 7 أيام
- +٢
- خلال 15 يومًا
- +٦
- خلال شهر
- جارٍ جمع السجل
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
README.md
الأصلHiqlite
Hiqlite is an embeddable SQLite database that can form a Raft cluster to provide strong consistency,
high availability (which is where Hiqlite derives from), replication, automatic leader fail-over
and self-healing features.
Why
Why another SQLite replication solution? Other projects exist already that can do this. The problem is that none of them checks all boxes. They either require an additional independent process running on the side which can do async replication, need a special file system, have bad throughput / latency, or are running as a server.
I don't think that running SQLite as a server is a good solution. Yes, it is very resource friendly, and it may be a good choice when you are heavily resource constrained, but you lose its biggest strength when doing this: having all your data local, which makes reads superfast without network latency.
Hiqlite builds on top of rusqlite and provides an async wrapper around it. For the Raft logic, it
builds on top of openraft while providing its own storage and network implementations.
Goal
Rust is such an efficient language that you most often only need a single process to achieve whatever you need, for most applications at least. An embedded SQLite makes everything very convenient. You get very fast local reads and at the same time, it comes with the benefit that you don't have to manage an additional database, which you need to set up, configure and more importantly maintain. And embedded SQLite will bring database updates basically for free when you build a new application version.
When configured correctly, SQLite offers very good performance and can handle most workloads these days. In very first benchmarks that I did to find out if the project makes sense at all, I got up to 24.5k single inserts / s on a cheap consumer grade M2 SSD. These tests were done on localhost with 3 different processes, but still with real networking in between them. On another machine with older SATA SSDs it reached up to 16.5k inserts / s.
At the end, the goal is that you can have the simplicity and all the advantages of an embedded SQLite while still being able to run your application highly available (which is almost always mandatory for me) and having automatic fail-over and self-healing capabilities in case of any errors or problems.
Currently implemented and working features
- full Raft cluster setup
- everything a Raft is expected to do (thanks to openraft)
- persistent storage for Raft logs and SQLite state machine
- "magic" auto setup, no need to do any manual init or management for the Raft
- self-healing - each node can automatically recover from un-graceful shutdowns and even full data volume loss
- automatic database migrations
- fully authenticated networking
- optional TLS everywhere for a zero-trust philosophy
- encrypted backups to s3, cron job, or manual
(with s3-simple +
cryptr); backups on the local disk without the
s3feature - restore from remote backup (with log index roll-over)
- strongly consistent, replicated
EXECUTEqueries- on a leader node, the client will not even bother with using networking
- on a non-leader node, it will automatically switch over to a network connection so the request is forwarded and initiated on the current Raft leader
- strongly consistent, replicated
EXECUTEqueries with returning statement through the Raft- you can either get a raw handle to the row as
Row::Owned - or you can map the
RETURNINGstatement to an existing struct
- you can either get a raw handle to the row as
- transaction executes
- simple
Stringbatch executes - consistent read / select queries on leader
query_as()for local reads with auto-mapping tostructs implementingserde::Deserialize.query_map()for local reads forstructsthat implementimpl From<&mut hiqlite::Row<'_>>which is the more flexible method with more manual workquery_map_hint(),query_as_hint()andquery_raw_hint()- the local reads above with an extrasize_hintargument, the row count you expect, which the read thread builds its resultVecwith- a
FromRowderive macro with different optionalcolumnattributes to reduce boilderplate - a
sql!()macro that prepares your query against yourmigrationsdirectory at compile time, so a query SQLite cannot prepare fails the build instead of the query;sql_map!()also checks the columns it returns against the fields of the type you map it into - in addition to SQLite, multiple in-memory K/V caches with optional independent TTL per entry per cache - K/V caches are disk-backed and store their WAL file + Snapshots on disk, which means they are easy on your memory, and they can rebuild their in-memory data after a restart
- listen / notify to send real-time messages through the Raft
- geo-replicated clusters without latency on leader switches: mark a far node with
election_disablein its node line and it never nominates itself, while it stays a full voting member dlockfeature provides access to distributed lockscountersfeature provides distributed counters- optional raft traffic rate-limiting to always guarantee stability
- standalone binary with the
serverfeature which can run as a single node, cluster, or proxy to an existing cluster - integrated simple dashboard UI for debugging the database in production - pretty basic for now but it gets the job done
Performance
I added a bench example for easy
testing on different hardware and setups. This example is very simple and it mostly cares about
INSERT performance, which is usually the bottleneck when using Raft, because of 2 network
round-trips for each write by design.
The performance can vary quite a bit, depending on your setup and hardware, of course. Quite a lot
of work has been put into performance tuning already and I would say, it will be able to handle
everything you throw at it. When you reach the threshold, you are probably in an area where you
usually would not rely on a single database instance with something like a Postgres anymore as well.
SSDs and fast memory make quite a big difference of course. Regarding the CPU, the whole system is
designed to benefit more from fewer cores with higher single core speed like Workstation CPU's or
AMD Epyc 4004 series. The reason is the single writer at a time limitation from SQLite.
Just to give you some raw numbers so you can get an idea how fast it currently is, some numbers below. These values were taken using the bench example.
The benchmarks activate the jemalloc feature, which is quite a bit faster than glibc malloc but
is not supported on Windows MSVC target for instance. For cache performance, there are 2 different
metrics. One uses a disk-backed cache, the other one is fully in-memory. Disk-backed provides a lot
more consistency and can even rebuild the whole in-memory cache from the WAL + Snapshot on disk,
which means even a restart does not make you lose cached data. A pure in-memory version will need to
re-join and sync all data from the other nodes between restarts, but it will be a lot faster. The
disk-backed caches are limited by your disks IOPS and throughput only.
When you take a look at the numbers below, you will see that the SQLite implementation can almost reach the physical limits of the disk, when it has roughly the same throughput as the cache does. This is actually really impressive, considering that SQLite only allows a single writer at the same time.
Test command (-c adjusted each time for different concurrency):
cargo run --release -- cluster -c 4 -r 100000
Beefy Workstation
AMD Ryzen 9950X, DDR5-5200 with highly optimized timings, M2 SSD Gen4
SQLite:
| Concurrency | 100k single INSERT |
100k transactional INSERT |
|---|---|---|
| 4 | ~45.000 / s | ~680.000 / s |
| 16 | ~78.000 / s | ~602.000 / s |
| 64 | ~92.000 / s | ~535.000 / s |
For a simple SELECT, we have 2 different metrics. By default, hiqlite caches all prepared
statements. A simple SELECT with a fresh connection, which has not been prepared and cached yet,
it took ~180-210 micros. Once the connection has been used once and the statement has been cached,
this drops down dramatically to 6 -25 micros (hard to measure these short ones).
Cache (disk-backed):
| Concurrency | 100k single PUT | single entry GET |
|---|---|---|
| 4 | ~51.000 / s | ~6 micros |
| 16 | ~87.000 / s | |
| 64 | ~103.000 / s |
Cache (WAL in-memory):
| Concurrency | 100k single PUT |
|---|---|
| 4 | ~103.000 / s |
| 16 | ~272.000 / s |
| 64 | ~500.000 / s |
Older Workstation
AMD Ryzen 3900X, DDR4-3000, 2x M2 SSD Gen3 as Raid 0
Note: These tests were not performed with the latest version yet. They are expected to be ~12% faster now.
SQLite:
| Concurrency | 100k single INSERT |
100k transactional INSERT |
|---|---|---|
| 4 | ~11.000 / s | ~391.000 / s |
| 16 | ~21.000 / s | ~325.000 / s |
| 64 | ~28.000 / s | ~282.000 / s |
Cache (disk-backed):
| Concurrency | 100k single PUT | single entry GET |
|---|---|---|
| 4 | ~13.000 / s | ~12 micros |
| 16 | ~26.000 / s | |
| 64 | ~34.000 / s |
Cache (WAL in-memory):
| Concurrency | 100k single PUT |
|---|---|
| 4 | ~24.000 / s |
| 16 | ~72.000 / s |
| 64 | ~160.000 / s |
Crate Features
default
By default, the following features are enabled:
auto-healaws_lc_rsbackupsqlitetoml
aws_lc_rs / ring / fips
The rustls crypto backend. Exactly one of aws_lc_rs or ring must be selected whenever TLS is
compiled in (the sqlite and cache features enable it), and aws_lc_rs is the default. Selecting
both, or TLS with neither, is a compile error. Whichever you pick, the other backend's crates are
never compiled in.
aws_lc_rs prefers the post-quantum hybrid group X25519MLKEM768 ahead of X25519 in its TLS 1.3
key exchange. ring has no post-quantum group at all, so a ring build negotiates X25519. A peer
that supports both takes the group offered first, so the difference shows up as the group in the
handshake, never as a failure.
fips is the FIPS-validated aws-lc-rs build. It implies aws_lc_rs and cannot be combined with
ring. It builds aws-lc-fips-sys from source, so it needs a C toolchain, cmake and libclang.
auto-heal
This feature allows for auto-healing the State Machine (SQLite) in case of an un-graceful shutdown.
To reduce I/O and improve performance, Hiqlite does not write the last_applied_log_id from the
Raft messages into SQLite with each write. If it would do that, we would need to execute 1 extra
query for each incoming request, which effectively would double the amount of I/O if we just think
about single EXECUTE queries. Instead of doing that, it tracks the last applied ID in memory and
only persists it into the DB in the following situations:
- a new snapshot creation has been triggered
- a backup has been triggered
- the metadata of the whole Raft changes (leader change, a node has joined, ...)
- the node is being shut down
To make sure it would not start up a database where the last ID has not been persisted correctly,
Hiqlite creates a lock file at startup (like most other DB's). If this file exists with the next
start, it means that the application has been killed (host crashed, kill -9, ...), because
otherwise it would remove the lock file after the last_applied_log_id has been persisted
correctly.
The auto-heal feature enabled the functionality to recover an un-graceful shutdown automatically
by simply deleting the whole existing SQLite and rebuilding it from the latest snapshot + raft logs
to always reach a clean state.
If you have special needs, you may not want this. I can't think of a situation where it would make much sense to disable it, but you could do it.
backup
This feature allows the creation of automatic backups for disaster recovery. It pulls in cron as
an additional dependency and enables the sqlite feature as well, because it does not make sense
without it.
When backup is enabled, you will get the (by default) nightly backup cron job and you can manually
trigger backup creation's via the hiqlite::Client. Backups without pushing them to an S3 storage
don't make too much sense, because even when a cluster node would lose its whole volume, it would
simply be rebuilt from the current raft leader via snapshot + log replication.
With this feature alone, backups stay on the local disk: every node vacuums a copy into the
backups directory of its data_dir, and the local cleanup job deletes what has expired. The
encrypted push to a bucket, the bucket cleanup, and restoring from an object there is the s3
feature on top.
Backups will be created locally first on each of the Raft nodes. Afterward, only the leader will encrypt the backup and push it to the configured S3 bucket for disaster recovery.
Auto-restoring from a backup on S3 storage will also be possible with this feature enabled. The likelihood that you need to do this, is pretty low though.
You lose a cluster node
If you lost a cluster node for whatever reason, you don't need a backup. Just shut down the node, get rid of any possibly left over data, and restart it. The node will join the cluster and fetch the latest snapshot + logs from the current leader node.
You lose the full cluster
If you end up in a situation where you lost the complete cluster, it is the only moment when you probably need restore from backup as disaster recovery. The process is simple:
- Have the cluster shut down. This is probably the case anyway, if you need to restore from a backup.
- Provide a backup file name on S3 storage with the
HQL_BACKUP_RESTOREvalue with prefixs3:(encrypted), or a file on disk (plain sqlite file) with the prefixfile:. Set it on every node of the cluster: node 1 applies the backup, all other nodes wipe their state and join clean. - Start up the cluster again.
- After the restart, make sure to remove the
HQL_BACKUP_RESTOREenv value.
An embedded app can hand the same source to hiqlite::start_node_from_backup as a parameter
instead, which never reads the env var. That is what you want when one process runs several Hiqlite
instances with their own backups.
cache
This feature will start another independent raft group (can run without sqlite enabled as well).
The hiqlite::Client will get new functions like get() and put(). The cache feature will
build multiple raft-replicated, in-memory caches on all nodes. Basically an in-memory KV store with
optional per cache per entry TTL for each key.
cast_ints
This feature will make it possible to do integer casts when converting from a DB INTEGER. SQLites
INTEGER is technically always an i64. This means you cannot safely convert into e.g. a u32.
However, since Rust is strictly typed, you can assume that even though columns can store i64, they
will never contain values outside a u32.
This version will do boundary checks. For instance, when you want to cast to u32 and your DB
column contains a value bigger than u32::MAX, it will only contain up to u32::MAX. When it's
below 0, it will only contain 0, and never overflow.
DB column values can be cast into the following types with this feature:
u64i32u32i16u16i8u8Option<any_of_the_above_ints>
cast_ints_unchecked
This works in the same way as cast_ints, with the exception that it's a tiny bit faster. It will
do an unchecked cast without boundary checks. When you insert into properly typed values with your
queries, this is safe to use.
dashboard
This feature is the one that makes the crate size on crates.io that big. Hiqlite comes with pre-built, static HTML files to optionally serve a simple dashboard. With this dashboard, you have the possibility to run queries against your database, which typically is not that easy for a SQLite in production, which is probably deployed inside some container.
The dashboard will be served alongside the API HTTP server. It is very basic for now, but it gets
the job done. It will pull in quite a few extra dependencies and enable sqlite feature, because it
does not work with the cache or other features currently.

dlock
The dlock feature gives you access to distributed locks, synchronized over all Raft nodes. It
depends on the cache feature to work.
In some cases, you can't achieve what you need to do within a single query or inside a transaction. For instance, you need to fetch data from the DB, compute stuff with it, and write something back to the DB while the data on the DB must be locked the whole time. Because transactions with Hiqlite can't let you hold a lock directly on the DB (because of the Raft replication), you get distributed locks.
You can lock any key, then do whatever you need, and as soon as the Lock you will get is being
dropped, it will be released automatically.
Important: Distributed locks have a hard timeout of 10 seconds (2s in debug builds), and they send heartbeats every 3 seconds (500ms in debug builds). A heartbeat extends the expiry. The hard timeout makes sure that a stale or crashed client can never hold a lock forever.
external-state-machine
This opt-in feature is for applications that already own a trusted consensus log. It exposes a single-writer SQLite engine with true read-only connections on threads of their own, bounded retry receipts, and validated page-image snapshots. It does not start a Hiqlite Raft group, network service, membership store, or raw-SQL replication protocol.
The caller submits a dense monotonic sequence, an opaque exact native coordinate, a canonical
command digest, and a typed DeterministicSqliteOperation. Every global entry must be represented
by either an applied operation or an explicit advance-only entry. Exact retained retries recover the
persisted typed response without re-executing.
synchronous=FULL is the default; NORMAL and rebuildable ReplayableOff are explicit. External
mode never invokes auto-heal or implicitly deletes its database. It also makes no atomicity claim
between SQLite, the caller's consensus log, or another storage engine. See the
external_state_machine module documentation for the recovery, snapshot, and format contracts.
build_snapshot_into publishes without replacing an existing caller-owned path, and restore
closes and recreates the read connections. A read that is already inside its query is waited for up
to a 30s grace; past it the restore proceeds and that one read keeps answering the rows it started
on. Migrating a quiescent legacy database requires the explicit adopt_existing_projection
constructor; the caller must prove that database is the agreed initial state. An outer manifest
codec should map ExternalSnapshotEvidence into a frozen versioned format it owns, reconstruct the
typed value with the public evidence constructors,
and call ExternalSqlite::validate_snapshot to verify a decoded staged image before opening or
mutating a live projection. The engine's Serde shape is descriptive, not a stable wire
format promise; EXTERNAL_SQLITE_SNAPSHOT_FORMAT exposes the page-image identity without
duplicating a private literal.
This feature is deliberately excluded from both default and full; enable it only for the
alternate ownership model. A minimal walkthrough with a mocked consensus log lives in
examples/external-state-machine.
Use the re-exported hiqlite::rusqlite when implementing operations, so the Transaction type
always matches the version Hiqlite was compiled against.
full
This feature enables the regular Hiqlite cluster features apart from the server feature:
auto-healbackupcachecountersdashboarddlocklisten_notifys3shutdown-handlesqlitetomlwebpki-roots
in-memory-snapshots
By default, even if you set cache_storage_disk = false to keep the WAL in-memory, Cache snapshots
will still be written to disk into a temp file. This means the data_dir must be writable.
Snapshots are only necessary if a new node joins the Raft cluster, and then only the leader needs to
stream the latest snapshot to the new node. This means in most cases, you never need them. Writing
them into a temp file is the most efficient solution. However, with this feature, you can opt-in to
keep snapshots in-memory as well. This will not need any disk at all (as long as you don't enable
sqlite of course). This is costly, though, because you effectively double your memory requirements
for each single cache entry.
jemalloc
This feature enables the jemallocator instead of using the default glibc malloc. It is a lot
more performant, solves some issues with memory fragmentation, and can be tuned for specific use
cases. However, it does not work on Windows MSVC targets and out of the box, without any tuning, it
will use a bit more memory than default malloc.
listen_notify
Sometimes, you need something simple like Postgres' listen / notify to send real time messages
between nodes of your deployment, without the need for message delivery guarantees or something like
that. That is exactly what the listen_notify feature will let you do. It pulls in a few additional
dependencies and enables the cache feature it depends on.
Depending on your setup, you will get different levels of message delivery guarantees. The classic
Postgres listen / notify will forward messages, if another connection is listening, and drop them
if not, pretty simple. With Hiqlite, if your node is a real Raft member, meaning it is not using a
remote client, you will have a guaranteed once delivery with any form of listen(). If however you
have a remote client, which is connected to a remote Hiqlite cluster without a local replicated
state, you will not receive missed messages, if you stopped listening for some time. In this case,
you will have the classic Postgres behavior.
Important:
If you enabled this feature and you notify() via the hiqlite::Client, you must make sure to
actually consume the messages on each node. Behind the scenes, Hiqlite uses an unbound channel to
never block these. This channel could fill up if you notify() without listen().
s3
You would probably never just enable the s3 feature on its own in the current implementation. It
has been outsourced for a possible future feature expansion. It depends on the backup feature and
enables it, while backup no longer enables it back. This feature will enable the possibility to
push encrypted State Machine (SQLite) backups to a configured s3 bucket, to clean up the expired
backups in that bucket, and to restore from an object in it.
server
This feature only exists to make it possible to run Hiqlite as a standalone DB / Cluster, if you really want this. It will build a binary which spins up a cluster with the given configuration, or you can use it to install Hiqlite to spin up instances easily with
cargo install hiqlite --features server
You should never enable the server feature if you are using Hiqlite as a crate and run it inside
your application, which should always be preferred, because it would make all operations a lot
faster because of local data and less network round-trips. Embedding Hiqlite is actually one of its
biggest advantages over a server / client database like Postgres, which would never be able to even
come close to the read and SELECT speeds of a local SQLite instance.
shutdown-handle
As mentioned in other places already, a Hiqlite node should always be shut down gracefully to
prevent full State Machine rebuilds with each restart. Most applications already have some sort of
shutdown handles or can listen automatically. If you already have something like that, you can leave
this feature disabled and simply call hiqlite::Client.shutdown() before exiting your main(). In
any other case, you can enable the shutdown-handle and register an automatic shutdown handle like
shown in the examples, which you can .await just before exiting your main().
sqlite
This is the main feature for Hiqlite, the main reason why it has been created. The sqlite feature
will spin up a Raft cluster which uses a SQLite instance as the State Machine.
This SQLite database will always be on disk and never in-memory only. Actually, the in-memory SQLite
is slower than on-disk with all the applied default optimizations. The reason is that an in-memory
SQLite cannot use a WAL file. This makes it slower than on-disk with a WAL file and proper PRAGMA
settings in all of my tests. Another issue with an in-memory SQLite is that you will get into
problems with queries blocking each other all the time as soon as you have multiple connections for
the same reason as above: no WAL file.
This has its own feature though, because you may only be interested in having an in-memory cache /
KV store sometimes. In this case, you can disable the default features and only enable cache or
whatever you need. You would not even need any volume attached to your container in that case.
webpki-roots
This feature will simply enable baked-in TLS ROOT CA's to be independent of any OS trust store, like for instance when you don't even have one inside your minimal docker container.
Panics
Never panicking is NOT a goal of this crate. In fact, panic is very helpful and desired in
certain scenarios. We want to handle errors gracefully where it makes sense, of course, but this
crate will panic in situations where you encounter an unrecoverable error quite often. For
instance, when you are trying to map a value from the DB row to a struct, and the types are
incompatible by definition. This is a classic unrecoverable error. No matter how many times
your application tries to do it, it will never work. For this reason, when you derive FromRow for
instance, and you have a type mismatch with the DB, you will encounter a panic. This requires an
actual code or database change, this is nothing temporary. A panic is the only logical thing you
can throw here. It's fast, it's loud, you cannot miss it. When you only log some error in such
cases, this is easily missed and requires a lot more time during debugging. When your process
panics, you know you screwed up.
All situations that are recoverable, like e.g. a failed network request, will of course return
proper Results. If you ever encounter a panic and you think it's actually recoverable, please
open an issue and explain the situation. In general, panics are not bad, they are actually helpful
and serve a purpose when used correctly, and they actually speed up development by yelling at you
when you did something wrong.
In the example of the
FromRowderive macro: If you actually want your conversions to handle errors gracefully, > you can always do your ownimplfor types and usetry_*methods.
Limitations
You must not use non-deterministic functions inside your database-modifying statements. These are
functions like now() or random() for instance. Apart from the fact, that you should generally
avoid this on any database to shift the resource usage from the DB (which almost always will be the
bottleneck when scaling) into your application code, which is typically easy to scale, the writer
connection for the database will always panic! as a safety-net, if you use any of these functions.
If you really want to, you can use them on all non-modifying queries of course.
Known Issues
There are currently some known issues:
- When creating synthetic benchmarks for testing write throughput at the absolute max, you will see
error logs because of missed Raft heartbeats and leader switches, even though the network and
everything else is fine. The reason is that the Raft heartbeats in the current implementation
come in-order with the Raft data replication. So, if you generate an insane amount of Raft data
which takes time to replicate, because you end up being effectively I/O bound by your physical
disk, these heartbeats can get lost, because they won't happen in-time. This issue will be
resolved with the next major release of
openraft, where heartbeats will be sent separately from the main data replication. - In the current version, the logging output is very verbose on the
infolevel. This is on purpose until everything has been stabilized. In future versions, this will be reduced quite a bit.
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…