Modelos de Consistencia

Fuerte frente a eventual · Linealizabilidad

Escrito por Darjus Kazakevic · revisado el

Pick the consistency model per operation, and put its name in the API.

Once data has more than one copy, “the value” depends on which copy you read and when. Linearizable (strong) means every read sees the latest write, as if there were one copy. It costs a coordination round trip. Eventual means the copies agree once writes stop. It is fast, but a user may not see their own write for a moment.

Most systems use the models in between, such as read-your-writes and monotonic reads. The mistake is to choose once for the whole system. A balance after a transfer needs read-your-writes; a view count does not. So the API must let callers ask for freshness, or they will assume it.

En esencia — Choose per operation how stale a read may be, from linearizable to eventual, and make that choice visible to the caller instead of hiding it.

Cuándo recurrir a él

Recurre a él cuando

  • Data has replicas or caches, so a read can land on a stale copy
  • Some operations need fresh reads, like a balance after a transfer
  • Designing an API whose callers have different freshness needs

Reconsidéralo cuando

  • A single-copy store, where every read already sees the latest write
  • Replicas serve only as failover standbys and never take reads
  • Data that never changes once written, like a stored image

Conceptos clave

Linearizable (Strong consistency)
Every read returns the latest completed write, and all clients see operations in one order. It needs one leader for the data, or consensus between replicas, so it costs latency and availability.
Eventual consistency
If writes stop, all copies end up with the same value. Until then, a read may return an older value. Typical for async replicas, caches and DNS.
Read-your-writes
A user always sees their own writes, even if others see them later. Read from the primary for a short time after a write, or pass the write's version with the next read.
Monotonic reads
A user never sees time go backwards: after seeing version 5, they never get version 4. Pin each user to one replica, or track the last version they saw.
Causal consistency
If one write caused another, everyone sees them in that order. A reply never appears before the comment it answers. Stronger than eventual, cheaper than linearizable.

Reglas de diseño

Choose per operation, not per system
A balance after a transfer needs read-your-writes; a view count is fine eventually. Take the strong, slow path only where the operation asks for it, and let everything else read a replica.
Put the model in the signature
A caller who cannot ask for freshness will assume it. Make consistency a parameter of the read, and return a version from each write so the caller can insist on seeing it.
Measure replica lag and alert on it
Eventual consistency has a number: how far behind the replicas are. Track it and alert when it passes what your reads can accept, such as one second.
Know your database's default
DynamoDB reads are eventually consistent unless you ask for strong ones. Postgres replicas are asynchronous by default. Check the default before you assume a read is fresh.

Errores habituales

  • A user saves a profile and the page reloads from a replica that is 2 seconds behind. The change looks lost, and the user saves it again.
  • Reading from a cache after a write without invalidating it. The cache returns the old value until its TTL expires.
  • Assuming eventual means within milliseconds. Under load or a network problem, replica lag can reach minutes.
  • Strong consistency everywhere, to be safe. Every read goes to the primary, which becomes the bottleneck.