Docsity

Distributed Systems Notes, Summaries of Computer Science

Toggle sidebar
bg1
CS 6450 - Distributed Systems | Lecture notes (wk 5 + 6)
Topics: consensus, Paxos, Raft, fault tolerance, CAP
1. Consensus Problem
Fundamental problem in distrib. computing. Given a set of processes, each w/ an initial value, they all need to
AGREE on a single output value.
Formal properties (from lecture):
- Termination โ€“ every correct process eventually decides
- Agreement โ€“ no two correct processes decide differently
- Validity (aka non-triviality) โ€“ decided value must be proposed by some process
FLP Impossibility (Fischer, Lynch, Paterson 1985): no deterministic algorithm can solve consensus in an async
system if even ONE process can fail (crash-stop). This is HUGE. Means we have to relax something - either
async assumption OR fault model OR one of the properties above.
Practical systems get around FLP by:
- using timeouts (partial synchrony model) - assume msgs delivered w/in unknown-but-finite time delta
- randomization - not deterministic anymore, so FLP doesn't apply directly
- relaxing consistency (eventual consistency, etc.)
1.1 Failure Models
Different types, in order of increasing badness:
- Crash-stop: process stops, never sends another msg. Simplest.
- Crash-recovery: can come back, but might've lost state. Need stable storage.
- Omission: process sends/receives some msgs but drops others
- Byzantine: process can behave ARBITRARILY - send wrong msgs, lie, etc.
Note: most practical systems (ZooKeeper, etcd, etc.) only handle crash-stop. BFT is way more expensive - need
3f+1 nodes to tolerate f byzantine faults vs just 2f+1 for crash-stop.
??? -> ask prof: is there a clean proof for the 3f+1 bound?
2. Paxos
Lamport's algorithm. Originally described in "The Part-Time Parliament" (1989, but not published until 1998 bc
reviewers didn't like the Greek parliament story lol)
pf3
pf4
pf5

Partial preview of the text

Download Distributed Systems Notes and more Summaries Computer Science in PDF only on Docsity!

CS 6450 - Distributed Systems | Lecture notes (wk 5 + 6) Topics: consensus, Paxos, Raft, fault tolerance, CAP

1. Consensus Problem

Fundamental problem in distrib. computing. Given a set of processes, each w/ an initial value, they all need to AGREE on a single output value. Formal properties (from lecture):

  • Termination โ€“ every correct process eventually decides
  • Agreement โ€“ no two correct processes decide differently
  • Validity (aka non-triviality) โ€“ decided value must be proposed by some process FLP Impossibility (Fischer, Lynch, Paterson 1985): no deterministic algorithm can solve consensus in an async system if even ONE process can fail (crash-stop). This is HUGE. Means we have to relax something - either async assumption OR fault model OR one of the properties above. Practical systems get around FLP by:
  • using timeouts (partial synchrony model) - assume msgs delivered w/in unknown-but-finite time delta
  • randomization - not deterministic anymore, so FLP doesn't apply directly
  • relaxing consistency (eventual consistency, etc.)

1.1 Failure Models

Different types, in order of increasing badness:

  • Crash-stop: process stops, never sends another msg. Simplest.
  • Crash-recovery: can come back, but might've lost state. Need stable storage.
  • Omission: process sends/receives some msgs but drops others
  • Byzantine: process can behave ARBITRARILY - send wrong msgs, lie, etc. Note: most practical systems (ZooKeeper, etcd, etc.) only handle crash-stop. BFT is way more expensive - need 3f+1 nodes to tolerate f byzantine faults vs just 2f+1 for crash-stop. ??? -> ask prof: is there a clean proof for the 3f+1 bound?

2. Paxos

Lamport's algorithm. Originally described in "The Part-Time Parliament" (1989, but not published until 1998 bc reviewers didn't like the Greek parliament story lol)

Three roles:

  • Proposer - proposes values
  • Acceptor - votes on proposals
  • Learner - learns the decided value (One process can play multiple roles in practice)

2.1 Single-Decree Paxos (Basic Paxos)

Two phases: Phase 1 (Prepare/Promise):

  • Proposer picks a proposal number n (must be unique & higher than any it's used)
  • Sends Prepare(n) to majority of acceptors
  • Acceptor responds w/ Promise(n, v_prev, n_prev) where v_prev is the value from the highest-numbered proposal it's already accepted (or null). Also promises not to accept anything w/ # < n Phase 2 (Accept/Accepted):
  • If proposer gets promises from a majority:
  • If any acceptor sent back a prev value, proposer MUST use the value w/ the highest n_prev
  • Otherwise, proposer can use its own value
  • Proposer sends Accept(n, v) to majority
  • Acceptor accepts if n >= highest promised n Key insight: any two majorities overlap in at least one node. So the "highest prev accepted value" constraint ensures we never pick a conflicting value across rounds. TODO: re-read the proof of safety - i kind of followed it in lecture but wasn't 100%

2.2 Multi-Paxos

Basic Paxos only decides one value. Real systems need a sequence of decisions (replicated log). Multi-Paxos optimizes: elect a stable leader, skip Phase 1 for subsequent slots (as long as leader doesn't change). Problem: lots of underspecified details in original paper - leader election, log gaps, state transfer for new replicas. This is why Raft was invented.

2.3 Paxos Variants (briefly)

  • Fast Paxos - proposers can go directly to acceptors in 1 round when no conflicts
  • Cheap Paxos - uses fewer "permanent" acceptors, cheaper in the common case
  • Byzantine Paxos (PBFT-like) - handles BFT, much more complex

3. Raft

Ongaro & Ousterhout 2014. Goal: understandability. Decomposes into 3 subproblems: leader election, log replication, safety. Key structural choices vs Paxos:

  • Strong leader - all writes go through leader, simplifies reasoning
  • Randomized timeouts for election (150-300ms) instead of explicit prepare phase
  • Log entries only flow from leader to followers, never the other way

3.1 Leader Election

Terms - monotonically increasing integers. Each term starts w/ an election. States: Follower -> Candidate -> Leader (or back to Follower)

  • Follower: passive, responds to RPCs. If timeout w/o hearing from leader -> becomes candidate
  • Candidate: sends RequestVote RPCs to all nodes. Needs majority to win.
  • Leader: sends heartbeats (empty AppendEntries) to prevent new elections Vote granted iff: (1) candidate's term >= voter's current term, AND (2) candidate's log is at least as "up-to-date" as voter's. Up-to-date = higher last term, or same last term but longer log. <- this is the "election restriction" that guarantees the elected leader has all committed entries

3.2 Log Replication

Leader receives client request -> appends to local log -> sends AppendEntries to followers -> once majority acknowledge, marks entry as committed -> applies to state machine -> responds to client AppendEntries also includes prevLogIndex and prevLogTerm for consistency check. Follower rejects if its log doesn't match at that point. Leader then decrements nextIndex and retries (log matching property ensures convergence). Committed = safely replicated on majority. NEVER overwritten. Raft guarantees committed entries appear in all future leaders' logs.

3.3 Safety

State Machine Safety: if a server has applied a log entry at index i, no other server applies a different entry at index i.

Raft never commits entries from prev terms by counting replicas - only commits entries from CURRENT term by counting, which implicitly commits all earlier entries (Figure 8 in paper shows why this matters - tripped me up on hw3)

3.4 Log Compaction / Snapshots

Log grows forever otherwise. Solution: snapshots. Server periodically snapshots state machine state + last included index/term. Discards log up to that point. Leader can send InstallSnapshot RPC to lagging followers. Snapshot includes enough info to rebuild membership config too.

4. CAP Theorem

Brewer 2000 (conjecture), proved by Gilbert & Lynch 2002. In a distributed system, can have at most 2 of 3:

  • C - Consistency (every read gets most recent write, or error)
  • A - Availability (every req receives a non-error response)
  • P - Partition tolerance (system operates despite network partitions) Catch: P isn't really optional in a real distributed system - network partitions happen, you have to handle them. So really it's C vs A during a partition. CP systems: ZooKeeper, etcd, HBase, Spanner (kinda). Will reject reads/writes to maintain consistency during partition. AP systems: CouchDB, Cassandra, DynamoDB (default). Will return potentially stale data to stay available. CAP is often misunderstood/overused. PACELC is a better model: during (P)artitions choose A vs C, (E)lse choose L(atency) vs C. Most interesting trade-offs are in the non-partition case! Note to self: review consistency model hierarchy for the final - linearizability > sequential > causal > eventual is NOT exactly right, they're not totally ordered

Problem: coordinator failure. If coord crashes AFTER sending prepare but BEFORE sending commit, participants are STUCK (they voted yes and can't release locks). This is the "blocking" problem. 3PC (three-phase commit) tries to fix this by adding a pre-commit phase. Non-blocking in some failure scenarios but not network partitions. Rarely used in practice. Modern approach: Paxos/Raft-based transaction managers (coordinator itself is replicated). Spanner does this w/ Paxos groups per shard + 2PC across shards.

7. Vector Clocks & Logical Time

Lamport clocks: each process maintains a counter. On event, increment. On send, piggyback clock. On receive, take max of local + received, then increment. Problem: Lamport clocks only capture -> (happened-before), NOT the reverse. a->b implies L(a) b iff V(a) < V(b) (componentwise <=, at least one strict)

  • a || b (concurrent) iff neither V(a) <= V(b) nor V(b) <= V(a) Space: O(N) per message. For large systems, use version vectors w/ bounded size or approximate techniques (Dynamo uses vector clocks but truncates them when too long - ???) Key papers: Lamport 1978 (clocks), Fischer et al 1985 (FLP), Lamport 1989/98 (Paxos), Brewer 2000 (CAP), Gilbert+Lynch 2002 (CAP proof), Ongaro+Ousterhout 2014 (Raft), Attiya et al on linearizability Exam probably covers: FLP statement, Paxos phases, Raft election restriction, CAP trade-offs, consistency model definitions