Learning Library

← Back to Library

CAP Theorem Explained Concisely

Key Points

  • The CAP theorem, coined by Eric Brewer during his MIT PhD work in the early 2000s, explains fundamental trade‑offs in cloud‑native, distributed system design.
  • “C” (Consistency) means every client sees the same data at the same time, “A” (Availability) guarantees every request receives a response, and “P” (Partition tolerance) ensures the system continues operating despite network splits.
  • Because of the theorem’s “you can’t have your cake and eat it too” principle, a distributed database can reliably provide only two of the three guarantees at any given moment.
  • For example, MongoDB is typically classified as a CP system: it prioritizes strong consistency and partition tolerance by routing all writes through a primary node that replicates to secondary nodes, sacrificing pure availability during partitions.

Full Transcript

# CAP Theorem Explained Concisely **Source:** [https://www.youtube.com/watch?v=eWMgsk7mpFc](https://www.youtube.com/watch?v=eWMgsk7mpFc) **Duration:** 00:09:01 ## Summary - The CAP theorem, coined by Eric Brewer during his MIT PhD work in the early 2000s, explains fundamental trade‑offs in cloud‑native, distributed system design. - “C” (Consistency) means every client sees the same data at the same time, “A” (Availability) guarantees every request receives a response, and “P” (Partition tolerance) ensures the system continues operating despite network splits. - Because of the theorem’s “you can’t have your cake and eat it too” principle, a distributed database can reliably provide only two of the three guarantees at any given moment. - For example, MongoDB is typically classified as a CP system: it prioritizes strong consistency and partition tolerance by routing all writes through a primary node that replicates to secondary nodes, sacrificing pure availability during partitions. ## Sections - [00:00:00](https://www.youtube.com/watch?v=eWMgsk7mpFc&t=0s) **Understanding the CAP Theorem** - The speaker introduces the CAP theorem, credits Eric Brewer’s work, and explains how its three components—consistency, availability, and partition tolerance—govern the design of distributed databases. ## Full Transcript
0:00have you ever heard the cliche you can't 0:02have your cake and eat it too 0:05well that's a cliche that's really 0:06reduced down that there's always some 0:09sacrifice involved in any situation now 0:12we're not here to talk about life 0:14scenarios here but it kind of relates to 0:16our topic today on cap theorem hello my 0:20name is jamil spain developer advocate 0:22with ibm 0:25now this theorem really has to we have 0:27to give our credit due it came from a 0:29person called eric brewer who developed 0:32this while he was getting his phd at mit 0:35roughly in the 2000s and the topic of 0:38conversation here was cloud native 0:40design 0:41distributed architectures and this 0:43principle really relates down 0:45to how databases are designed to be 0:47distributed in nature 0:49so now that we've defined where the 0:52background comes from let's really break 0:54down the acronyms now of cap cap and 0:58the c stands for 1:01consistency 1:06which really deduces down that all the 1:09clients need to be able to get the same 1:12data at the same time all right all the 1:14data is consistent there the next the a 1:17availability 1:27all right as data is written is it 1:29actually always replicated across to all 1:32the nodes that are there and the last is 1:38uh 1:39the p is for partition 1:46and we're going to add that on really 1:47partition tolerance 1:54okay and so that really is if let's say 1:56one or more of the nodes uh come out of 1:59communication out of sync 2:01how well do they recover from that do 2:03they have a procedure for 2:05balancing those out and getting 2:06reconnected and what happens after that 2:09occurs so now we have all three that are 2:12here cap 2:14let's talk about how it's represented 2:16and how it's actually talked about how 2:18you'll find it so let's i'll put these 2:21down you'll often see them pictured this 2:23way cap with three circles and really 2:27you'll you'll see these acronyms that 2:29come from what are you going to achieve 2:32now i said we're going to relate this 2:33back to you can't have your cake and eat 2:35it too well that's the situation here 2:37you really can only have two out of the 2:39three at any given time so a lot of you 2:41are distributed architectures your 2:43decisions you make 2:44on which database to use um you know it 2:48really depends about what's most 2:50important here but let's talk about how 2:52these pair up so if i take the 2:54intersection of these 2:56this would be a 2:58ca 3:01cp 3:05and ap 3:06okay and i'm gonna 3:08outline these as we go from here 3:13so when it comes down to it let's take a 3:17database like mongodb 3:23all right this is going to be focused on 3:25the consistency 3:27and partition petition tolerance uh 3:29there why from a consistency well we 3:32know from the design of is that it 3:34has a primary node 3:39and there are also secondary nodes 3:44all the writes go to the primary node 3:47and 3:47all the secondary nodes as they as you 3:50add multiple ones they all replicate 3:53directly from the masters logs of 3:54transactions and everything is there so 3:56you get the consistency from there that 3:59um that data will always be in sync uh 4:02because you're always writing in one 4:03place and it's always reading reads 4:05always come from that one source of of 4:08of action there 4:10so 4:11in the in the event so we have the 4:12consistency that that checks box 4:15that piece there now from a petition 4:17let's say what happens in the event that 4:19one of the notes go down your master 4:20goes down um or your your primary node 4:23goes down then one there's a brief 4:26moment where an election process has to 4:29happen one of the secondary nodes 4:31becomes the primary node and then you 4:34know if that other primary comes back up 4:36it becomes then a secondary node so in 4:39that brief time that that procedure is 4:42handled the petition balancing there but 4:44in that time that the the primary goes 4:47down it's not going to allow any reads 4:49to i mean rights to occur uh to that 4:52situation there so you have a moment 4:54where there's going to be all reads that 4:55are available all right and so that's 4:58really how a lot of these kind of pair 5:00up to match uh based upon what you need 5:02what's most important for you is 5:05uh you know having that recovery model 5:07in place 5:08and being able to always guarantee that 5:10consistency in the event that you may 5:13have some availability outage uh there 5:15as well 5:17so that's great for that cp now let's 5:19deal with the other cross section of 5:21that which is ap 5:23now here let's take a use case of a 5:28a distributed system like apache 5:30cassandra 5:33and i wanted to be able to show the 5:35opposites of variance here with apache 5:38cassandra 5:40the difference is from is that 5:42there is really no primary server so all 5:46of the notes 5:48are kind of um 5:50all the notes are kind of independent as 5:54they go so we're going to always have 5:55that availability all right they're 5:57going to always be available to serve 5:59out read write data all right 6:02and 6:03in the event of a partition so with that 6:06process of um so you always have the 6:09availability since they're always up 6:10always running now from a petition 6:13perspective 6:14they do something called eventually they 6:17all have to synchronize with each other 6:19and so because they're all kind of 6:21distributed 6:23they're always all can read and write to 6:25each of those they have some period 6:26where they're all syncing 6:28so you won't have always instant 6:30consistency there that you would get 6:31with db but at least they have a 6:35facility set in place to be able to 6:37synchronize with each other as let's say 6:39one of them goes one of the nodes goes 6:41down they have a procedure when it comes 6:43back up of course it has a job of kind 6:45of catching back up to date uh with the 6:47others there so that kind of solves the 6:50ap for that and so generally you'll see 6:53on the web think about when you look at 6:54distributed databases what do they offer 6:58here all right um and pick two of these 7:00that you want to achieve there may not 7:02ever be a situation where you have all 7:04three available here before we end this 7:07talk i do want to talk a little bit 7:08about let's take this a step further as 7:11technologists we have to challenge 7:13ourselves as well to think through a lot 7:15of the decisions we make and for me i 7:18thought about that we can also apply 7:20this to microservices and 7:23how you're you make decisions about how 7:25you architect your particular components 7:27whether they're front-end end or in the 7:30middle part 7:31as well so think about decisions like 7:33from a web front end 7:35availability may be a concern in that 7:37situation you may not be able to pair 7:40two of these necessarily but at least 7:43have in mind the piece that it wants to 7:45play now we know with most microservices 7:48they achieve the single responsibility 7:50principle that you really have one and 7:52only job that you're supposed to 7:54allocate or do in your architecture so 7:57from a web front end i may have multiple 7:59replicas 8:00to meet the availability because that's 8:02most important for that i want everyone 8:04to always 8:05as you request a web page your front end 8:08i want you any client to always be able 8:10to get responses there as well and so 8:13then i would take move on to the 8:15distributed tier 8:16where i then hit the the back end to 8:19make sure that that suffices that it can 8:20always maybe deliver that data as well 8:23so just kind of think about it that way 8:26how your how these cap principles 8:30meet 8:31the 8:32responsibility 8:36we do srp single responsibility 8:38principle here now this just touched the 8:40iceberg here but i definitely 8:42hope this was useful in understanding 8:44the background of cap theorem and how it 8:46can apply for you in your architectures 8:49thank you 8:50if you have any questions please drop us 8:52a line below and if you want to see more 8:54videos like this in the future 8:57please like and subscribe