What are the core principles of distributed systems (CAP, consistency, consensus)?
Definition. A distributed system is several machines cooperating over an unreliable network. Its defining feature is *partial failure*: some parts fail while others keep running.
Key concepts
- CAP theorem — during a network partition a system must choose between consistency (every read sees the latest write) and availability (every request gets an answer). PACELC adds that, even without a partition, you trade latency against consistency.
- Consistency models — strong (linearizable), sequential, causal and eventual; choose the weakest model the business rule tolerates.
- Replication — leader/follower, multi-leader or leaderless; quorum reads and writes (R + W > N) tune the trade-off.
- Consensus (Raft, Paxos) — lets nodes agree on a value or leader despite failures; underpins coordination services and many databases.
- Time and ordering — physical clocks drift; use logical or vector clocks, or database-assigned sequence numbers, for ordering.
- The eight fallacies — the network is *not* reliable, latency is not zero, bandwidth is not infinite, topology changes, and so on.
Practice: use timeouts, idempotency and retries with backoff; avoid distributed transactions where a saga or outbox suffices; prefer a single well-run database until measurements say otherwise.