Loading...
Design Fundamentals • CAP TheoremMedium⏱️ ~3 min
When to Choose CP vs AP
The Decision Framework
The choice between CP and AP isn't about which is better. It's about matching system behavior to business requirements and user expectations.
Choose CP When:
Mistakes are expensive or dangerous. Use CP for:
Financial transactions: Bank balances, payment processing, ad billing. A duplicate charge or lost transaction violates regulations and costs money.
Inventory and bookings: Concert tickets, hotel rooms, limited stock items. Overselling creates customer service nightmares.
Configuration and feature flags: Serving inconsistent app configurations across servers can break functionality or create security holes.
The cost of CP is concrete: during a partition that isolates 40% of your nodes, those nodes return errors. If the partition lasts 5 minutes, users hitting those nodes see 100% error rate. Your overall availability might drop from 99.99% to 99.5% during incidents.
Choose AP When:
Speed and availability trump perfect accuracy. Use AP for:
Social feeds and content: Users care that something loads in under 100ms to 200ms. Missing the last few posts temporarily is acceptable.
Recommendations and search: Showing slightly stale results beats showing nothing. A 2 second lag in search index is invisible to users.
Metrics and logging: Losing a few log lines or having aggregations lag by seconds is preferable to blocking writes.
Analytics dashboards: Business intelligence queries can tolerate minutes of staleness.
The cost of AP is subtle: you need conflict resolution logic. With last write wins, clock skew of 200ms between regions can cause lost updates. With version vectors, you need application logic to merge conflicts, like combining shopping cart items from different replicas.
The Numeric Trade-Off
Consider a payment service processing 50k transactions per second:
CP configuration: 5 node cluster, require majority (3) acknowledgments. Cross region RTT is 50ms. Write latency p99 is 75ms. During partition isolating 2 nodes, 40% of traffic sees errors. System maintains perfect consistency.
AP configuration: 3 node cluster, W equals 1, R equals 1. Write latency p99 is 5ms. During same partition, all nodes continue accepting writes. System provides 99.99% availability but may create duplicate charges that require reconciliation.
For payments, the CP cost (occasional 40% error rate during rare partitions) is cheaper than the AP cost (complex reconciliation, potential regulatory issues, customer refunds).
💡 Key Takeaways
✓Choose CP when correctness errors are expensive: payments, inventory, bookings. Accept occasional 40% error rate during partitions over data conflicts.
✓Choose AP when speed and availability matter more: social feeds, recommendations, logs. Accept seconds of inconsistency over blocking requests.
✓CP write latency is 50ms to 150ms cross region for consensus versus 1ms to 5ms for AP single node writes. This affects your throughput capacity.
✓AP requires conflict resolution: last write wins loses data with clock skew over 200ms, version vectors need merge logic. CP avoids this complexity.
📌 Examples
1Banking app (CP): MongoDB replica set, majority writes, 20ms p99 latency. During partition, isolated nodes reject transactions. Better to show error than create overdraft.
2Content feed (AP): Cassandra, W=1 R=1, 5ms p99 latency. During partition, all nodes accept posts. User sees 99.99% availability, might miss recent posts for 30 seconds.
Loading...