🎯 Interview Template

Since this is a distributed system, Partition Tolerance is mandatory. The key decision is whether to prioritize Consistency or Availability. Because stale data would/would not be acceptable in this system, I would choose CP/AP accordingly.


CAP-Theorem.excalidraw


1️⃣ What is CAP Theorem?

CAP Theorem states that in a Distributed System, you can guarantee only 2 out of 3 propertie

CAP
ConsistencyAvailabilityPartition Tolerance

Definitions

Consistency (C)

  • Every user sees the same latest data.
  • All reads return the most recent write.

Availability (A)

  • Every request receives a response.
  • Response may contain stale data.

Partition Tolerance (P)

  • System continues working despite network failures between nodes.

2️⃣ Interview Shortcut

Important Rule

In distributed systems: ✅ Partition Tolerance is mandatory

So the real interview question becomes:

CP or AP ?

Choose one:

  • Consistency + Partition Tolerance (CP)
  • Availability + Partition Tolerance (AP)

3️⃣ Why Can’t We Have Both?

Example

Two servers:

  • USA Server
  • Europe Server

User A updates profile on USA server. Before update replicates to Europe: 🚨 Network partition happens. Now User B reads from Europe server.

We have two choices:

Option 1: Show Error

  • Don’t serve stale data.
  • Wait until servers synchronize.

➡️ Consistency

Option 2: Show Old Data

  • Continue serving requests.
  • Data may be stale.

➡️ Availability


4️⃣ When to Choose Consistency (CP)

Rule

Use when stale data can cause business problems.

Examples

🎟 Ticket Booking

  • Cannot sell same seat twice.
  • Prevent double booking.

📦 Inventory Systems

  • Last item in stock.
  • Cannot allow multiple users to buy same item.

💰 Financial Systems

  • Trading systems
  • Banking systems
  • Order books

Need latest data at all times.

Interview Line

Wrong data is worse than temporary downtime.


5️⃣ When to Choose Availability (AP)

Rule

Use when stale data is acceptable for a short period.

Examples

📱 Social Media

  • Posts
  • Likes
  • Comments

⭐ Review Platforms

  • Yelp
  • Google Reviews

🎬 Netflix

  • Movie descriptions
  • Recommendations

👤 User Profiles

  • Name changes
  • Profile picture updates

Interview Line

Showing slightly old data is acceptable.


6️⃣ CAP Decision Framework

Ask:

Question

If users see stale data for a few seconds, will it be acceptable?

YES

➡️ Prioritize Availability (AP)

NO

➡️ Prioritize Consistency (CP)


7️⃣ Impact on System Design

If Choosing Consistency (CP)

  • Possible Design Choices:
    • Single Primary Database
    • Distributed Transactions
    • Atomic Transactions
    • Synchronous Replication
    • Higher Latency Accepted

Common Technologies

  • PostgreSQL
  • MySQL
  • Google Spanner

If Choosing Availability (AP)

  • Possible Design Choices:
    • Multiple Replicas
    • Read Replicas
    • Asynchronous Replication
    • Eventual Consistency
    • CDC (Change Data Capture)

Common Technologies

  • Cassandra
  • DynamoDB
  • Multi-region Replicas

8️⃣ Senior-Level Interview Insight

Different parts of the same system may choose different CAP priorities.

Ticketmaster Example

FeatureChoice
Ticket BookingConsistency
Event SearchAvailability
Event DetailsAvailability

Tinder Example

FeatureChoice
Match CreationConsistency
Profile ViewingAvailability
Profile UpdatesAvailability

Interview Line

CAP decisions can vary per component, not necessarily per system.


9️⃣ Consistency Levels

1. Strong Consistency

  • Every read gets latest write.
  • Example:
    • Bank balance
    • Flight seat booking

2. Causal Consistency

  • Related operations appear in correct order.
    • Example:
      • Reply should not appear before original comment.

3. Read-Your-Own-Writes

User immediately sees their own updates. Others may still see old data. Example:

  • Profile update

4. Eventual Consistency

System becomes consistent after some time. Example:

  • Social media posts
  • Netflix metadata

🔟 Most Important Interview Answer

Step 1

Assume:

Partition Tolerance is required.

Step 2

Ask:

Is stale data acceptable?

Step 3

Choose:

  • CP → Booking, Payments, Inventory, Banking
  • AP → Social Media, Profiles, Reviews, Streaming

🚀 One-Line Memory Trick

CAP Decision

“Money & Seats = CP, Posts & Profiles = AP”

  • 💰 Money → Consistency
  • 🎟 Seats → Consistency
  • 📱 Posts → Availability
  • 👤 Profiles → Availability