Distributed Systems¶
happy-simulator includes full implementations of distributed systems algorithms for simulation and educational use.
Consensus¶
Raft¶
from happysimulator import RaftNode, Network, datacenter_network
nodes = [RaftNode(f"node-{i}") for i in range(5)]
network = Network(name="cluster")
# Add links between all nodes...
# Raft handles leader election, log replication, and membership changes
See examples/distributed/raft_leader_election.py for a complete example.
Paxos¶
PaxosNode— single-decree PaxosMultiPaxosNode— multi-decree with stable leaderFlexiblePaxosNode— configurable quorum sizes
Leader Election¶
from happysimulator import LeaderElection, BullyStrategy, RingStrategy, RandomizedStrategy
election = LeaderElection(
name="election",
strategy=BullyStrategy(),
network=network,
)
Distributed Locks¶
from happysimulator import DistributedLock
lock = DistributedLock(name="mutex", nodes=nodes, network=network)
# Acquire returns a LockGrant with fencing token
grant = yield lock.acquire()
# ... critical section ...
grant.release()
Failure Detection¶
from happysimulator import PhiAccrualDetector
detector = PhiAccrualDetector()
detector.heartbeat(timestamp)
phi = detector.phi(now) # suspicion level (>8 usually means dead)
Membership¶
from happysimulator import MembershipProtocol
protocol = MembershipProtocol(name="swim", nodes=nodes, network=network)
# SWIM protocol for scalable failure detection and membership
CRDTs¶
Conflict-free Replicated Data Types for eventual consistency:
from happysimulator import GCounter, PNCounter, LWWRegister, ORSet, CRDTStore
counter = GCounter("node-1")
counter.increment(5)
counter.merge(remote_counter.state())
store = CRDTStore(name="store", node_id="node-1", network=network)
Replication¶
- Primary-backup replication
- Multi-leader replication
- Chain replication
See examples/distributed/ for complete examples of each pattern.
Next Steps¶
- Networking — network topology and partitions
- Clocks — time skew and logical ordering
- Fault Injection — systematic failure testing