• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

A CODER'S JOURNEY

Life lessons from a coder

  • HOME
  • CAREER
  • CODE CRAFT
  • PERSONAL FINANCE
  • ABOUT
  • HOME
  • CAREER
  • CODE CRAFT
  • PERSONAL FINANCE
  • ABOUT

System Design Interview Concepts – CAP Theorem

July 21, 2018 //  by Deb Haldar//  Leave a Comment

Table Of Contents
What is CAP theorem?
Consistency
Availability
Partition Tolerance
System classification based on CAP theorem
CAP theorem in action
How to use CAP theorem during system design interview?
AP Systems
CP Systems
CA Systems
Final thoughts on CAP theorem

What is CAP theorem?

CAP Theorem has become the holy grail of distributed system design in recent years. CAP theorem states that any distributed computer system can support only any two among consistency, availability, and partition tolerance.

Let's explore the meaning of these properties in a little more detail.

Consistency

Consistency implies the following:

  • When data is partitioned (distributed), all the nodes see the same data at a given time, and this should be true for all times
  • When queried, each node will return the latest data. If not, the system will just error out.
  • Consistency is achieved by updating several nodes before allowing further reads.

Availability

Availability implies the following:

  • At all times, every request being fired at the system generates a valid response.
  • While doing this, it doesn't mean that every request will receive a response with the latest information (data). Availability is achieved by replicating the data across different servers.

Partition Tolerance

Partition tolerance implies the following:

  • The system is able to perform continuously even if a network failure or data loss occur.
  • Partition tolerance can be achieved by replicating data and system functionality sufficiently across a cluster of nodes and network. The redundancy introduced ensures the system as a whole continues to function even in situations where a node or a set of nodes cannot communicate with each other.

System classification based on CAP theorem

Because only two of the three properties stated by CAP can be guaranteed at any time, systems are usually classified into three types under CAP Theorem:

  •  CA System: Data is consistent between all nodes, and you can read/write from any node, while you cannot afford to let your network go down. (For example: RDBMS like MSSQL Server, Oracle and columnar relational stores)
  • CP System: Data is consistent and maintains tolerance for partitioning and preventing data going out of sync. (For example: Google Big Table, MongoDB (document oriented), and HBase (columnar))
  • AP System: Nodes are online always, but they may not get you the latest data; however, they sync whenever the lines are up. (For example: CouchDB (document oriented), and Cassandra (columnar))
System Classification Based on CAP Theorem

System Classification Based on CAP Theorem

CAP theorem in action

       In real life distributed systems, network partition will happen and we need to ensure that when network partition does happen, the system as a whole is still functional. So, really, our choice boils down to either Consistency Guarantee or Availability guarantee in most cases.

CAP Theorem in action during network partition

CAP Theorem in action during network partition

           A row is updated in Node 1 as shown in the preceding figure. Before Node 3 receives the update propagation, it gets a request for the same row. Now Node 3 has two choices--either provide the copy of the row it has (which may be stale) or deny the request. In the first case, it is compromising on consistency and in the second case, on availability.

How to use CAP theorem during system design interview?

As you saw in the previous section, different types of data stores provides different types of guarantees - some provide consistency guarantees while some provide availability guarantees. 

During a system design interview, you should always clarify what type of guarantee the interviewer is looking for in the system under discussion. For example, if you're building a news feed like twitter- it might have the following requirements:

  • Highly available
  • Consistency can take a hit in favor of availability, if a news feed does not show up for a little while, it should be fine.

With the above requirements, you immediately know that you need an AP system, that is, a system which offers availability during a network partition. So you can choose a NoSQL solution like Cassandra or Dynamo DB.

For bonus points during the interview, you can use the following data from Greg Linden's blog:

" Amazon found every 100ms of latency cost them 1% in sales. "

" Google reports half a second delay caused a 20% drop in traffic."


Please find below a handy cheat sheet for choosing the right data store for your system.

AP Systems

  • Dynamo DB
  • Voldemort
  • Cassandra
  • SimpleDB
  • CouchDB
  • Riak

CP Systems

  • Google BigTable
  • Hbase
  • MongoDB
  • MemcacheDB
  • Redis

CA Systems

  • RDBMS( MySQL, MSSQL Server)

Final thoughts on CAP theorem

    Each one of the three properties, namely, Availability, Consistency and Partition Tolerance, should not be viewed as a binary off/on switch , but rather as tunable parameters when you're designing a distributed system. That is, if you opt for more consistency, you'll need to make your availability or partition tolerance requirements little lax. Conversely, you can tune up your availability if you are prepared to sacrifice some consistency or network partition tolerance.


If you've enjoyed this post, you might also like System Design Interview Concepts : Consistent Hashing .

Category: ALL, CAREER, CODE CRAFTTag: System Design Interviews

Previous Post: «How to fix 20 most frequent C pointer mistakes Top 20 C pointer mistakes and how to fix them
Next Post: System Design Interview Concepts – Load Balancing »

Primary Sidebar

Popular Posts

  • Top 20 C++ multithreading mistakes and how to avoid them
    Top 20 C++ multithreading mistakes and how to avoid them
  • Top 10 C++ header file mistakes and how to fix them
    Top 10 C++ header file mistakes and how to fix them
  • Top 10 dumb mistakes to avoid with C++ 11 smart pointers
    Top 10 dumb mistakes to avoid with C++ 11 smart pointers
  • Top 20 C pointer mistakes and how to fix them
    Top 20 C pointer mistakes and how to fix them
  • How to send email using C# and Outlook.com
    How to send email using C# and Outlook.com
  • Top 25 C++ API design mistakes and how to avoid them
    Top 25 C++ API design mistakes and how to avoid them
  • 6 Tips to supercharge C++11 vector performance
    6 Tips to supercharge C++11 vector performance
  • Top 15 C++ Exception handling mistakes and how to avoid them.
    Top 15 C++ Exception handling mistakes and how to avoid them.
  • 20 ABI (Application Binary Interface) breaking changes every C++ developer should know
    20 ABI (Application Binary Interface) breaking changes every C++ developer should know
  • C++11 Multithreading Tutorial via Q&A - Thread Management Basics
    C++11 Multithreading Tutorial via Q&A - Thread Management Basics

Archives

  • November 2019 (1)
  • September 2019 (3)
  • May 2019 (1)
  • April 2019 (3)
  • March 2019 (1)
  • December 2018 (2)
  • September 2018 (1)
  • August 2018 (2)
  • July 2018 (2)
  • May 2018 (1)
  • April 2018 (1)
  • October 2017 (1)
  • August 2017 (2)
  • January 2017 (1)
  • November 2016 (1)
  • October 2016 (2)
  • August 2016 (1)
  • June 2016 (2)
  • May 2016 (4)
  • April 2016 (2)
  • March 2016 (1)
  • February 2016 (3)
  • January 2016 (3)

Recent posts

  • Understanding Quick Sort for coding interviews
  • Understanding Insertion Sort for coding interviews
  • Understanding Bubble Sort for coding interviews
  • Understanding selection sort for coding interviews
  • Generate binary numbers using a queue

Footer

Design

With an emphasis on typography, white space, and mobile-optimized design, your website will look absolutely breathtaking.

Learn more about design.

Copyright © 2023 · Mai Lifestyle Pro on Genesis Framework · WordPress · Log in