{"id":365,"date":"2016-07-07T03:33:08","date_gmt":"2016-07-07T03:33:08","guid":{"rendered":"http:\/\/jeffcardillo.com\/blog\/?p=365"},"modified":"2025-04-21T05:11:01","modified_gmt":"2025-04-21T05:11:01","slug":"cap-theorem-a-brief-discussion","status":"publish","type":"post","link":"http:\/\/jeffcardillo.com\/blog\/2016\/07\/07\/cap-theorem-a-brief-discussion\/","title":{"rendered":"CAP Theorem &#8211; A Brief Discussion"},"content":{"rendered":"<p>Most of us have heard the popular proverb \u201cyou can\u2019t have your cake and eat it too\u201d. The literal meaning of this is that if you eat your cake, then it is gone, so you no longer have it. Metaphorically it teaches us that we cannot have two incompatible things.<\/p>\n<p>CAP theorem (aka Brewer\u2019s theorem from Eric Brewer) makes a similar assertion for distributed computer systems. CAP theorem states that you can have at most two of the following three properties; <em>Consistency<\/em>, <em>Availability<\/em>, and <em>Partition Tolerance<\/em>, because any two are mutually exclusive to the third. A brief outline of the properties follows:<!--more--><\/p>\n<ul>\n<li><strong>C<\/strong>onsistency refers to the property that all nodes in a distributed system see the same data at the same time.<\/li>\n<li><strong>A<\/strong>vailability refers to the property that the distributed system will always be able to respond to a given request.<\/li>\n<li><strong>P<\/strong>artition tolerance refers to the ability for the distributed system to continue operation even when faced with network failures.<\/li>\n<\/ul>\n<p><strong><br \/>\nWhy are we limited to two?<\/strong><\/p>\n<p>For a simple example, let\u2019s assume we have a distributed system with 2 partitioned nodes, node-A and node-B. Having two nodes improves <em>availability<\/em> because each is independently capable of providing services. If node-A\u2019s state is updated while the nodes are partitioned then node-B becomes inconsistent. We can ensure that the system maintains <em>consistency<\/em> by making node-B appear unavailable, but this requires us to give up <em>availability<\/em>. In order to have both <em>consistency<\/em> and <em>availability<\/em> we must allow both nodes to communicate, which gives up<em> partition tolerance<\/em>. This example illustrates that it isn\u2019t possible to have all properties maximized; compromises need to be made. Traditionally, large distributed systems required<em> partition tolerance<\/em> so designers had to choose between <em>consistency<\/em> and <em>availability<\/em> for the second property.<\/p>\n<p><span style=\"font-weight: 400;\">CAP theorem first appeared in 1998 and it still holds today, but savvy designers are able to play fast-and-loose with the \u201cchoose 2 out of 3\u201d rule. One thing in the designer\u2019s favor is that partitions are relatively rare in distributed systems. Another boon to the designer are modern techniques that grant flexibility when handling partitions and recovering from them after they happen. These allow designers to focus on maximizing combinations of <em>consistency<\/em> and <em>availability<\/em> for the specific need of the application until a partition actually occurs. When it does occur, the system may need to fall back to favoring either <em>availability<\/em> or <em>consistency<\/em> until the partition ends.<\/span><\/p>\n<p>There are still significant challenges in making this all work, such as detecting when a partition has occurred and implementing a recovery strategy when it does. For example, if <em>availability<\/em> is the chief concern during a partition event, the state of multiple nodes can progress independently and will need to be merged once the partition ends. The merge itself may present conflicts that will need to be dealt with by the system in order to return to a consistent state.<\/p>\n<p>That\u2019s a very brief summary of CAP theorem. It tells us that when designing distributed computer systems we need to make compromises in order to achieve certain desirable characteristics. As architecture techniques and tools evolve, designers are able to maximize combinations of the properties depending on the state of the system. Even with these advancements, however, designers still cannot maximize all properties at all times; they cannot have their cake and eat it too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of us have heard the popular proverb \u201cyou can\u2019t have your cake and eat it too\u201d. The literal meaning of this is that if you eat your cake, then it is gone, so you no longer have it. Metaphorically it teaches us that we cannot have two incompatible things. CAP theorem (aka Brewer\u2019s theorem from Eric Brewer) makes a similar assertion for distributed computer systems. CAP theorem states that you can have at most two of the following three&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/jeffcardillo.com\/blog\/2016\/07\/07\/cap-theorem-a-brief-discussion\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[42,40],"tags":[43,44],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-architecture","category-computer-science","tag-software-architecture","tag-system-design"],"_links":{"self":[{"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":23,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":445,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions\/445"}],"wp:attachment":[{"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jeffcardillo.com\/blog\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}