CAP Theorem: ATM Simulation

The CAP theorem states that a distributed system can provide at most two of three guarantees simultaneously: Consistency, Availability, and Partition tolerance. Since network partitions are unavoidable in distributed systems, the practical choice is between consistency and availability during a partition.

This simulation uses a simple ATM network to illustrate the trade-off.

Scenario

Three ATMs are connected to a central bank backend. Each ATM can process withdrawals if it can verify the account balance with the backend. When a network partition isolates one ATM from the backend, the system must decide how to handle withdrawal requests at the disconnected ATM.

Simulation

ATM
ATM 1
๐Ÿฆ
Bank Backend
ATM
ATM 2
ATM
ATM 3

Choice: Consistency (CP)

The isolated ATM rejects withdrawals until it can confirm the latest balance. The customer cannot withdraw money from this ATM, but the system guarantees that no overdraft or double-spend can occur.

Trade-off: The system sacrifices availability at the partitioned node to maintain a consistent view of the account balance across all nodes.

Real-world example: Some banking systems choose this approach to prevent overdrafts, accepting that customers at affected ATMs must wait or find another machine.

Choice: Availability (AP)

The isolated ATM allows withdrawals based on its last known balance. The customer can withdraw money, but the system may need reconciliation later if the balance was stale.

Trade-off: The system sacrifices consistency to remain available. The partitioned ATM may allow a withdrawal that, combined with withdrawals at other ATMs, exceeds the account balance.

Real-world example: Many ATM networks allow limited offline withdrawals (often with a cap) to keep the service available, accepting the small risk of overdrafts that can be reconciled later.

Key Takeaways

Further Reading