Choosing CP (Consistency + Partition tolerance) over AP (Availability + Partition tolerance) depends on the business requirements, data criticality, and failure tolerance of your system. Here's when CP is the better choice:
1. Systems where data consistency is absolutely critical
If inconsistent data can lead to catastrophic consequences, choose CP. Examples include:
- Financial systems (banking, stock trading):
- Inconsistent balances or transactions can cause financial losses or fraud.
- Example: During a network partition, it's better to reject a withdrawal (unavailable) than allow it with stale data (e.g., withdrawing more money than the account has).
- Healthcare systems (patient records, prescriptions):
- Stale medical data can endanger lives.
- If a doctor requests a patient's record during a partition, it's safer to return an error (unavailable) than provide outdated or wrong information.
- Airline reservation systems:
- Selling the same seat to two passengers due to inconsistent data is unacceptable.
- During a partition, it's better to say "system is down, try later" than risk overbooking.
2. Read-heavy systems with infrequent writes
If your system is mostly read and writes are rare, consistency is more important than availability. Examples:
- Configuration management systems (e.g., DNS, LDAP):
- Configuration data changes infrequently.
- During a partition, it's better to return an error (unavailable) than serve stale configs that could misconfigure critical systems.
- Primary key-value stores for mission-critical metadata (e.g., user IDs, authentication tokens):
- Reads are frequent, but writes are rare.
- Inconsistent auth tokens or user IDs can cause security breaches.
3. Systems where eventual consistency is unacceptable
In some cases, eventual consistency (AP systems) isn't good enough. Choose CP when:
- Strong consistency is required immediately after a write.
- Stale data has zero tolerance.
- Example: Distributed locks (e.g., ZooKeeper, Chubby):
- If multiple nodes think they hold the lock (due to stale data), it breaks the system.
- CP ensures only one node holds the lock at a time, even during partitions.
4. ACID-compliant databases
If you're building a distributed relational database that needs ACID transactions (Atomicity, Consistency, Isolation, Durability):
- Google Spanner, CockroachDB, and Amazon Aurora are CP systems.
- They sacrifice availability during network partitions to ensure transactional consistency.
- Example: In a bank transfer (debit from A, credit to B), both operations MUST succeed or fail together. Inconsistent states (e.g., debit happens but not credit) are unacceptable.
5. Regulatory or compliance requirements
Some industries have strict data integrity laws:
- GDPR (Europe): Inconsistent personal data can violate privacy rules.
- PCI-DSS (payments): Inconsistent transaction records are illegal.
- HIPAA (healthcare, US): Stale medical records aren't allowed.
In these cases, CP ensures compliance by never serving potentially wrong data, even if it means being unavailable temporarily.
6. Low-latency writes aren't critical
If write latency is tolerable (e.g., seconds or minutes), choose CP. Examples:
- Scientific data repositories (e.g., genomic databases):
- New data is written infrequently, but when it is, all nodes MUST see the same version.
- A short delay (unavailability) during a partition is better than inconsistent results in research.
- Legal document storage:
- Once a document is stored, it must be immutable and consistent across all replicas.
- Temporary unavailability is preferable to serving altered or outdated versions.
When CP might hurt:
- High write throughput is needed (e.g., IoT sensor data, clickstream analytics).
- 99.99% uptime is a business requirement (e.g., social media, e-commerce during sales).
- Users can't tolerate "system is down" errors (e.g., gaming, chat apps).
In these cases, AP is better (e.g., Facebook, Twitter tolerate temporary inconsistencies for high availability).
Real-world CP systems:
- Google Spanner (globally consistent database)
- ZooKeeper (coordination service for distributed locks)
- Redis (with strong consistency modes)
- Amazon Aurora (MySQL/PostgreSQL with strong consistency)
- CockroachDB (distributed SQL database)
- HBase (consistent NoSQL for Hadoop)
In short, choose CP over AP if:
- Data inconsistency is catastrophic (finance, healthcare).
- Strong consistency is mandatory by law (GDPR, HIPAA).
- Reads are critical, and stale data is worse than no data.
- Writes are infrequent, and latency during partitions is tolerable.
- ACID transactions or distributed locks are required.
If availability is more important than perfect consistency (e.g., social media, ads, caching), AP is the way to go! 😊
| CP (Consistency + Partition tolerance) | AP (Availability + Partition tolerance) |
|---|
| Financial transactions, healthcare records | Social media feeds, news updates |
| Distributed locks (ZooKeeper) | Caching layers (Redis, Memcached) |
| ACID databases (CockroachDB, Spanner) | Shopping carts (temporary inconsistency OK) |
| Critical configs (DNS, LDAP) | Leaderboards in games (eventual accuracy) |
| Regulatory compliance (GDPR, HIPAA) | IoT sensor data (some loss OK) |
Now you know when to sacrifice availability for consistency! 🚀