Grokking the System Design Interview

I Wish I Knew These Data Consistency Concepts Before the System Design Interview

Data Consistency in Distributed Databases: Challenges and Solutions.

Arslan Ahmad
Level Up Coding
Published in
9 min readMay 7, 2024

--

In today’s interconnected world, distributed databases are fundamental to many of the services we use daily, from banking systems to social media platforms.

These databases are spread across multiple physical locations to improve redundancy, scale with demand, and reduce latency for users around the world. However, managing data consistency across these diverse systems poses significant challenges.

In this blog, we’ll explore the main challenges and share practical solutions to achieve and maintain data consistency in distributed databases.

What are Distributed Databases?

Distributed databases are systems where storage devices are not all attached to a common processor. They may be located in different physical locations and may be interconnected by data communications networks.

These databases can vary from a single database spread across multiple servers to multiple databases replicated or partitioned across several sites.

The main goals of using distributed databases are to ensure reliability, scalability, and availability.

They are designed to handle large data volumes, high transaction rates, and a large number of concurrent users.

Why is Data Consistency Important in Distributed Databases?

Data consistency in distributed databases ensures that all users see the same data at the same time, regardless of where the data is stored or which database server they are accessing.

This is crucial for maintaining the integrity of data across the network, which in turn:

  • Builds Trust: Users and applications rely on accurate data for decision-making and processing. Inconsistent data can lead to erroneous outputs and diminished trust in the system.
  • Ensures Legal Compliance: For many industries, particularly finance and healthcare, data consistency is not just important — it’s mandated by law.
  • Improves System Efficiency: Consistent data helps in optimizing the performance of various algorithms that drive analytics and operational processes.

Consistency Levels in a Database System

Consistency levels in a database system determine how reliable and synchronized data remains across different nodes in a distributed environment.

These levels range from strict, where every change is instantly visible across all nodes, to eventual, where changes are guaranteed to be visible only after some unspecified time.

The choice of consistency level affects the trade-offs between system availability, latency, and accuracy of data, with stricter levels typically requiring more resources and potentially reducing system availability.

Let us discuss the types of consistency levels in a database system:

1. Sequential Consistency

In sequential consistency, all operations are seen in the same sequence by all parts of the system. This order doesn’t need to match the actual real-time order in which the operations were issued.

The key is that operations are consistent across all nodes, meaning every part of the system agrees on the operation order, enhancing predictability in distributed environments.

Example

Imagine a scenario where you and your friend are checking the scores of a live game on different apps.

If the score update process is sequentially consistent, both of you will see the score changes in the same order, though not necessarily at the same real-time moment the scores change.

This ensures that everyone perceives the game progress consistently, even if there’s a slight delay.

Let us understand it from the technical angle of threads.

How It Works with Threads

Sequential consistency ensures that all users see operations in the same order as they happen, but this order can be any order that respects the order from each thread.

Sequential Consistency

Here, both User A and User B see the changes to x sequentially.

First, x is set to 1, and then x is set to 2. Even though the users are different, the order they see the changes must follow the order set by the threads.

2. Strict Consistency

Strict consistency is the most stringent form where any operation on data has to be seen by all system components at the same instant.

This level demands that all changes are immediately visible to all parts of the system, adhering to the exact real-time sequence of events.

However, achieving this in distributed systems can be highly challenging due to the complexities of ensuring instant state visibility across dispersed nodes.

Example

Think of a clock that everyone in the world uses to set their own clocks. If this clock shows a specific time, everyone immediately sees that exact time without any delay.

In database terms, if data is written, everyone around the world sees this update instantly. This level is ideal but hard to achieve because it requires that every part of the system updates instantly across all locations.

How It Works with Threads

Since Strict consistency is the strongest form of consistency any read operation always returns the latest write value.

Strict Consistency

In the example, as soon as x is updated to 2, every subsequent read immediately and consistently returns 2, showing the very latest value.

3. Linearizability (Atomic Consistency)

Linearizability, or atomic consistency, approximates strict consistency by ensuring that completed operations are immediately visible to all parts of the system.

However, it acknowledges that there is a real-world delay between when an operation is performed and when it is seen by the system.

This model works well for operations that overlap in time, providing a balance between strict ordering and practical performance.

Example

Consider online banking where two people are transferring money simultaneously. Atomic consistency ensures that these transactions are seen by the system in the exact order they occur.

If one transaction happens a split second before the other, the system will process them in that order, with no overlap, ensuring no errors in balance calculations.

How It Works with Threads

Atomic consistency ensures that every operation on the database looks instantaneous to all users. When a change is made, everyone immediately sees that change.

Atomic Consistency

As seen in the table, when x is written to 2 in Thread 2, every read operation after that immediately sees x as 2, regardless of when or where it happens.

4. Causal Consistency

Causal consistency ensures that causally related operations (those that have a cause-effect relationship) are seen by all parts of the system in the same order.

For unrelated operations, the order can vary. This model is less stringent than linearizability but still provides a logical order that can be crucial for maintaining the integrity of transaction sequences in distributed applications.

Example

Imagine sending a text message to cancel a meeting and then another to schedule a new time. Causal consistency ensures that these messages are seen by the recipient in the order sent, even if others might receive different non-related messages in between.

This helps in understanding the context and flow of communication properly.

How It Works with Threads

Causal consistency focuses on the cause and effect relationship.

Causal Consistency

If one operation (like writing x = 1) might influence another (like writing y = 1), then everyone must see the first operation before they see the second.

In the table, after x is written to 1, y is written to 1. Thread 2 reads x first and then y. The system ensures that changes that are causally related appear in order across all users.

5. Eventual Consistency

Eventual consistency is a relaxed model where the system does not guarantee immediate consistency. However, it ensures that if no new updates are made to the data over a period, eventually, all accesses to that data will return the last updated value.

This model is often used in systems where performance and availability are prioritized over immediate data accuracy.

These consistency models offer different balances of data accuracy, system performance, and complexity.

Choosing the right model depends on the specific requirements and constraints of the application and system architecture.

Example

Think of updating your profile picture on a social network. You change the picture, and not everyone sees it updated immediately.

Some friends might still see the old picture for a while. However, given enough time without further changes, everyone will eventually see the new picture.

This model balances system performance with data accuracy, which is adequate for non-critical updates.

How It Works with Threads

Eventual consistency promises that if no new updates are made to the data, eventually all accesses to that data will return the last updated value.

Eventual Consistency

Initially, reads might show old data (as seen when Thread 1 reads x as 1 even after it was updated to 2), but eventually, all reads will consistently show the latest value.

Find more real-world applications of the database consistency levels.

Let us now hop onto the challenges that hinder data consistency in databases:

Challenges in Achieving Data Consistency

1. Network Delays and Partitions

One of the primary challenges in maintaining data consistency across distributed systems is handling network delays and partitions.

Data must be replicated across various nodes, and when parts of the network fail or experience delays, it can lead to inconsistencies.

2. Concurrent Data Access

Concurrent access to data by multiple systems or users can lead to race conditions or conflicting changes. This scenario often requires complex coordination to ensure that all parts of the distributed system reflect the same state.

3. Database Failure

System failures can disrupt the synchronization process between databases, resulting in one part of the system having outdated or incorrect data compared to others.

4. Node Failures

Failures in one or more nodes can disrupt the data consistency across the system. Depending on the failure’s nature and the recovery mechanisms in place, this can lead to temporary inconsistencies or data loss

5. Latency

The physical distance between distributed nodes can introduce latency. This delay can lead to scenarios where recent updates to data on one node have not yet propagated to other nodes, leading to stale or outdated data reads.

Learn about the challenges of SQL database scaling.

Solutions to Ensure Data Consistency

Here are some of the solutions to ensure data consistency in database systems.

  1. Implementing CAP Theorem Principles

The CAP Theorem states that a distributed system can only simultaneously provide two out of the following three guarantees: Consistency, Availability, and Partition tolerance.

Here’s how you can balance these:

  • Consistency: Every read receives the most recent write or an error.
  • Availability: Every request receives a response, without guarantee of it containing the most recent write.
  • Partition Tolerance: The system continues to operate despite arbitrary partition failures.

Understanding and choosing the right balance between these elements based on your application’s needs is crucial.

2. Data Versioning

Using data versioning can help manage changes effectively by keeping track of each update’s version. This method helps resolve conflicts by allowing the system to reference versions and apply changes in the correct order.

3. Conflict Resolution Strategies

Develop a strategy to handle conflicts when they occur. This can be as simple as a ‘last writer wins’ approach or more complex strategies like event sourcing or using CRDTs (Conflict-Free Replicated Data Types) that allow concurrent modifications without conflicts.

4. Use of Transactions

Employ distributed transactions with two-phase commit protocols or leverage newer, more robust systems like Google’s Spanner that integrate synchronized clocks to manage transactions more effectively.

5. Regular Audits and Reconciliation

Conduct regular audits and reconciliation processes to ensure that all copies of the database eventually reflect the same data. This process can identify and correct discrepancies across distributed nodes.

Wrapping Up

Managing data consistency in distributed databases requires careful planning and strategic implementation of technologies and protocols designed to handle the complexities of modern distributed systems.

By understanding the challenges and implementing robust solutions, system designers can ensure that their databases remain consistent, reliable, and efficient, regardless of scale.

Embracing these strategies will not only improve your system’s robustness but also enhance user satisfaction by providing reliable and consistent access to their data, no matter where they are in the world.

--

--

Founder www.designgurus.io | Formally a software engineer @ Facebook, Microsoft, Hulu, Formulatrix | Entrepreneur, Software Engineer, Writer.