The Log and Its Hash Index
How it works
- Write: append the key-value pair to the log file, then update the in-memory hash map with the new byte offset.
- Read: look up the key in the hash map (RAM),
seek()straight to that byte offset, read the value. - The payoff: if that region of the file is already in the OS page cache, the read touches no disk at all.
What is a "disk block," precisely?
A block (also called a sector or page, depending on which layer you're standing on) is the smallest unit the OS reads from or writes to disk at once — typically 4 KB.
- A file is a logical sequence of bytes; the OS maps it onto a set of physical disk blocks, which need not be contiguous.
- A file smaller than one block still occupies a whole block; a larger file spreads across many.
- So: one block → part of one file, and one file → usually many blocks.
- The page cache works at block granularity — reading a few bytes pulls the entire containing block into RAM, which is why nearby reads and re-reads are effectively free.
The four failures
| Problem | Why it happens | Fixed by |
|---|---|---|
| Disk grows forever | Old log entries are never deleted; an overwritten key keeps every prior version on disk. | Compaction (see Concurrency, tombstones and compaction, Compaction strategies) |
| Slow restart | The hash map lives only in RAM. Rebuilding it means a full scan of the entire log. | SSTable footers (see Concurrency, tombstones and compaction) |
| Bounded by RAM | Every key needs an entry. Spilling the map to disk is impractical — random I/O plus hash-collision handling. | Sparse indexes (see SSTables and sparse indexes) |
| No range queries | Hash maps have no order. keys between "aa" and "az" means looking up each one individually. | Sorted files (see SSTables and sparse indexes) |
The Invariants
- Guarantees: O(1) point lookups with at most one disk seek, and writes that stay purely sequential.
- Does not guarantee: Bounded RAM, bounded disk, fast restart, or any range-scan capability. All four are structural, not tuning problems.