Skip to content

fmartinez09/java-wal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Java Append-Only Log (WAL)

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.


✨ Features

  • 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.

πŸ“– Why?

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.


πŸš€ Quick Start

// 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();

πŸ“Š Diagram

WAL Diagram

About

Java Append-Only Log

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages