Bird
Raised Fist0

Database Replication - Primary-Replica, Multi-Primary Patterns

HLDmediumInterview freq: common
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/5
Load Balancer

Receives client write request

Load Balancer selects a Primary Node to handle the write

Protocol:TCP
BeforeClient request is pending at Load Balancer
AfterRequest forwarded to Primary Node 1
Primary Node 1

Processes write and updates local data

Primary Node 1 commits the write and starts replication

Protocol:Internal DB Commit
BeforePrimary Node 1 has no new write for this key
AfterWrite committed locally; replication stream initiated
Replica Nodes

Apply replication stream updates

Replica Nodes update their data asynchronously from Primary Node 1

Protocol:Replication Stream
BeforeReplica Nodes have stale data for the updated key
AfterReplica Nodes have updated data consistent with Primary Node 1
Multi-Primary Coordination Service

Detects concurrent writes on multiple primaries

Coordinates to identify conflicting writes across Primary Nodes

Protocol:Conflict Metadata Exchange
BeforeWrites may be conflicting but not yet detected
AfterPotential conflicts identified and metadata sent to Conflict Resolution
Conflict Resolution Service

Resolves conflicts and finalizes data state

Applies conflict resolution rules and updates primaries

Protocol:Conflict Resolution Protocol
BeforeConflicting writes exist across primaries
AfterConflicts resolved; consistent data state enforced cluster-wide
🏗️
Architecture Diagram
Architecture Diagram
                    ┌───────────────────────┐
                    │     Load Balancer     │
                    └────────────┬──────────┘
                                 │ TCP
                                 ▼
                    ┌───────────────────────┐
                    │    Primary Node 1     │
                    └───────┬───────────────┘
                            │ Replication Stream
                            ▼
                    ┌───────────────────────┐
                    │     Replica Nodes     │
                    └────────────┬──────────┘
                                 │
                                 │
                    ┌────────────▼──────────┐
                    │ Multi-Primary Coord.  │
                    │      Service          │
                    └────────────┬──────────┘
                                 │ Conflict Info
                                 ▼
                    ┌───────────────────────┐
                    │ Conflict Resolution   │
                    │      Service          │
                    └───────────────────────┘

This architecture illustrates a database replication system supporting both primary-replica and multi-primary patterns. The Load Balancer directs client write requests to one of the Primary Nodes, which commit writes locally and replicate changes asynchronously to Replica Nodes. In multi-primary setups, the Multi-Primary Coordination Service detects concurrent writes across primaries and exchanges conflict metadata. The Conflict Resolution Service then applies rules to resolve these conflicts, ensuring cluster-wide data consistency. This design balances availability and consistency by separating write handling, replication, coordination, and conflict resolution into distinct components, enabling scalable and resilient multi-primary replication.

Primary Focus

✓

Practice

(1/5)
1. In the web crawler's request flow, at Step 5 the Duplicate Detection component receives a URL. What happens if this URL has been seen before?
easy
A. The URL is discarded and not sent to the Scheduler to avoid redundant crawling
B. The URL is immediately re-queued to the Scheduler for crawling again
C. The URL is marked for delayed re-crawl but still sent to the Scheduler
D. The URL is sent to the Politeness Policy Manager to check domain delays

Solution

  1. Step 1: Understand Duplicate Detection behavior at Step 5

    If the URL has been seen before, it is filtered out to prevent redundant crawling.
  2. Step 2: Eliminate other options

    Re-queuing duplicates wastes resources; marking for delayed re-crawl is a separate process; Politeness Policy Manager handles timing, not duplication.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Duplicate Detection discards seen URLs to avoid redundancy [check]
Hint: Duplicate Detection discards duplicates before scheduling [OK]
Common Mistakes:
2. For the review system, which architecture better supports real-time aggregation of review scores for millions of listings with minimal latency, given that eventual consistency is acceptable?
medium
A. Use a stream processing system like Apache Flink to aggregate reviews in real-time
B. Use a relational database with complex joins and transactions for immediate consistency
C. Use batch processing with daily MapReduce jobs to update aggregated scores
D. Use a NoSQL document store with denormalized review aggregates updated on each write

Solution

  1. Step 1: Identify real-time aggregation needs

    Millions of listings require low-latency updates; immediate consistency is not mandatory.
  2. Step 2: Evaluate architectures

    Relational DB (A) is costly and slow at scale; batch jobs (C) have high latency; NoSQL denormalization (D) risks write amplification and stale data. Stream processing (B) provides scalable, low-latency aggregation with eventual consistency.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Stream processing enables scalable real-time aggregation with eventual consistency [check]
Hint: Stream processing fits real-time aggregation with eventual consistency [check]
Common Mistakes:
3. You need to model a social media feed with millions of users and high write throughput. Which data modeling approach is best to optimize for write scalability and read performance?
medium
A. Embed all posts and comments inside each user document to minimize joins.
B. Use a graph database to model user relationships and posts for efficient traversal.
C. Store all posts in a single collection with a compound index on user and timestamp for fast queries.
D. Use a referencing pattern with separate collections for users, posts, and comments, and denormalize popular posts into user feed documents.

Solution

  1. Step 1: Analyze write scalability and read patterns

    High write throughput and millions of users require scalable writes and efficient feed reads.
  2. Step 2: Evaluate referencing with denormalization

    Referencing separates concerns; denormalizing popular posts into feeds optimizes read performance without large documents.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Referencing plus denormalization balances write scale and read speed [check]
Hint: Reference plus denormalize popular data for scalable writes and fast reads [check]
Common Mistakes:
4. Without a durable message queue like Kafka, what exactly breaks in a chat system's message delivery?
medium
A. The system fails to authenticate users leading to unauthorized access
B. Messages are encrypted incorrectly causing security breaches
C. Messages are duplicated multiple times due to lack of deduplication
D. Alice's send blocks until Bob responds; if Bob's server crashes, message is lost with no retry

Solution

  1. Step 1: Identify role of durable queue

    Kafka decouples sender and receiver; without it, sender waits synchronously for receiver's ack.
  2. Step 2: Trace failure without Kafka

    If Bob's server crashes, no ack arrives, send blocks indefinitely and message is lost with no retry mechanism.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    No Kafka -> synchronous send blocks, message lost if receiver crashes [check]
Hint: No Kafka -> send blocks and message lost if receiver crashes [check]
Common Mistakes:
5. Without the use of consistent hashing in a distributed cache, what failure scenario occurs when a node is removed?
medium
A. Only the keys assigned to the removed node are lost permanently
B. The system automatically replicates keys to other nodes without delay
C. Clients continue to query the removed node causing request failures
D. All keys must be remapped causing a cache-wide performance degradation and potential cache stampede

Solution

  1. Step 1: Understand modular hashing failure on node removal

    Modular hashing requires remapping all keys, causing performance degradation and cache misses.
  2. Step 2: Evaluate options

    The system automatically replicates keys to other nodes without delay incorrectly assumes automatic replication without delay; Clients continue to query the removed node causing request failures is invalid as clients rely on updated routing.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Without consistent hashing, remapping causes cache stampede, no instant replication [check]
Hint: No consistent hashing -> full remap, cache stampede risk [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard