Interview Notes Search

Hello InterviewQuick Reference

Full Article

CAP Theorem

CAP Basics

CAP Properties

Consistency means all nodes see the same data at the same time and differs from ACID consistency; Availability means every request to a non-failing node gets a possibly stale response; Partition Tolerance means the system continues despite message loss or network partitions.

  • Consistency : All nodes see the same data at the same time; not the same as ACID consistency.
  • Availability : Every request to a non-failing node receives a response, possibly stale.
  • Partition Tolerance : System continues despite message loss or network partitions between nodes.

Interview Decision

Prefer availability by default

Default for most designs; stale data is acceptable when inconsistency is not catastrophic.

Ask latest-read question

Ask: "Does every read need to read the most recent write?".

Treat partitions as mandatory

Network failures will happen; choose consistency or availability during a partition.

Choose Consistency

Ticket Booking Systems need consistency to prevent two users booking the same seat; E-commerce Inventory needs consistency to avoid showing one remaining item to multiple users; Financial Systems need accurate order books because stale data can cause incorrect-price trades.

  • Ticket Booking Systems : Prevent two users from booking the same seat during a partition.
  • E-commerce Inventory : Avoid showing one remaining item as available to multiple users.
  • Financial Systems : Stock trading platforms need accurate order books; stale data can cause wrong-price trades.

Choose Availability

Social Media can show an old profile picture for a few minutes; Content Platforms can show old movie descriptions temporarily; Review Sites prefer slightly outdated restaurant hours over no information.

  • Social Media : Old profile pictures for a few minutes are acceptable.
  • Content Platforms : Old movie descriptions are acceptable temporarily.
  • Review Sites : Slightly outdated restaurant hours briefly are better than no information.

Design Impact

Consistency Patterns

Distributed Transactions use two-phase commit to keep stores in sync but add complexity and latency; Single-Node Solutions avoid propagation issues with one database instance but limit scalability.

  • Distributed Transactions : Use two-phase commit to keep cache and database in sync; adds complexity and latency.
  • Single-Node Solutions : One database instance avoids propagation issues but limits scalability.

Availability Patterns

Multiple Replicas serve reads from replicas with asynchronous replication for better availability and possible staleness; Change Data Capture propagates primary database changes asynchronously to replicas, caches, and other systems.

  • Multiple Replicas : Serve reads from replicas; asynchronous replication improves availability with possible staleness.
  • Change Data Capture ( CDC): Propagate primary database changes asynchronously to replicas, caches, and other systems.

Technology Choices

Traditional RDBMSs such as PostgreSQL and MySQL fit consistency-prioritized designs; Google Spanner is a consistency-prioritized choice; DynamoDB can be configured for either priority; Cassandra is an availability-prioritized choice; Redis Clusters are an availability-prioritized choice.

  • Traditional RDBMSs : PostgreSQL and MySQL fit consistency-prioritized designs.
  • Google Spanner : Technology choice when prioritizing consistency.
  • DynamoDB : Configurable for either priority based on chosen mode.
  • Cassandra : Technology choice when prioritizing availability.
  • Redis Clusters : Technology choice when prioritizing availability.

Advanced Choices

Feature Choices

Ticketmaster uses consistency for booking seats and availability for viewing event details; Tinder uses consistency for matching and availability for viewing profiles.

  • Ticketmaster : Consistency for booking seats; availability for viewing event details.
  • Tinder : Consistency for matching; availability for viewing profiles.

Consistency Models

Eventual Consistency is the default behavior of most distributed databases and converges over time; Strong Consistency makes all reads reflect the most recent write and is the most expensive model; Causal Consistency makes related events appear in the same order to all users; Read-your-own-writes Consistency lets users see their own updates immediately while others may see older versions.

  • Eventual Consistency : Default behavior of most distributed databases; consistency returns over time.
  • Strong Consistency : All reads reflect the most recent write; most expensive but needed for absolute accuracy.
  • Causal Consistency : Related events appear in the same order to all users.
  • Read-your-own-writes Consistency : Users see their own updates immediately; others may see older versions.