A single project that combines four core distributed-systems ideas into one coherent system, instead of four disconnected demos:
| Piece | What it does | Where |
|---|---|---|
| Raft consensus | Leader election + replicated log, from scratch | raft/node.py |
| Replicated KV store | The state machine Raft drives | raft/kv_store.py |
| CRDT text editor | Conflict-free concurrent document editing (RGA) | crdt/rga.py, crdt/document.py |
| Streaming broker | Kafka-style pub/sub (topics, partitions, offsets, consumer groups) | streaming/broker.py |
| Fault-tolerant scheduler | Background jobs, active only on the current Raft leader | scheduler/scheduler.py |
They aren’t bolted together — each one is used for the job it’s actually good at:
A real collaborative app (think Google Docs) needs exactly this mix: durable shared state (KV store) for things like document metadata and permissions, low-latency conflict-free sync (CRDT) for the actual typing, a broadcast layer (streaming) to fan edits out to every open client, and something running maintenance jobs (snapshotting, compaction, cleanup) that keeps working even if a machine dies. Four separate mini-projects would each be shallower; wiring them into one flow is what gives this the weight of a systems project rather than four algorithm demos.
pip install -r requirements.txt # just Flask; everything else is stdlib
python demo/run_cluster.py --nodes 5
This starts one broker process and five node processes on localhost
(see the port scheme printed on startup). Open two of the printed
URLs (e.g. http://127.0.0.1:9101/ and :9102/) in two browser tabs
– typing in one shows up in the other within about a second, synced
through the CRDT + streaming layer. The same page also has a small KV
read/write panel, and a status line showing this node’s Raft role.
To watch failover: find the current leader (the /status endpoint on
any node reports leader_id), kill that node’s process, and watch a
new leader get elected within ~1-3 seconds – the KV store keeps
accepting writes (against the new leader) and the scheduler’s jobs
resume on whichever node just took over.
No pytest dependency – everything runs on the standard library’s
unittest:
python -m unittest tests.test_raft -v # real 3-node cluster over real sockets:
# election, log replication, failover
python -m unittest tests.test_crdt -v # RGA convergence properties
python -m unittest tests.test_streaming -v # broker partitioning / consumer groups
python -m unittest tests.test_scheduler -v # leadership-gated job execution
test_raft.py is the one worth reading first if you want to see the
consensus algorithm actually exercised: it starts three real
RaftNodes listening on real TCP ports, asserts exactly one leader
gets elected, proposes a command and checks it replicates to all
three, then kills the leader process’s node and checks a new leader
gets elected in a higher term.
Every one of these was a deliberate scope cut for a project of this size, not an oversight – worth stating up front if asked:
Document.apply_remote buffers/retries any op that briefly arrives
before its anchor. A adversarial network could still violate this;
a production CRDT would use vector clocks or similar.The first version of the RGA CRDT used a flat list with a
scan-and-skip rule to place new characters. It passed every isolated
unit test. It only broke once the whole running cluster was
exercised: two browser sessions typing concurrently converged to two
different final strings on different nodes ("YoHi" on one replica,
"YHio" on another) – a genuine violation of the CRDT’s core
guarantee. The fix was structural: model the document as a proper
parent/children tree and take its pre-order traversal, so a
character’s position only ever depends on comparisons within its own
sibling group, never on what order operations arrived in. The
regression test for this exact scenario is in tests/test_crdt.py
(test_concurrent_two_char_inserts_converge_regardless_of_delivery_order).
Good story for a project report: unit tests alone didn’t catch it –
running the real distributed system did.
network/ shared async TCP JSON-RPC transport (Raft, broker, scheduler all use it)
raft/ RaftNode (election + log replication), KVStateMachine
crdt/ RGA (the CRDT algorithm), Document (bridges RGA <-> streaming broker)
streaming/ Broker (topics/partitions/offsets), BrokerServer, BrokerClient
scheduler/ Job, FaultTolerantScheduler (gated on Raft leadership)
server/ NodeRunner (wires everything together), Flask HTTP API, CLI entry point
demo/ local cluster launcher + browser demo page
tests/ unittest suite, including a real multi-node Raft integration test