Read Write Configuration

Back

Loading concept...

NoSQL Replication: Read & Write Configuration

The Library Story πŸ“š

Imagine you run a magical library with multiple branches across town. You have ONE main library (the Primary) where all new books arrive first. Then you have several branch libraries (the Replicas) that get copies of those books.

But here’s the tricky part:

  • Who can check out books? (Read)
  • Who can add new books? (Write)
  • How do we make sure everyone has the same books? (Consistency)

This is exactly what Read Write Configuration solves in NoSQL databases!


πŸ”– Read Replicas

What Are They?

Read Replicas are copies of your database that can only answer questionsβ€”they can’t make changes.

Think of them as photocopy machines at each branch library. They have copies of all the books, so people can read them. But if someone wants to donate a new book? They must go to the main library.

Why Use Them?

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  MAIN DATABASE  β”‚ ← All writes go here
β”‚    (Primary)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ copies data to...
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
    β–Ό         β–Ό        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚Replicaβ”‚ β”‚Replicaβ”‚ β”‚Replicaβ”‚
β”‚   1   β”‚ β”‚   2   β”‚ β”‚   3   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜
    β–²         β–²        β–²
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    Many users can
    READ from here!

Simple Example:

  • Your app has 10,000 users reading data
  • Only 100 users writing data
  • Without replicas: Main database handles everything = SLOW!
  • With 3 replicas: Reads spread across 4 servers = FAST!

🎯 Read Preferences

What Is It?

Read Preference tells the database: β€œWhere should I look for my answer?”

It’s like asking: β€œWhich library branch should I visit to find my book?”

The Five Choices

Preference Where to Read Real-World Analogy
primary Only main library β€œI need the LATEST edition!”
primaryPreferred Main first, branch if busy β€œLatest if available”
secondary Only branch libraries β€œAny copy is fine”
secondaryPreferred Branch first, main if none β€œBranch is closer to me”
nearest Closest library β€œI’m in a hurry!”

Quick Example

// Always read from primary (freshest data)
db.users.find().readPref("primary")

// Read from nearest server (fastest)
db.users.find().readPref("nearest")

When to Use What?

  • Showing bank balance? β†’ primary (must be accurate!)
  • Showing product reviews? β†’ secondary (slightly old is OK)
  • Global users? β†’ nearest (speed matters most)

✍️ Write Concerns

What Is It?

Write Concern answers: β€œHow many copies must confirm they saved my data before I can relax?”

Imagine mailing an important letter. Do you:

  • Just drop it in a mailbox? (No confirmation)
  • Wait for β€œdelivered” notification? (Some confirmation)
  • Wait for the person to call you back? (Full confirmation)

The Levels

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         WRITE CONCERN LEVELS            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                         β”‚
β”‚  w: 0  β†’ "Fire and forget"              β”‚
β”‚         Don't wait for anything         β”‚
β”‚         ⚑ Fastest | 🎲 Risky           β”‚
β”‚                                         β”‚
β”‚  w: 1  β†’ "Primary confirmed"            β”‚
β”‚         Main server saved it            β”‚
β”‚         βš–οΈ Balanced                      β”‚
β”‚                                         β”‚
β”‚  w: 2  β†’ "Primary + 1 replica"          β”‚
β”‚         Two copies exist                β”‚
β”‚         πŸ›‘οΈ Safer                         β”‚
β”‚                                         β”‚
β”‚  w: "majority" β†’ "Most servers agree"   β”‚
β”‚         Can survive failures            β”‚
β”‚         🏰 Safest | 🐒 Slower            β”‚
β”‚                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Code Example

// Fast but risky (logging, analytics)
db.logs.insertOne(
  { event: "click" },
  { writeConcern: { w: 0 } }
)

// Safe (important user data)
db.users.insertOne(
  { name: "Alice", balance: 100 },
  { writeConcern: { w: "majority" } }
)

πŸ“– Read Concerns

What Is It?

Read Concern answers: β€œHow fresh must my data be?”

Back to our library: When you ask for a book list, do you want:

  • Whatever the librarian remembers? (Fast but maybe outdated)
  • The official catalog? (Accurate but slower)

The Levels

Level Meaning Use When
local β€œGive me what you have” Speed > Accuracy
available β€œWhatever is fastest” Very fast reads
majority β€œMost servers agree” Need reliable data
linearizable β€œThe absolute truth” Critical operations

Simple Analogy

πŸƒ "local"        β†’ Ask the nearest employee
πŸ“‹ "majority"     β†’ Check with most employees
πŸ“œ "linearizable" β†’ Check the official record

Code Example

// Quick check (may be slightly stale)
db.products.find().readConcern("local")

// Reliable read (confirmed by most servers)
db.orders.find().readConcern("majority")

βš–οΈ Quorum

What Is It?

Quorum means β€œthe minimum votes needed to make a decision.”

Think of it like a class vote:

  • 5 students in class
  • Need more than half to agree = at least 3 votes
  • That’s your quorum!

The Magic Formula

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                     β”‚
β”‚   Quorum = (Total Servers / 2) + 1  β”‚
β”‚                                     β”‚
β”‚   3 servers β†’ Quorum = 2            β”‚
β”‚   5 servers β†’ Quorum = 3            β”‚
β”‚   7 servers β†’ Quorum = 4            β”‚
β”‚                                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Why It Matters

Scenario: 5 servers, 2 go offline

Without Quorum:
  - Remaining 3 might disagree
  - Data becomes inconsistent 😱

With Quorum (needs 3):
  - 3 remaining can still agree βœ…
  - System keeps working safely

Real Example

// Write must be confirmed by majority
db.payments.insertOne(
  { amount: 500 },
  { writeConcern: { w: "majority" } }
)
// With 5 servers: waits for 3 to confirm

⏱️ Replication Lag

What Is It?

Replication Lag is the time delay between:

  1. Data written to the primary
  2. Data appearing on replicas

It’s like when breaking news happens:

  • News anchor reports it FIRST
  • Then it spreads to social media
  • Some people hear it 5 minutes later

That delay? That’s lag.

Why It Happens

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                         β”‚
β”‚   PRIMARY                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”                               β”‚
β”‚   β”‚ NEW β”‚ ← Data written at 10:00:00    β”‚
β”‚   β”‚DATA β”‚                               β”‚
β”‚   β””β”€β”€β”¬β”€β”€β”˜                               β”‚
β”‚      β”‚                                  β”‚
β”‚      β”‚ Network travel time...           β”‚
β”‚      β”‚                                  β”‚
β”‚   β”Œβ”€β”€β”΄β”€β”€β”                               β”‚
β”‚   β”‚COPY β”‚ ← Arrives at 10:00:02         β”‚
β”‚   β”‚     β”‚                               β”‚
β”‚   β””β”€β”€β”€β”€β”€β”˜                               β”‚
β”‚   REPLICA                               β”‚
β”‚                                         β”‚
β”‚   LAG = 2 seconds                       β”‚
β”‚                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Common Causes

  1. Network distance - Replica is far away
  2. Heavy load - Primary is very busy
  3. Large writes - Big data takes time to copy
  4. Slow replica - Replica hardware is weak

The Problem It Creates

User writes: "Balance = $100"
         ↓
      PRIMARY has $100

User reads (from REPLICA):
         ↓
      Still shows $50! 😰
         (hasn't received update yet)

How to Handle It

// For critical reads: force primary
db.balance.find({ user: "alice" })
  .readPref("primary")

// Or wait for majority confirmation
db.balance.find({ user: "alice" })
  .readConcern("majority")

🎯 Putting It All Together

The Decision Flow

graph TD A["Need to READ data?"] --> B{How fresh?} B -->|Latest only| C["readPref: primary"] B -->|Recent enough| D["readPref: secondary"] B -->|Fastest| E["readPref: nearest"] F["Need to WRITE data?"] --> G{How safe?} G -->|Critical| H["w: majority"] G -->|Important| I["w: 2"] G -->|Fast OK| J["w: 1"]

Cheat Sheet Summary

Situation Read Preference Write Concern Read Concern
Bank balance primary majority majority
Social feed nearest 1 local
Order placement primary majority linearizable
Analytics secondary 0 local
Chat messages nearest 1 local

🌟 Key Takeaways

  1. Read Replicas = Extra copies for reading (reduces load)
  2. Read Preferences = Where to read from
  3. Write Concerns = How many must confirm a write
  4. Read Concerns = How reliable must the read be
  5. Quorum = Minimum votes for decisions
  6. Replication Lag = Delay in copying data

Remember our library analogy:

  • Main library = Primary database
  • Branch libraries = Replicas
  • Choosing which branch = Read Preference
  • Confirming book was cataloged = Write Concern
  • Checking official catalog = Read Concern
  • Voting on decisions = Quorum
  • Time for new books to reach branches = Replication Lag

You’ve got this! πŸš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.