Analyze consistency, sharding and partition trade-offs
Analyze consistency, sharding and partition trade-offs
Linearizability makes each completed operation appear to occur at a single point between invocation and response, respecting real-time order. Eventual consistency allows temporary disagreement but requires convergence under its stated conditions. Sharding partitions data by a key; replication copies data for availability or durability. During a network partition, a system cannot guarantee both linearizable operations and a successful response to every request reaching a nonfailed node.
package main
import ( "errors" "fmt")
func read(minVersion, localVersion int, connected bool) (int, error) { if localVersion >= minVersion { return localVersion, nil } if !connected { return 0, errors.New("cannot satisfy version") } return 0, errors.New("refresh required")}func main() { v, e := read(4, 3, false) fmt.Println(v, e) v, e = read(3, 3, false) fmt.Println(v, e)}The model implements a minimum-version read contract, not linearizability. A client carrying version 4 rejects a stale replica at version 3. Accepting version 3 for a weaker requirement improves availability but may return old data. Choose contracts per operation: inventory reservation may need stronger guarantees than a product description.
Expected output:
0 cannot satisfy version3 <nil>Partition keys predictably
package main
import ( "fmt" "hash/fnv")
func shard(key string, n uint32) uint32 { h := fnv.New32a() _, _ = h.Write([]byte(key)) return h.Sum32() % n}func main() { a := shard("customer-42", 4) fmt.Println(a < 4, a == shard("customer-42", 4)) fmt.Println(shard("customer-42", 1))}For positive shard counts, hash modulo n provides a stable mapping for an unchanged n. Changing n may move many keys, so rebalancing needs a migration protocol; consistent hashing or a shard directory can reduce movement. Hot keys can still overload one shard. Cross-shard operations need explicit consistency and failure handling; replicas do not eliminate partitions.
Expected output:
true true0Try it locally
Route nonnegative numeric IDs to three shards using modulo. Print shard numbers for IDs 0, 3 and 5. Explain why adding a fourth shard requires migration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distributed services 09: Analyze consistency, sharding and partition trade-offs
Check the lesson objective before moving on.
Is a minimum-version check equivalent to linearizability?
What can happen when changing hash modulo shard count?