Schema
Redlite stores data in SQLite using a multi-table schema designed for Redis compatibility while leveraging SQLiteβs strengths.
Overview
Section titled βOverviewβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ keys ββ (db, key) β type, expires_at, created_at, updated_at ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β ββββββββββββββββββββββββββββββββββββββββ β β βΌ βΌβββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ strings β β hashes β β lists ββ (db, key) β β (db, key, β β (db, key, ββ β value β β field) β β position) ββ β β β value β β β value ββββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
βββββββββββββββββββ ββββββββββββββββββββ sets β β zsets ββ (db, key, β β (db, key, ββ member) β β member) ββ β β β score ββββββββββββββββββββ βββββββββββββββββββCentral metadata table for all keys.
CREATE TABLE keys ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, type TEXT NOT NULL CHECK (type IN ('string', 'hash', 'list', 'set', 'zset')), expires_at INTEGER, -- Unix timestamp (milliseconds) created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (db, key));| Column | Type | Description |
|---|---|---|
db | INTEGER | Database number (0-15) |
key | TEXT | Key name |
type | TEXT | Data type (string, hash, list, set, zset) |
expires_at | INTEGER | Expiration timestamp in milliseconds (NULL = no expiry) |
created_at | INTEGER | Creation timestamp in milliseconds |
updated_at | INTEGER | Last update timestamp in milliseconds |
strings
Section titled βstringsβString values (binary data stored as BLOB).
CREATE TABLE strings ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, value BLOB NOT NULL, PRIMARY KEY (db, key), FOREIGN KEY (db, key) REFERENCES keys(db, key) ON DELETE CASCADE);Hash field-value pairs.
CREATE TABLE hashes ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, field TEXT NOT NULL, value BLOB NOT NULL, PRIMARY KEY (db, key, field), FOREIGN KEY (db, key) REFERENCES keys(db, key) ON DELETE CASCADE);List elements with gap-based positioning for O(1) push operations.
CREATE TABLE lists ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, position REAL NOT NULL, -- Gap-based for efficient insert value BLOB NOT NULL, PRIMARY KEY (db, key, position), FOREIGN KEY (db, key) REFERENCES keys(db, key) ON DELETE CASCADE);The position column uses REAL (floating point) to allow efficient insertions:
- Initial elements: 1.0, 2.0, 3.0, β¦
- Insert between 1.0 and 2.0: 1.5
- Insert between 1.0 and 1.5: 1.25
Set members (unique values per key).
CREATE TABLE sets ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, member BLOB NOT NULL, PRIMARY KEY (db, key, member), FOREIGN KEY (db, key) REFERENCES keys(db, key) ON DELETE CASCADE);Sorted set members with scores.
CREATE TABLE zsets ( db INTEGER NOT NULL DEFAULT 0, key TEXT NOT NULL, member BLOB NOT NULL, score REAL NOT NULL, PRIMARY KEY (db, key, member), FOREIGN KEY (db, key) REFERENCES keys(db, key) ON DELETE CASCADE);
CREATE INDEX zsets_score ON zsets(db, key, score);Indexes
Section titled βIndexesβ-- Fast expiration lookupsCREATE INDEX keys_expires ON keys(expires_at) WHERE expires_at IS NOT NULL;
-- Fast score-based queries for sorted setsCREATE INDEX zsets_score ON zsets(db, key, score);Foreign Keys & Cascading Deletes
Section titled βForeign Keys & Cascading DeletesβAll value tables reference the keys table with ON DELETE CASCADE. When a key is deleted from keys, the corresponding data is automatically removed.
-- Deleting from keys tableDELETE FROM keys WHERE db = 0 AND key = 'mykey';
-- Automatically removes related rows from strings, hashes, lists, sets, or zsetsType Safety
Section titled βType SafetyβThe type column in keys ensures a key can only have one type at a time. Attempting to use a key with a different type results in a WRONGTYPE error (matching Redis behavior).
Expiration
Section titled βExpirationβExpiration is stored as Unix timestamp in milliseconds in expires_at. Keys are lazily expired:
- On read: Check
expires_at, delete if expired - Background cleanup: Not yet implemented (planned)
WAL Mode
Section titled βWAL ModeβRedlite enables SQLiteβs Write-Ahead Logging (WAL) mode:
- Concurrent reads while writing
- Better performance for mixed read/write workloads
- Crash recovery