Junior implementation of a Write-Ahead Log (WAL) in Java.
Append-only logs are the foundation of modern databases and distributed systems, ensuring durability, recovery, and sequential I/O efficiency.
This project started as a simple LogFile class (append + read), then evolved into a more production-like design inspired by LevelDB, RocksDB, and Kafka.
- Append-only writes (
append(record)) and sequential reads. - Binary record format:
[len:4B][data:len][crc:4B]. - CRC32 checksums for corruption detection.
- Page buffering with padding (configurable size).
- Configurable fsync policy (always / never / every N / time-based).
- Record fragmentation across pages (FULL/FIRST/MIDDLE/LAST).
- Recovery scan: detects incomplete/corrupt pages and truncates safely.
- Segment rotation with
wal-000001.log,wal-000002.log, etc. - Iterator API for log replay.
Write-Ahead Logs are used in:
- Databases (Postgres, MySQL/InnoDB, FoundationDB, LevelDB, RocksDB)
- Distributed consensus (Raft, Paxos)
- Event streaming systems (Kafka, Pulsar)
Understanding WAL internals builds strong foundations for storage engines, transaction systems, and distributed databases.
// Create log
LogFile log = new LogFile("mylog.bin");
log.append(new Record("hello".getBytes()));
log.append(new Record("bye".getBytes()));
log.close();
// Replay log
LogFile reader = new LogFile("mylog.bin");
Record rec;
while ((rec = reader.readNext()) != null) {
System.out.println(rec);
}
reader.close();