Daily Archives: November 15, 2015

cap theorem

I’v been thinking such a problem for a long time. Let’s take my website for example. What if more and more people visit my website, and what do I do with my server. Natural way is that I pay more money to upgrade the CPU, memory or storage. But what if there are millions of visits that one server couldn’t handle it anymore? Another enhancement is that I use distribudated system.

Let’s say I simply have 2 servers in load balancing mode. Both 2 servers can response requests. We can use round robin to load balance.

It seems fine, but actually, the database behind it is still one database. Millions request will finally go to the single database.

Similarly, we should do same way to the database, make it a distributed database.

Ok, here is the question, when there is a write to db1 on data1, and another request try to check the data1 in db2. How do we handle this? Because it is a distribudated system, the data consistency is a thing we need to think about.

This is the problem which confuses me for a long time, until today I read about cap theorem. Below are the definition for each letter:
Consistency (all nodes see the same data at the same time)
Availability (a guarantee that every request receives a response about whether it succeeded or failed)
Partition tolerance (the system continues to operate despite arbitrary partitioning due to network failures)

And cap theorem says no 3 conditions together can satisfy for distribudated system.

For CA, we want consistency and availability at any time that we can’t tolerent any point failure. Because if network fails, we can’t gurantee each data is newly updated, or any server on service can’t always provide available service.

For AP, the application has high availability and partition tolerance, which means we always provide end-user service. But we can’t guarantee the data is recent updated. Facebook, twitter status can be designed in this way. Because it is ok that status update delayed from seconds to minutes. The idea for this scenario is aiming eventual consistency.

For CP, the system accepts partition tolerance and consistency. But it will lock the request when a server fails until the data is consistent.One of ths type of model I can think maybe is P2P download. Because it really requires data 100% correct data.

All we do is that we choose the database according to the business characteristic. Below is the different database for different scenarios:

It would be easy to answer my question now. There is no way that both 3 conditions exist at the same time. The only solution is that we trade-off one of the characteristic out of three according to our application scenario.