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

Speaker 1Speaker 2

Data Consistency in Distributed Systems

9:13
0:00-9:13
  1. Corey

    Today we’re talking about consistency in distributed systems. You’ll hear the word in different contexts, including the “C” in the CAP theorem and the “C” in database transactions. Here, we’ll focus on consistency as it relates to copies of data: when a system stores multiple copies, consistency is the goal of keeping those copies in agreement.

  2. Anna

    Let’s start with the simplest version: a single server and one copy of the data. Imagine early Facebook serving students on one campus. A student creates a profile, it’s saved on the server, and another student asks to see it. The server looks up that profile and returns it. With just one copy, there’s no disagreement between copies to worry about.

  3. Corey

    But that simple setup has serious limits. First, the server is a single point of failure: if it goes down, the whole service can become unavailable, and if the data is lost, there’s no other copy to fall back on. Second, a single machine can only be scaled up so far. Buying a more powerful server gets increasingly expensive, and eventually one computer just can’t handle the load.

  4. Anna

    There’s also distance. If the server is in the United States and users are connecting from the United Kingdom, their requests have to travel farther through the network. That adds latency—the time it takes to get a response. So the one-server design can be unreliable, hard to scale, and slow for faraway users.

  5. Corey

    One way to spread the load is to put servers in different locations, perhaps one near Harvard and one near Oxford. If each server stores only the data relevant to its own campus, local students can get fast responses. But now Harvard users can’t readily see Oxford users’ data, and vice versa. We’ve made the system more local, but we haven’t built the sharing that makes a social network useful.

  6. Anna

    We could let a Harvard server contact Oxford’s server whenever a Harvard user asks for Oxford data. That enables sharing, but it brings back the distance problem for those requests. And if the Oxford server fails, access to data stored only there is still disrupted. Distributing servers doesn’t automatically solve every problem.

  7. Corey

    A cache can help with the distance. If Harvard users often ask for a particular Oxford profile, the Harvard server can fetch it once and keep a local copy. Later requests can be answered from that nearby cache instead of crossing the ocean each time.

  8. Anna

    But a cache isn’t a complete fix. The first request may be a cache miss, meaning the information isn’t there yet and the server has to fetch it remotely. The cached copy might also be replaced, so a later request can miss again. Caching reduces some delays, but it can’t guarantee a fast local response every time.

  9. Corey

    A broader approach is to keep multiple copies of data in different places. If a server fails, another location may still have a copy. And if a Harvard user needs Oxford data, a nearby copy can provide a quicker response. That can improve both resilience and latency.

  10. Anna

    The catch is that copies can get out of sync. Suppose a profile says “C,” and someone updates it to “C plus plus” at the U.S. location. Until that update reaches the European copy, one location says “C plus plus” and the other still says “C.” That’s an inconsistency. The central challenge is not just making copies; it’s keeping them aligned as changes happen.

  11. Corey

    For a product where updates are rare or where a delay is acceptable, the system might let copies catch up later. The example of bank records is that a change may be recorded first and reflected in a statement later; customers can even be told that a change may take a few days to appear. But that kind of delay may not suit a real-time service where users expect updates to show up quickly.

  12. Anna

    For faster propagation, one server can send an update to another over the network. The message might say, “Change C to C plus plus.” A protocol such as TCP can provide reliable, ordered delivery under ordinary operation, which is useful when the order and arrival of updates matter.

  13. Corey

    Still, networks and servers can fail. If the sender gets no response, it can’t know for sure whether the update failed to arrive, the receiving server failed, or the update arrived but the acknowledgment got lost. That’s why systems often use acknowledgments: the receiver confirms that it got the message and processed it.

  14. Anna

    And if no acknowledgment arrives, the sender can retry. It might retry a limited number of times and then report a failure, or keep retrying until it gets confirmation. Either choice has consequences: stopping can leave copies inconsistent, while retrying indefinitely can mean waiting a long time.

  15. Corey

    This points to a fundamental difficulty in distributed systems: communication alone can’t give two machines perfect certainty about what the other one knows. If one system commits an update and sends an acknowledgment, the acknowledgment itself might be lost. Sending an acknowledgment of that acknowledgment doesn’t end the problem; that message could be lost too. This is related to the Two Generals problem.

  16. Anna

    So if a system waits for confirmation before deciding what to do, it can get stuck waiting. But if it commits locally without knowing whether the other copy has committed, the copies may disagree. The network isn’t guaranteed to deliver every message, and that makes agreement harder than simply sending an update.

  17. Corey

    One way to simplify the design is to assign a leader. The leader is the only server allowed to accept writes; the other servers are followers. A client sends an update to the leader, and the leader sends it onward to the followers. That gives the system one designated place where changes begin.

  18. Anna

    But having a leader doesn’t by itself make every read consistent. If the leader has updated a value and a follower hasn’t received the update yet, a read from that follower could return the old value. The system has to decide whether to wait for the follower to catch up or answer immediately with data that might be stale.

  19. Corey

    That illustrates the trade-off: if you insist that reads never expose conflicting copies, you may have to wait when a server or network connection is unavailable. If you answer despite missing updates, the service can remain available, but a user may temporarily see older data. Strong consistency can therefore come at a cost in availability and performance.

  20. Anna

    A more coordinated approach for a leader and several followers is two-phase commit. Think of the leader coordinating a transaction across multiple systems. A transaction might contain several statements, and the database can record those operations without making them visible as final changes yet.

  21. Corey

    In the first phase, the leader sends a prepare request. Each follower performs the required work and replies to say it is ready to commit—or reports that it can’t proceed. “Prepared” means the follower has done its part but is waiting for the leader’s final decision; it hasn’t yet made the transaction final.

  22. Anna

    If the leader receives the required acknowledgments, it sends a commit decision. The followers then commit the prepared transaction, so the update becomes part of the data they serve. If a follower can’t prepare, the leader can instead abort the transaction, and the work that was only prepared is rolled back.

  23. Corey

    That distinction matters: a follower shouldn’t decide on its own to roll back just because it hasn’t heard from the leader for a while. It needs to follow the coordinator’s decision, because the leader may already have decided to commit. If the leader or network fails at the wrong moment, participants can be left waiting for the decision to be resolved. Two-phase commit coordinates agreement, but it doesn’t make failures disappear.

  24. Anna

    There’s another failure case: a follower may receive a commit message, commit successfully, and then lose its acknowledgment on the way back. The leader may retry the commit because it doesn’t know the follower completed it. A transaction ID helps here: the follower can recognize that the repeated commit is for the same transaction and avoid applying the operation twice. That property is called idempotence.

  25. Corey

    To prevent users from seeing a mix of old and new values while the transaction is being coordinated, a system may block reads or writes for the affected data, for example by holding locks. That helps preserve consistency, but users may have to wait rather than get an immediate response. In the strongest version of this design, consistency is prioritized even when it hurts availability.

  26. Anna

    So the big lesson is that replication brings real benefits: it can help with failures, geography, and response time. But once there are multiple copies, every update raises the question of how and when the copies will agree. Coordination mechanisms like a leader and two-phase commit can make agreement stricter, but they bring costs in waiting, performance, and availability.

  27. Corey

    And that’s why distributed systems don’t always choose the strictest possible consistency. A common alternative is eventual consistency: copies may temporarily differ, but the system aims for them to converge if updates stop and communication succeeds. It’s an important next topic because it explores a different balance between keeping data aligned and keeping the service responsive.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent