Consistency vs. Availability
What this lesson covers
Consistency means that multiple copies of the same data match each other. The example is Facebook in its initial days: a single server at Harvard with a single copy of data, so the answer is always right. A single server has three problems. It is a single point of failure, vertical scaling costs a lot and hits a limit, and a student at Oxford connects across a continent, about 100 milliseconds away. A second server at Oxford, with no communication between the two, shares no data. Sending a Harvard student to the Oxford server shares the data and keeps one copy, but the Oxford server is still a single point of failure and the latency between continents is still high. Two phase commit is a protocol in which a leader asks its followers to prepare a transaction and then to commit it. Each follower begins a transaction, writes the statements, keeps the row locked and sends an acknowledgement. The leader then commits to its own database and sends a commit message to everyone. If the leader does not get all the acknowledgements for the prepare, it fails the transaction and tells the followers to roll back. A follower cannot roll back by itself on a timeout, so the leader keeps retrying the commit until it is acknowledged, and the transaction ID makes the retry idempotent. Until then, read and write operations on the row are blocked. The system is totally consistent, and the cost is that it is not available.
- Consistency means that multiple copies of the same data match each other
- A single server is a single point of failure, and vertical scaling costs a lot and hits a limit
- In two phase commit a leader asks its followers to prepare a transaction and then to commit it
- A follower cannot roll back by itself, so the leader retries the commit until it is acknowledged
- The row is blocked until then, so the system is consistent and is not available