Distributed state and consistency
Quick answer
Distributed state lives across processes or machines. Replicas may temporarily disagree, so the application must choose what freshness and ordering guarantees it requires.
Versions and conflicting updates
A stale read can occur when a replica has not received a recent write. A conditional update using an expected version detects a conflicting write at an authoritative store if the check and update are atomic. Eventual convergence requires delivery and conflict handling; local counters are not a distributed consensus protocol. Under network partition, a system cannot guarantee both linearizable operations and successful responses to all requests.
struct Item { value: u32, version: u64 }impl Item { fn set(&mut self, expected: u64, value: u32) -> bool { if self.version != expected { return false; } let Some(next) = self.version.checked_add(1) else { return false; }; self.value = value; self.version = next; true }}fn main() { let mut item = Item { value: 0, version: 1 }; println!("{}", item.set(1, 8)); println!("{}", item.set(1, 9)); println!("{} {}", item.value, item.version);}Expected stdout (not executed):
truefalse8 2The second writer holds an outdated version. A database implementation must perform an atomic compare-and-update.
fn main() { let required = 5u64; for replica in [4, 5, 6] { println!("{}", replica >= required); }}Expected stdout (not executed):
falsetruetrueA version threshold is meaningful only within the same authoritative version history.
Common mistake
Do not use timestamps alone as a universal conflict resolver; clocks can differ and concurrent writes can be legitimate.
Try it yourself
Explain how a client should handle a rejected conditional update without silently overwriting another user's change.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distributed state and consistency — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Two clients read version 7 and both propose updates. How should an authoritative store prevent a silent lost update?
Distributed state and consistency — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust struct Item { value: u32, version: u64 } impl Item { fn set(&mut self, expected: u64, value: u32) -> bool { if self.version != expected { return false; } let Some(next) = self.version.checked_add(1) else { return false; }; self.value = value; self.version = next; true } } fn main() { let mut item = Item { value: 0, version: 1 }; println!("{}", item.set(1, 8)); println!("{}", item.set(1, 9)); println!("{} {}", item.value, item.version); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Distributed state and consistency — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let required = 5u64; for replica in [4, 5, 6] { println!("{}", replica >= required); } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.