1. Home
  2. Explore
  3. Computer Science
  4. Data Consistency in Distributed Systems

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Data Consistency in Distributed Systems

Consistency in Distributed Systems

What consistency means

Consistency concerns whether multiple copies of data agree. If a system stores the same record in several places, consistency means those copies reflect the same information according to the system’s rules.

For example, if a user’s profile is stored on servers in both the United States and Europe, a change made to the profile should eventually—or, under stricter guarantees, immediately—be reflected in both copies.

Consistency is important in several areas of computing:

  • In distributed systems, it concerns agreement among replicated data.

  • In the CAP theorem, consistency is one of the properties considered alongside availability and partition tolerance.

  • In database transactions, consistency refers to maintaining valid data as transactions are applied. This is related to, but not identical to, the distributed-systems question of keeping replicas in agreement.


Why use more than one server?

A single server is simple: users send requests to it, and it reads or updates the one copy of the data. But this design has important limitations.

Single point of failure

If the server or its network connection fails, the entire service may become unavailable. If the server’s storage is lost and there is no backup, the data may also be lost. A system that depends on one component in this way has a single point of failure.

Limits of vertical scaling

A system can initially handle more users by giving its server more capacity, such as more processing power, memory, or storage. This is called vertical scaling.

Vertical scaling has practical limits:

  • Powerful machines become increasingly expensive.

  • A single machine can only support a finite amount of work.

  • Eventually, the system may need more capacity than one machine can provide.

High latency for distant users

Users far from the server may experience slow responses because requests must travel over long distances and through network equipment. Adding capacity to the server does not remove this geographic delay.

These problems motivate distributing data and processing across multiple servers.


Distribution, partitioning, caching, and replication

These approaches can improve a system, but they have different effects on data availability and consistency.

Partitioning data by location

A system might store data relevant to one region on a server in that region. For example, one server could serve users at a university in the United States while another serves users in Europe.

This can reduce local workload and improve response times for users accessing their region’s data. However, if the servers do not share data, users cannot freely access information stored in another region. Partitioning alone also does not remove the risk that a particular server will fail and make its data inaccessible.

Accessing data across regions

A regional server can forward a request for data it does not have to the server that stores that data. This enables sharing, but the request still travels over a long distance. The system therefore retains the latency problem, and the remote server may remain a single point of failure for its data.

Caching remote data

A regional server can keep a cache, or temporary local copy, of frequently requested data from another region. When the data is already in the cache, the server can respond quickly without contacting the remote server.

A typical cache lookup works as follows:

  1. A user requests a record.

  2. The local server checks whether the record is in its cache.

  3. If it is present, the local server returns it.

  4. If it is absent—a cache miss—the server retrieves it from the server that holds the data, stores a copy locally, and returns it.

Caching can reduce latency, but it does not eliminate remote requests. A cache miss requires contacting the remote server, and cached data may need to be replaced when cache space is limited. The cached copy can also become outdated if the original record changes and the update has not yet reached the cache.

Replicating data

With replication, the system deliberately stores multiple copies of the same data on different servers. This can improve both availability and response time:

  • If one server fails, another may still have a usable copy.

  • Users can often read from a nearby server rather than contacting a distant one.

Replication also creates the central consistency challenge: when one copy changes, the other copies must be updated. Until those updates are applied, different servers may return different values.


Propagating updates between replicas

Suppose a record initially contains the value “C.” A user changes it to “C++” on one server. The other servers must learn about and apply the update if the replicas are to agree.

One basic approach is to send an update message over the network. A reliable transport protocol such as TCP can help provide reliable, ordered delivery of data between connected endpoints. However, a distributed application still needs to handle failures such as server crashes, lost connections, or a recipient that is unavailable.

Acknowledgements and retries

A sender can ask the receiving server to acknowledge an update. The acknowledgement gives the sender evidence that the receiver processed the message. If no acknowledgement arrives, the sender may retry.

Retries introduce further questions:

  • How long should the sender keep retrying?

  • Should it retry indefinitely or stop after a limit?

  • What should the system do if the receiver processed the update but its acknowledgement was lost?

  • Can applying the same update more than once cause unwanted effects?

A common safeguard is to identify an operation with a unique transaction ID. If a duplicate message arrives with an ID that has already been processed, the receiver can recognize it and avoid applying the operation a second time. This is an example of idempotent handling: repeating an operation does not produce additional unintended effects.

Acknowledgements and retries improve reliability, but they cannot make communication certain. In a distributed system, a sender may be unable to distinguish between a failed message, a failed recipient, and a delayed acknowledgement.

The Two Generals problem

The Two Generals problem illustrates the limits of coordination over an unreliable communication channel. Two parties can exchange messages and acknowledgements, but each party can remain uncertain whether the other received the latest message. Sending an acknowledgement of an acknowledgement does not eliminate the uncertainty; the final acknowledgement may also be lost.

The practical lesson is that a system cannot base certainty on the hope that a final message or acknowledgement will definitely arrive. Distributed systems need explicit strategies for handling uncertain communication and failures.


Leader–follower replication

One way to simplify updates is to assign different roles to the servers:

  • The leader is the server that accepts writes.

  • Followers receive updates from the leader and can often serve reads.

A write request goes to the leader, which applies the update and sends it to its followers. Restricting writes to one server gives the system a clear source of update order and reduces the chance that separate servers independently accept conflicting changes.

However, the replicas can temporarily disagree. If the leader has applied an update but a follower has not yet received it, a read from that follower may return an older value.

The system must choose how to handle this situation:

  • If it returns the follower’s old value immediately, the read may be stale.

  • If it waits for the follower to catch up before responding, the read may be delayed or unavailable when communication is failing.

Thus, stronger guarantees about what reads can observe can come at the cost of waiting and reduced availability.


Two-phase commit

Two-phase commit, often abbreviated 2PC, is a coordination protocol used to make a transaction’s outcome consistent across multiple participants. A participant might be a database server or another service that must take part in the operation.

The protocol has a coordinator, often called the leader, and one or more participants.

Phase 1: Prepare

  1. The coordinator sends each participant a prepare request.

  2. Each participant performs the transaction’s required work without yet making the result final.

  3. Each participant records enough information to support either committing or aborting the transaction.

  4. Each participant replies with a vote: it is ready to commit, or it cannot proceed.

A participant that votes “ready” must be prepared to follow the coordinator’s eventual decision. It generally holds relevant locks or other resources while waiting.

Phase 2: Commit or abort

  • If all participants vote that they are ready, the coordinator decides to commit and sends a commit message.

  • If a participant cannot prepare, or the coordinator decides the transaction must not proceed, the coordinator sends an abort message.

  • Participants apply the decision and acknowledge it.

A transaction ID helps participants recognize repeated commit or abort messages. If an acknowledgement is lost, the coordinator can retry the decision message without treating the repeated message as a new transaction.

Why prepare is different from commit

The prepare phase does not make the transaction’s changes final. It establishes that participants are ready to commit if instructed. The commit decision is a separate step that makes the prepared changes take effect.

This distinction allows the coordinator to choose a common outcome for all participants rather than having each server independently decide whether to keep its changes.

Failure and blocking

Two-phase commit depends on coordination and communication. A participant may be prepared while waiting for the coordinator’s decision. If the coordinator fails or messages are delayed at that point, the participant may not know whether to commit or abort.

It is therefore not generally safe for a participant to decide by itself to roll back merely because a timer expires: the coordinator may already have decided to commit, and other participants may be committing. Instead, systems use durable logs, recovery procedures, and retries to discover and apply the transaction’s decision.

This uncertainty can cause blocking. Participants may have to keep locks and delay reads or writes until they learn the outcome. If a server cannot safely return either the old or new value, requests involving the affected data may have to wait.


Consistency, availability, and performance trade-offs

Strict coordination can keep replicas from exposing conflicting transaction outcomes, but it has costs:

  • Availability: Requests may have to wait or fail when a server or network link is unavailable.

  • Performance: Coordination adds network round trips and can increase response times.

  • Resource use: Locks and prepared transactions may hold resources while the system waits for a decision.

  • System cost: Supporting replication and coordination requires additional infrastructure and operational complexity.

A system that waits for all necessary acknowledgements before allowing reads to proceed can offer stronger consistency, but it may be unavailable for those operations during a failure or delay. A system that continues responding from a replica that has not yet received an update can remain more available, but it may return stale data.

The appropriate choice depends on what the application can tolerate. Financial systems may favor strict coordination when inconsistent outcomes would be harmful. Other applications may prefer faster responses and continued service, even if some reads temporarily reflect older data.


Eventual consistency

Eventual consistency is an alternative to requiring every replica to agree immediately. It allows replicas to differ temporarily while updates are being propagated. If updates stop and the servers can communicate successfully, the copies are expected to converge to the same value.

This approach can improve responsiveness and availability, but users may temporarily see different versions of the same data depending on which server answers their request. It is useful when temporary disagreement is acceptable, but less suitable when every operation must immediately observe a single agreed-upon state.

The central design decision in distributed data systems is how much consistency an application requires and what delays, failures, and resource costs it is willing to accept in return.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent