The Append-Only Log

Serialize commands to disk, rebuild the index on startup, and think about fsync.

Part 2 of 3Updated

Writes append a serialized command; they never seek. Sequential writes are what make this fast.

#[derive(Serialize, Deserialize)]
enum Command {
    Set { key: String, value: String },
    Remove { key: String },
}

The in-memory index maps each key to a byte offset in the log. get looks up the offset, seeks once, and deserializes one record. On startup you replay the log to rebuild the index.

Durability. A successful write only means the OS has your bytes, not the disk. If you need a write to survive power loss you must call file.sync_data(), and it is expensive. Most real stores batch: fsync once per group of writes. Decide explicitly which guarantee you’re offering β€” silently choosing the fast one is how data gets lost.