DEV Community

Cover image for CAP Theorem with examples
Hasanul Islam
Hasanul Islam

Posted on • Updated on

CAP Theorem with examples

CAP (Consistency, Availability, and Partition tolerance) theorem helps us to make our choice while designing distributed systems. We can incorporate any two of these 3 functionalities into our system, not all of them.

Consistency:

If we read immediately after writing to database, our system should return the updated result.

Available:

System should operate 24/7 without any downtime.

Partition Tolerance:

Tolerating partition means we are agreed to embrace distributed system - our machines are in different networks. There might have some sorts of network outage between different networks in distributed systems, but in that case our system should be operational. If our system does not tolerate network partition, then it is an application with one machine - not a distributed system.

If we choose to implement distributed system, that means, we are tolerating network partition. So, in the presence of P(Partition tolerance), we either choose C(Consistency) or A(Availability).

Scenerio 1:

A small system has one machine for database. No sharding, no replication.

Comment: This system ensures CA, i.e. Consistency and Availability compromising Partion tolerance.

Scenerio 2:

When we post to facebook, instagram, then instantly our friends might not see our post.

Comment: This system ensures AP, i.e Availability and Partition Tolerance comprosmising Consistency.

Scenerio 3:

When we write at google doc where lots of things can be handled at client side, and client can save the doc whenver the server is available.

Comment: This system ensures CP, i.e. Consistency and Partition tolerance compromising Availability.

Discussion (0)