Consistency Models — How Fresh Is Your Read?
Defines how up-to-date reads are relative to writes — from strong (always latest) to eventual (converges eventually).
When to use
- Strong consistency: financial transactions, inventory counts, anything where stale reads cause harm
- Eventual consistency: social feeds, shopping carts, analytics — staleness is acceptable
- Causal consistency: collaborative editing, messaging — you must see your own writes
Tradeoffs
- Strong requires coordination across replicas (latency cost per write)
- Eventual requires conflict resolution strategy (complexity on read)
- Go
- Python
// Read-your-writes: route reads to primary after a write
func (c *Client) UpdateUser(ctx context.Context, user User) error {
if err := c.primary.Write(ctx, user); err != nil {
return err
}
// Pin next read to primary to see our own write
c.session.SetReadPreference(Primary)
return nil
}
func (c *Client) GetUser(ctx context.Context, id string) (User, error) {
pref := c.session.ReadPreference()
if pref == Primary {
c.session.SetReadPreference(SecondaryPreferred) // reset after use
return c.primary.Read(ctx, id)
}
return c.replica.Read(ctx, id)
}
class UserClient:
def __init__(self, primary, replica):
self.primary = primary
self.replica = replica
self._read_from_primary = False
def update_user(self, user: dict) -> None:
self.primary.write(user)
# Pin next read to primary (read-your-writes)
self._read_from_primary = True
def get_user(self, user_id: str) -> dict:
if self._read_from_primary:
self._read_from_primary = False # reset after use
return self.primary.read(user_id)
return self.replica.read(user_id)
Gotcha: Eventual consistency does NOT mean random. It means replicas converge if writes stop. The window can be milliseconds or minutes depending on topology.