Durability: Crash-Safe Writes & Atomic Operations โ
FLASH DB ensures data durability through configurable WAL fsync and atomic SSTable operations.
Architecture โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Memory (MemTable) โ
โ Read + Write Cache โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ flush()
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Persistent Storage (SSTable) โ
โ Write-Ahead Log (WAL) + Sorted Run โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโWAL (Write-Ahead Log) โ
Every mutation goes through the WAL before hitting memory:
Client Request โ WAL Frame(s) โ fsync (mode-dependent) โ MemTable Update โ ResponseDurability Modes (v1.2.5+) โ
| Mode | Behavior | Default |
|---|---|---|
strict | fsync after every frame | โ |
balanced | Batch fsync every 64 ops or 25 ms; full sync on close() | โ |
throughput | No auto-fsync until close() | โ |
js
import { FlashClient } from "flash-zk";
const client = new FlashClient({
storagePath: "./flash_data",
engineOptions: { durability: "strict" }, // max safety
});
// Low-level WAL
import { FlashArc } from "flash-zk/engine/arc.mjs";
const wal = new FlashArc("./data/commit.farc", { durability: "balanced" });See Engine Options for full tuning.
Crash Recovery โ
On startup, WAL records are replayed to restore in-memory state:
- WAL file is read sequentially
- Each frame is deserialized (opcode + key + data)
- MemTable is reconstructed from valid frames
- Corrupt or truncated frames are skipped with a warning
js
// WAL recovery is automatic on collection.init()
await collection.init();Atomic SSTable Writes โ
SSTables are written atomically using a temp-file โ rename pattern:
1. Write to .tmp file
2. fsync the .tmp file (ensure data on disk)
3. fsync the parent directory (ensure rename is durable)
4. Rename .tmp โ final name
5. fsync parent directory againThis guarantees that either:
- The complete SSTable exists after a crash, or
- The old SSTable is intact (no partial writes)
Durability Guarantees โ
| Scenario | Data Safe? | Recovery |
|---|---|---|
| Crash after WAL fsync / close() | โ Yes | WAL replay |
| Crash during SSTable flush | โ Yes | Atomic rename |
| Crash during WAL append (balanced) | โ ๏ธ Last batch may replay | WAL replay |
| Crash in throughput mode before close | โ ๏ธ Unsynced frames lost | Partial replay |
| Power loss mid-write (strict mode) | โ Yes | fsync ensures disk write |
Performance Tuning โ
js
// Bulk import โ call close() to flush
engineOptions: {
durability: "throughput";
}
// Production default
engineOptions: {
durability: "balanced";
}WARNING
throughput mode may lose recent WAL frames on crash. Use only for disposable data or when you explicitly call close() before shutdown.