Consistency Patterns

Consistency Patterns


Consistency patterns are a set of techniques for data storage and data management in a distributed systems. 


Consistency Patterns determines the data propagation across the distributed system. 


Choice of the consistency pattern depends on the system requirements and usecases. 


Consistency patterns can be broadly categorised as follows

  1. Strong Consistency 
  2. Eventually Consistency
  3. Weak Consistency 


Strong Consistency 


In the strong consistency pattern, read operations performed on any server must always retrieve the data that was included in the latest write operation. 


The strong consistency pattern typically replicates data synchronously across multiple servers. 


Put another way, when a write operation is executed on a server, subsequent read operations on every other server must return the latest written data 



The benefits of strong consistency are the following :

  • simplified application logic
  • increased data durability
  • guaranteed consistent data view across the system



The limitations of strong consistency are as follows:

  • reduced availability of the service
  • degraded latency
  • resource-intensive



The workflow to reach strong consistency in data replication is the following:


  1. The server (client) executes a write operation against the primary database instance
  2. The primary instance propagates the written data to the replica instance
  3. The replica instance sends an acknowledgment signal to the primary instance
  4. The primary instance sends an acknowledgment signal to the client


The popular use cases of the strong consistency model are the following: 

  • File systems
  • Relational databases
  • Financial services such as banking
  • Semi-distributed consensus protocols such as two-phase commit (2PC)
  • Fully distributed consensus protocols such as Paxos


Google’s Bigtable and Google’s Spanner databases are real-world applications of strong consistency.



Eventual Consistency 


In the eventual consistency pattern, when a write operation is executed against a server, the immediate subsequent read operations against other servers do not necessarily return the latest written data 


The system will eventually converge to the same state and the latest data will be returned by other servers on succeeding read operations. 


The eventual consistency pattern typically replicates the data asynchronously across multiple servers. 


Any data changes are only eventually propagated across the system and stale data views are expected until data convergence occurs.


The benefits of eventual consistency pattern are as follows:

  • simple
  • highly available
  • scalable
  • low latency



The drawbacks of eventual consistency are the following :

  • weaker consistency model
  • potential data loss
  • potential data conflicts
  • data inconsistency





The workflow to attain eventual consistency in data replication is the following :

  1. The client executes a write operation against the primary database instance
  2. The primary instance sends an acknowledgment signal to the client
  3. The primary instance eventually propagates the written data to the replica instance



The eventual consistency pattern is a tradeoff between data staleness and scalability. The typical use cases of eventual consistency are the following :


Distributed databases such as Amazon Dynamo and Apache Cassandra are real-world applications of the eventual consistency pattern.



Weak Consistency

In the weak consistency pattern, when a write operation is executed against a server, the subsequent read operations against other servers may or may not return the latest written data. In other words, a best-effort approach to data propagation is performed-the data may not be immediately propagated. The distributed system must meet various conditions such as the passing of time before the latest written data can be returned.


The advantages of weak consistency are the following :

  • high availability
  • low latency


The disadvantages of weak consistency are as follows 6, 4:

  • potential data loss
  • data inconsistency
  • data conflicts



Figure 3: Weak consistency

The write-behind (write-back) cache pattern is an example of weak consistency. The data will be lost if the cache crashes before propagating the data to the database. The workflow of the write-behind cache pattern is the following:

  1. the client executes a write operation against the cache server
  2. the cache writes the received data to the message queue
  3. the cache sends an acknowledgment signal to the client
  4. the event processor asynchronously writes the data to the database


The common use cases of weak consistency are the following :

  • Real-time multiplayer video games
  • Voice over Internet Protocol (VoIP)
  • Live streams
  • Cache server
  • Data backups

For instance, the lost video frames due to poor network connectivity are not retransmitted in a live stream.



Summary of the Database Consistency Models:









Comments

Popular posts from this blog

Distributed Tracing

Scalability

Reverse Proxy Vs API gateway Vs Load Balancer