Eventual vs Strong Consistency in Distributed Databases
Tags: distributeddatabases, technology, database, consistency
Published: 2017-07-31 16:28:02

---
title: "Eventual vs Strong Consistency in Distributed Databases"
published: true
description: About Eventual and Strong Consistency in Distributed Databases
tags: Distributed-Databases, Technology, Databases, Consistency
---

Explanation of this topic starts with an analogy, taking an example from real life to understand the concept better.

I have the habit of writing something I call *Tech Notes* on my laptop daily to summarize technical concepts that I learn. It helps me to recollect them in an easier way whenever I want to.

But sometimes I used to worry about my laptop being stolen or what if it crashes. I will lose all these *Tech Notes* so I started backing them up on my external Hard Disk. To further reduce the possibility of losing them, I also purchased a subscription of Dropbox.

![alt text](https://cdn-images-1.medium.com/max/800/1*PiQhvAicV4TUXohXNdNMuA.png)

Every fortnight, I update my external Hard Disk with revised and newly written Tech Notes and Dropbox gets updated as soon as I connect my laptop to the internet.

*Here, I am using Hard Disk and Dropbox as a source of reading Tech Notes while the laptop is being used for reading as well writing them. (Master-Slave Model)*

> _Redundancy introduces Reliability._

Now let’s get to the point.

**Case 1: Eventual Consistency**
Whenever we use multiple replicas of a database to store data and let’s say a write request comes to one of the replicas. In such a situation, Databases had to discover a strategy to make this write request at one replica reach other replicas as well so that they all could write data of the request and become consistent.

> _Consistency here means that a read request for an entity made to any of the nodes of the database should return the same data._

Eventual consistency makes sure that data of each node of the database gets consistent eventually. Time taken by the nodes of the database to get consistent may or may not be defined.

Data getting consistent eventually means it will take time for updates to reach other replicas. So what?
This implies that if someone reads from a replica which is not updated yet (since replicas are updated eventually) then it may return stale data.

![alt text](https://cdn-images-1.medium.com/max/800/1*PFgHx8UYLhk3L5ePPmailQ.png)

My Hard Disk also keeps stale data for a period of 15 days as it gets updated fortnightly. Let’s assume John, my friend comes after few days of updation and asks for my Hard Disk.

```python
John: I want your hard disk to read your Tech Notes.

I: Sure, why not. But it has not been updated since last few days.

John: I am fine with it.
```

Now Hard Disk was supplied to John immediately *(low latency)* at the risk of having stale data in it. But I am sure about the fact that it will get updated when the next fortnight starts.

> _Eventual consistency offers low latency at the risk of returning stale data_

While on the other hand, we have something known as Strong Consistency.

**Case 2: Strong Consistency**
It says data will get passed on to all the replicas as soon as a write request comes to one of the replicas of the database.
But during the time these replicas are being updated with new data, response to any subsequent read/write requests by any of the replicas will get delayed as all replicas are busy in keeping each other consistent.

As soon as they become consistent, they start to take care of the requests that have come at their door.

![alt text](https://cdn-images-1.medium.com/max/800/1*klFtrGr8U-XmyiZ1CJx-0w.png)

This time my friend Veronica comes and asks for my Tech Notes.

```python
Veronica: I want your latest Tech Notes.

I: Sure, why not. I will share a Dropbox link with you.
   But Veronica, access it after few minutes as I have written a
   new Tech Note on the laptop which will get synced with my 
   Dropbox account.
```

Now Veronica was able to access up-to-date Tech Notes but after few minutes of delay.

**Conclusion**
* Strong Consistency offers up-to-date data but at the cost of high latency.
* While Eventual consistency offers low latency but may reply to read requests with stale data since all nodes of the database may not have the updated data.

*If you liked the article, please share it with others.*
_This article was first published on Medium. You can take a look at it [here]
(https://medium.com/techspire/eventual-vs-strong-consistency-in-distributed-databases-282fdad37cf7)_.