Study guides · Intermediate

Consensus, Raft, CAP, and Sharding

Trace majority replication and partition behavior, then distinguish consensus from sharding and replication.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Replication and sharding solve different problems
  2. A majority of three is two
  3. Use Raft to agree on a log
  4. Make the partition trade-off concrete
  5. Replicate each of two shards independently
  6. Separate log agreement from one business operation
  7. Preserve reads and writes while changing ownership
  8. Solve the owner-count versus replica-count exercise
  9. Ask Logeia about this articleCheck your understanding

Replication and sharding solve different problems

Replication places copies of the same logical data on several nodes, supporting goals such as fault tolerance and distributed reads. Sharding divides responsibility for different data by a key or range rule. Distributing total data volume is different from agreeing on an order of changes among copies. A system can replicate each shard, combining both techniques. Key skew, cross-shard operations and moving data introduce additional coordination problems. Adding machines therefore does not automatically accelerate every operation. Start by identifying which nodes own distinct data and which nodes are meant to hold equivalent replicas.

A majority of three is two

In a three-node replica group, two nodes form a majority. Any two majorities intersect, a property used in consensus safety arguments. Merely writing to any two machines does not implement a correct consensus protocol, however. Terms, voting restrictions, log freshness, durable state and recovery behavior must work together. Progress depends on a communicating majority, not only a count of machines that have not crashed. Two surviving nodes separated from one another may be unable to make progress. State the fault model and protocol rules before claiming what one-node failure tolerance actually means.

Use Raft to agree on a log

Raft elects a leader for a term and replicates log entries through that leader. Its rules for committing entries from the current term on a majority are part of safe log advancement. A replica count for an arbitrary older-term entry is not interchangeable with that rule. Voting and log restrictions prevent a disconnected old leader from committing outcomes that conflict with a later legitimate leader. Reads also require care: returning an old leader’s local state without checking its authority does not automatically supply a linearizable read. Consensus is a set of coordinated invariants, not just a leader label and a majority count.

Make the partition trade-off concrete

During a partition, groups cannot observe one another’s updates. Continuing to answer every request while preserving the relevant single-copy consistency guarantee imposes an impossibility trade-off under CAP’s formal definitions. This is not a general slogan that any two of three desirable labels can always be chosen freely. A majority-based system may continue updates on one side while rejecting or delaying writes on the minority side. Designs allowing both sides to accept changes need explicit conflict-resolution and visibility rules after communication returns. Compare the actual operations and guarantees rather than assigning an entire product an unexplained consistency-versus-availability label.

Replicate each of two shards independently

Assign keys zero through 99 to S1 and one hundred through 199 to S2. Replicate S1 at A1,A2,A3 and S2 at B1,B2,B3. Key seventy has three S1 copies and key 150 three S2 copies. Two data responsibilities and three replicas per responsibility are distinct. Six physical servers do not automatically form one six-member consensus group; identify the history and membership being coordinated.

Partitioning broadly divides data and can occur within one database. Sharding commonly refers to distribution across owners, with product terminology still worth checking. Explain the actual key split and copy arrangement rather than relying on vocabulary alone. Both mechanisms can coexist. Drawing responsible ranges and replica sets side by side makes the practical distinction explicit.

KeyData ownerReplica set
70S1A1, A2, A3
150S2B1, B2, B3

Separate log agreement from one business operation

S1 can agree on a decrement log while a lost-response retry submitted as another operation creates a second agreed command. Raft orders commands; the application still needs to recognize one business operation. Processed identity and result can belong to the state machine and its snapshots.

An old leader also needs the selected read-authority procedure rather than merely claiming leadership. The detailed Raft article separates persistence, commitment, application and read conditions. Quorum overlap is useful mathematics, not a substitute for all protocol rules. Explain what the shared member accepts and remembers across restart. Those obligations connect membership arithmetic with an actual safety contract.

Preserve reads and writes while changing ownership

Moving part of S1 requires more than a new map. Copy data, retain live updates and deletions, catch up and transfer authority. Old-map retries must not let both owners commit independently. Generations and fencing need an enforcing storage boundary.

Range placement helps ordered queries but can concentrate monotonic writes. Consistent and rendezvous hashing can limit reassignment without replacing copy or consensus. The hash lab calculates ownership changes only. Draw partition, placement function, copy, authority and per-shard replication separately to identify what further execution evidence a live change requires. A successful mapping calculation is one component of that verification, not completion of every box.

If one S1 member and two S2 members stop, S1 retains two while S2 retains one. Three surviving servers overall do not give both three-member groups the same progress conditions. Count correct reachable members per history, not just powered servers across the system.

Stating whether the objective is capacity through partitioning or failure tolerance through replication helps prevent the counting error. Placement needs movement and skew checks; replication needs persistence and agreement checks. Both can affect ordinary workload resources, which remain part of a concrete implementation assessment.

Solve the owner-count versus replica-count exercise

For key seventy, analyze S1's history and three members rather than counting unrelated S2 copies toward its acknowledgment. Work spanning both shards introduces a separate distributed-transaction or compensation question.

If A1 cannot communicate with the other two S1 members, being alive does not authorize independent continuing commitment under the majority policy. Place the CAP question on that actual partition history. Distinguish member count, communication, log, placement and business scope before proceeding to the detailed readings.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 18

Choose a learning path