ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/store/tasks/task-7-5-snapshots
TASK

Implementation

Implement log compaction with snapshots:

  1. Periodically snapshot state machine state
  2. Record snapshot index and term
  3. Discard log entries before snapshot
  4. On recovery, restore from snapshot then replay log
  5. Send InstallSnapshot to followers that are too far behind

This prevents unbounded log growth.

Sample Test Cases

Create snapshotTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c0","dest":"n1","body":{"type":"seed_state","msg_id":2,"state":{"x":1,"y":2},"commit_index":5,"term":2}}
{"src":"c0","dest":"n1","body":{"type":"take_snapshot","msg_id":3,"up_to_index":5}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"seed_state_ok","in_reply_to":2,"msg_id":1}}
{"src":"n1","dest":"c0","body":{"type":"snapshot_ok","in_reply_to":3,"msg_id":2,"last_included_index":5,"last_included_term":2}}

Hints

Hint 1
Snapshot state machine periodically
Hint 2
Discard log entries before snapshot
Hint 3
Send snapshot to slow followers
OVERVIEW

Theoretical Hub

Log Compaction

The Raft log grows forever as commands arrive. Snapshots compress applied log entries into a compact state representation. Only the snapshot and subsequent log are needed for recovery.

InstallSnapshot RPC

When a follower is so far behind that leader discarded needed entries, send the snapshot instead. The follower replaces its state with the snapshot and resumes from there.

Key Concepts

snapshotlog compactionrecovery
main.py
python
Implement Log Compaction with Snapshots - The Store | Build Distributed Systems