08 SQL Vs NoSQL
08 SQL Vs NoSQL
SQL V S NOSQL — PA RT 1
What Is a Database?
DEFINITION
A database is an organised collection of structured data stored and managed electronically. Think of
it as a giant, highly organised digital filing cabinet where data can be stored, retrieved, updated, and
deleted in a controlled, efficient manner.
Every app you use — Instagram, WhatsApp, your banking app — relies on a database to store its
data.
2
What Is a DBMS?
DEFINITION
A Database Management System (DBMS) is the software layer that manages the database. It is the
middle-man between your application code and the raw data. It handles security, querying, indexing,
backups, and ensures data integrity.
Example: MySQL (the DBMS) manages a MySQL database. The two main families of DBMS are
Relational (SQL) and Non-Relational (NoSQL).
[Link] 1/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive
LIBRARY ANALOGY
YOUR APPLICATION ([Link], Python, Java) Sends SQL or queriesDBMS (MySQL, PostgreSQL, MongoDB) reads/writes DATABASE (Actual data on disk)
API queries Security · Querying · Indexing Tables, Documents, Files
Backups · Integrity · Concurrency
3
The Core Question Every Interview Asks
THE QUESTION
"Would you use SQL or NoSQL here?" — This is one of the most common system design
interview questions. Choosing wrong means you'll either struggle to scale past a few million users
(wrong NoSQL choice) or spend months debugging data inconsistencies that corrupt your
business logic (wrong SQL choice).
SQL or NoSQL?
[Link] 2/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive What SQL is, how relational databases work internally,
ACID, JOINs, normalisation, and when to choose SQL
1 What Is SQL?
DEFINITION
SQL stands for Structured Query Language. It is both the language used to interact with
relational databases and the informal name for relational databases themselves.
SQL databases organise data into tables (like spreadsheets) with rows and columns. Each table
represents one type of entity (Users, Orders, Products), and relationships between tables are defined
using primary and foreign keys.
EXCEL ANALOGY
A SQL database is like a well-organised Excel workbook with multiple sheets. Each sheet has
predefined column headers. Every row must follow the same structure. You can cross reference
data between sheets using a shared ID column.
USERS TABLE Each row = one user. Each column = one field. ORDERS TABLE
id (PK) name email city 1 Amit amit@.. Mumbai 2 Priya priya@.. id (PK) user_id (FK) total status 101 1 5000 done 102 3 2500
Delhi 3 Sara sara@.. BLR FOREIGN KEY pending 103 1 8000 done
orders.user_id → [Link]
[Link] 3/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive CORE CONCEPTS — TABLES, ROWS,
COLUMNS
WHAT IS NORMALISATION?
The process of organising tables to eliminate data redundancy and ensure data integrity. You break
one big table into multiple smaller, related tables so that each piece of data is stored exactly once.
[Link] 4/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive ┌────┬───────┬─────────┐
┌─────┬─────────┬───────┬───────────┐
│ id │ name │ email │ │ id │ user_id │ total │ product │
├────┼───────┼─────────┤ ├─────┼─────────┼───────┼───────────┤
│ 1 │ Amit │ amit@.. │ │ 101 │ 1 │ 5000 │ Laptop │
│ 2 │ Priya │ priya@..│ │ 102 │ 2 │ 2500 │ Headphone │
└────┴───────┴─────────┘ │ 103 │ 1 │ 8000 │ Phone │
└─────┴─────────┴───────┴───────────┘
Amit's data stored ONCE. Orders reference via FK (user_id=1).
Change email? Update ONE row. Always consistent.
JOINs — SQL's Superpower
A JOIN is an SQL operation that combines rows from two or more tables based on a related
column. This is what makes relational databases powerful — you split data across tables
(normalisation) and then recombine it on the fly with JOINs.
LEFT JOIN: ALL rows from left table + matching rows from right.
ALL users + their orders (if any). Users without orders show NULL.
RIGHT JOIN: ALL rows from right table + matching rows from left.
ALL orders + their user info. Orphan orders show NULL user.
FULL JOIN: ALL rows from BOTH tables. Non-matching show NULL.
Every user and every order, matched where possible.
AB
INNER JOIN Only matching rows LEFT JOIN RIGHT JOIN FULL JOIN
All left + matching right All right + matching left All from both tables
[Link] 5/11
3
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive ACID — The Guarantee
WHAT IS ACID?
ACID is a set of four properties that guarantee database transactions are processed reliably. This
is the cornerstone of SQL databases and the reason banks, payment systems, and anything
involving money use SQL.
A = ATOMICITY
"All or nothing." A transaction either completes fully or not at all. If transferring ₹5000 from Account
A to B:
Step 1: Debit A by 5000
Step 2: Credit B by 5000
If Step 2 fails, Step 1 is ROLLED BACK. Money doesn't vanish.
C = CONSISTENCY
"Always valid state." The database moves from one valid state to another. Constraints (foreign keys,
unique, NOT NULL) are always enforced. You can never have an order pointing to a user_id that doesn't
exist.
I = ISOLATION
"Transactions don't interfere." Multiple transactions running at
the same time don't see each other's partial changes.
User A transferring money and User B checking balance simultaneously — B sees either the state
BEFORE or AFTER, never a half-done state.
D = DURABILITY
"Once committed, it's permanent." After a transaction commits,
the data survives even if the server crashes or loses power.
The database writes to disk (write-ahead log) before confirming.
A C I D
Atomicity Consistency Isolation Durability
All or nothing. Always valid state. Constraints always No interference. Once committed,
Debit+Credit both happen, or enforced. No bad data. Concurrent txns don't see partial it's permanent. Survives crashes &
neither. changes. power loss.
[Link] 6/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive
You transfer ₹5000 from your account to a friend. Atomicity ensures the money is both debited from
you AND credited to your friend — or neither happens. Consistency ensures total money in the
system doesn't change. Isolation ensures if someone checks your balance mid transfer, they see the
old or new balance, not a weird in-between. Durability ensures once the transfer is confirmed, it stays
even if the bank's server crashes.
4
The SQL Query Language — What You Can Do
SQL is divided into four sub-languages, each for a different type of operation: TYPE
DML Data Manipulation Language SELECT , INSERT , UPDATE , DELETE Read and write actual data
DCL Data Control Language GRANT , REVOKE Manage permissions and access control
TCL Transaction Control Language COMMIT , ROLLBACK , SAVEPOINT Manage transactions (ACID guarantees)
DCL — Permissions:
[Link] 7/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive GRANT SELECT ON users TO
readonly_user;
REVOKE DELETE ON users FROM junior_dev;
TCL — Transactions:
BEGIN;
UPDATE accounts SET balance = balance - 5000 WHERE id = 1; UPDATE accounts
SET balance = balance + 5000 WHERE id = 2; COMMIT; - both succeed together
(atomicity!)
- or ROLLBACK; if something goes wrong
SQL Server Commercial Enterprise Windows systems, BI Stack Overflow, Dell Oracle DB Commercial Mission-
SQL databases traditionally scale vertically — you make the single server bigger (more RAM, faster
CPU, bigger disk). This works until you hit hardware limits. Horizontal scaling (adding more servers)
is possible but complex for SQL because JOINs across servers are hard and maintaining ACID
across distributed nodes is expensive.
[Link] 8/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive VERTICAL VS HORIZONTAL SCALING
SQL databases CAN scale horizontally (sharding, read replicas), but it's much harder than with
NoSQL databases that were BUILT for it.
↑
BIGGER Server
More RAM, CPU, Disk Srv 1 Srv 2 Srv 3 Srv 4 Srv 5 Srv 6 →keep adding!
VERTICAL (Scale Up) Server HORIZONTAL (Scale Out)
→ Rich JOINs & aggregation query power entry Horizontal sharding is complex to
[Link] 9/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive
→ documentation →
40+ years of tools, ecosystem, Not ideal for unstructured data (logs,
→ Schema changes need careful → Easy to find skilled developers
JSON)
migrations
→ Excellent for complex reporting & BI → Can bottleneck at very high write loads
1. Your data has clear relationships (users have orders, orders have items)
2. You need ACID transactions (money transfers, inventory, bookings)
3. Your schema is well-defined and stable (not changing every week)
4. You need complex queries — JOINs, aggregations, GROUP BY, subqueries
5. Data integrity is more important than raw write speed
6. You need reporting and analytics on structured data
Banking & Financial Systems → ACID is non-negotiable. Money can't vanish. E-Commerce →
Products, orders, payments — clear relationships. Healthcare → Patient records need strict
integrity.
ERP / CRM Systems → Complex business rules, many entity relationships. Inventory Management →
Stock counts must be exact (no overselling). Booking Systems (Hotels/Flights) → Double-booking =
disaster. Need isolation. Reporting & BI Dashboards → Complex JOINs and aggregations across
tables.
[Link] 10/11
04/04/2026, 12:57 SQL vs NoSQL — Part 1: The Big Picture & SQL Deep Dive WHAT IS SQL?
Structured Query Language. Data in tables (rows + columns). Relationships via primary/foreign keys.
Schema enforced — every row must match the structure.
CORE CONCEPTS
Table = rows + columns. PK = unique row ID. FK = link to another table's PK. Index = fast lookup (B+
Tree). Normalisation = eliminate redundancy.
INNER = matching rows only. LEFT = all left + matching right. RIGHT = all right + matching left. FULL
= everything from both.
ACID GUARANTEES
Atomicity (all or nothing) · Consistency (always valid) · Isolation (no interference) · Durability
(permanent after commit)
SQL SUB-LANGUAGES
SCALING
Clear relationships · ACID needed · Stable schema · Complex queries/JOINs · Data integrity
critical · Reporting/BI
POPULAR DATABASES
PostgreSQL (complex queries, JSON). MySQL (web apps, LAMP). SQL Server (enterprise/BI).
Oracle (mission-critical). SQLite (embedded/mobile).
[Link] 11/11
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE
SQL V S NOSQL — PA RT 2
What Is NoSQL?
DEFINITION
NoSQL stands for "Not Only SQL". It is a broad family of databases that don't use the relational
table model. Instead of one single design (tables + SQL), NoSQL offers multiple data models,
each optimised for a specific type of data or workload.
The common thread: they sacrifice some consistency guarantees in exchange for horizontal
scalability, schema flexibility, and high availability.
If SQL is one type of organised filing cabinet (rigid but reliable), NoSQL is a collection of very
different storage tools — a backpack, a whiteboard, a spreadsheet, a corkboard — each perfect for a
specific purpose. You pick the tool that fits your data, not force your data into one shape.
INTERVIEW CRITICAL
[Link] 1/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE You must know all 6 types, what they store, and when
to use them. Each type is designed for a fundamentally different kind of data. Interviewers will ask:
"What type of NoSQL would you use for X?" — and you need to pick the right one with reasoning.
② DOCUMENT ⑤ IN-MEMORY
JSON docs, flexible schema. RAM-first. Microsecond latency. Redis, Memcached
MongoDB, Firestore
TYPE 1
⑥ TIME-SERIES Timestamped data. Metrics. InfluxDB, Prometheus
GET key, SET key value, DELETE key — nothing more, nothing less
WHAT IS IT?
Stores data as simple key → value pairs. The key is a unique identifier. The value can be
anything: a string, number, JSON blob, or binary data. The database doesn't care what's
inside the value — it just stores and retrieves it by key. Operations are extremely fast: GET ,
SET , DELETE .
KEY VALUE
─────────────────────────────────────────────────
"user:1001:session" → "abc123xyz789"
"user:1001:cart" → {"items": [{"id": 42, "qty": 2}]}
"rate_limit:ip:1.2.3" → "47"
"cache:product:555" → "{name: 'Laptop', price: 75000}"
"leaderboard:game1" → [{"user": "amit", "score": 9500}, ...]
Operations:
[Link] 2/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE SET "user:1001:session"
"abc123xyz789" → store
GET "user:1001:session" → retrieve → "abc123xyz789"
DELETE "user:1001:session" → remove
TTL "user:1001:session" 3600 → auto-expire in 1 hour
Use Cases: Session storage, user preferences, shopping carts, rate limiting, real-time
leaderboards, caching results of expensive queries.
Examples: Redis (most popular), Amazon DynamoDB, Azure Cosmos DB, Memcached.
It's essentially a giant hash table. hash(key) → memory location → value. O(1)
lookups. Redis stores everything in RAM — reads take microseconds, not
milliseconds. This is why it's used as a caching layer in front of slower databases.
TYPE 2
WHAT IS IT?
{
"_id": "user_1001",
"name": "Amit Sharma",
"email": "amit@[Link]",
"age": 28,
"address": { ← nested object
"city": "Mumbai",
"state": "Maharashtra",
"pin": "400001"
[Link] 3/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE },
Use Cases: User profiles, product catalogs, blog posts with comments, content
management systems, e-commerce catalogs, configuration storage.
SQL: User data split across users, addresses, orders tables → needs JOINs →
data always consistent → but slower for "get everything about this user."
TYPE 3
WHAT IS IT?
Similar to SQL tables but each row can have completely different columns. Data is stored in
column families rather than rows. Optimised for writing massive amounts of data and for
time-series queries ("give me all records between time A and time B"). Built for distribution
across many nodes.
[Link] 4/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE
Examples: Apache Cassandra (most popular), HBase, Google Bigtable (the original), Azure
Table Storage.
Cassandra uses a Log-Structured Merge Tree (LSM Tree) — it writes sequentially to an in-
memory table, then flushes to disk. No random I/O on writes. This lets it handle millions of
writes per second across distributed nodes. Instagram uses Cassandra to store billions of
user activity events.
TYPE 4
When relationships ARE the data — social networks, fraud detection, recommendations
WHAT IS IT?
Data is stored as nodes (entities) and edges (relationships between entities). Ideal
when the relationships between data are just as important as the data itself.
[Link] 5/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE Traversing millions of connections is extremely
fast — impossible in SQL without many expensive JOIN operations.
SQL handles relationships via JOINs. For "friends of friends," you need a self-JOIN. For
"friends of friends of friends," you need another JOIN. Each level of depth adds
another JOIN — and SQL JOINs get exponentially slower. Graph databases store
relationships as first-class citizens — traversing 5 levels deep is just as fast as
traversing 1 level. That's the fundamental difference.
[Link] 6/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE TYPE 5
WHAT IS IT?
Data is stored primarily in RAM (memory) rather than on disk. This eliminates disk I/O, making reads and writes
orders of magnitude faster. Trade-off: data is volatile (lost on power failure) unless persistence mechanisms are
enabled. Used as a speed layer in front of other databases.
Without cache:
App → Database (disk) → 5-50ms per read
TYPICAL PATTERN:
1. App checks Redis first (cache hit? → return instantly)
2. Cache miss → query actual database
3. Store result in Redis with TTL (e.g., expire in 5 minutes)
4. Next request for same data → served from RAM
This is called CACHE-ASIDE or READ-THROUGH pattern.
Use Cases: Caching (most common), session management, real-time leaderboards, pub/sub messaging, rate
limiting, temporary data that changes rapidly.
Examples: Redis (also a key-value store), Memcached, Apache Ignite, Hazelcast, SAP HANA.
PERSISTENCE IN REDIS
[Link] 7/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE slower.
TYPE 6
WHAT IS IT?
Specialised database for storing data points indexed by time. Data is naturally ordered
chronologically. Highly optimised for ingesting millions of events per second, and for queries
like "average CPU over the last 5 minutes" or "all readings between 9am and 10am."
METRIC: server_cpu_usage
┌─────────────────────┬───────────┬──────────┐
│ timestamp │ server_id │ cpu_% │
├─────────────────────┼───────────┼──────────┤
│ 2024-03-15 09:00:01 │ srv-01 │ 45.2 │
│ 2024-03-15 09:00:01 │ srv-02 │ 72.8 │
│ 2024-03-15 09:00:02 │ srv-01 │ 47.1 │
│ 2024-03-15 09:00:02 │ srv-02 │ 71.5 │
│ 2024-03-15 09:00:03 │ srv-01 │ 52.3 │
│ ...millions more... │ │ │
└─────────────────────┴───────────┴──────────┘
SPECIAL FEATURES:
• Automatic downsampling: keep per-second data for 1 day,
per-minute data for 1 month, per-hour data for 1 year
• Built-in aggregation: AVG, MIN, MAX, PERCENTILE over time windows
• Compression: timestamps compress extremely well (sequential)
• Retention policies: auto-delete data older than X days
Use Cases: DevOps monitoring (CPU, memory metrics), IoT sensor data, financial tick data,
application performance monitoring (APM), smart grid data, weather data.
[Link] 8/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE Examples: InfluxDB, Prometheus (+
Grafana), TimescaleDB, Amazon Timestream, Graphite.
3
All 6 NoSQL Types — Quick Comparison
Key-Value key → value (any blob) Caching, sessions, rate limiting Redis Document JSON/BSON
In-Memory RAM-first (any model) Caching layer, real-time data Redis Time-Series Timestamped data points
4
NoSQL Advantages & Disadvantages
✅ NOSQL ADVANTAGES ❌ NOSQL DISADVANTAGES
→ Horizontal scaling — add nodes easily tolerance → Each type optimised for its Eventual consistency — stale reads
migrations → Built for massive write →→→ No native JOINs — complex queries are
hard
throughput → High availability and fault
Limited multi-document transaction support
→ Multi-region distribution is built-in → No enforced schema = risky data quality
[Link] 9/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE
1
ACID — The SQL Standard (Recap with Depth)
ACID is a set of four properties that guarantee database transactions are processed reliably and that
data is never left in a corrupted state, even during system failures. Every major SQL database is
ACID-compliant by default.
[Link] 10/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE Written to disk (WAL — Write-Ahead
2
BASE — The NoSQL Philosophy
WHAT IS BASE?
BASE is the alternative philosophy adopted by most distributed NoSQL databases. It relaxes
ACID's strict consistency guarantees to achieve higher availability and easier horizontal scaling.
BA = BASICALLY AVAILABLE
The system guarantees SOME response to every request,
even during partial failures. The response might be stale
or from a replica, but the system never goes completely down.
S = SOFT STATE
The system's state may change over time even WITHOUT new inputs,
as data propagates between nodes asynchronously.
Example: A social media post you just liked shows 100 likes to you,
but still shows 99 to someone in another region — the update
hasn't propagated to their node yet.
E = EVENTUALLY CONSISTENT
Given enough time with no new updates, ALL nodes will converge
to the same data. Consistency is guaranteed EVENTUALLY — not
immediately.
[Link] 11/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE EVENTUAL CONSISTENCY — HOW IT
WORKS
T = 0s
Write arrivesNode A
✓ UpdatedNode B
✓ Updated
✗ StaleNode C
✗ Stale
T = 2s
PropagatingNode A
✓ UpdatedNode B
⟳ SyncingNode C
Converged!Node A
✓ UpdatedNode B
✓ UpdatedNode C
DIMENSION ACID (SQL) BASE (NOSQL) Consistency Strong — immediate, always Eventual — converges over
time Availability May sacrifice for consistency Prioritised above consistency Transactions Multi-row, multi-
table ACID Single-document or limited Failure Handling Roll back entire transaction Resolve conflicts after the
fact Scalability Harder to scale horizontally Built for horizontal scale Data Integrity Enforced by the database
Enforced by application code Latency Higher (coordination overhead) Lower (no sync coordination) Use
When Financial, inventory, medical Social, analytics, IoT, sessions Example DBs MySQL, PostgreSQL,
[Link] 12/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE Amazon's shopping cart uses BASE/eventual
consistency intentionally. They would rather show a slightly stale cart than fail to load the page.
But Amazon's payment processing uses ACID/strong consistency — you can NEVER have
eventual consistency for money. The payment must either go through completely or not at all.
Real systems use BOTH. The same company picks ACID for critical paths and BASE for
everything else. This is the nuanced answer interviewers want.
4
CAP Theorem — The Distributed Systems Trade-off
In a distributed system, you can only guarantee two out of three properties at the same time:
Since network partitions always happen in distributed systems, you really choose between CP
(consistency + partition tolerance) or AP (availability + partition tolerance).
C
Consistency
AP Systems CP Systems
Cassandra, DynamoDB, CouchDB MongoDB, HBase, Redis
Available but may serve stale data Consistent but may reject reads
A P
Availability Partition Tol.
CA (Consistency + Availability):
→ Only possible on SINGLE node (no partitions)
→ Traditional single-server SQL databases (PostgreSQL, MySQL)
→ Not truly distributed — if that node fails, everything fails
WHAT IS NOSQL?
"Not Only SQL." Multiple data models. Sacrifices consistency for scalability, flexibility,
availability.
6 NOSQL TYPES
ACID (SQL)
Atomicity (all or nothing) · Consistency (always valid) · Isolation (no interference) · Durability
(permanent). Used for: money, inventory, medical.
BASE (NOSQL)
Basically Available (always responds) · Soft state (may change without input) · Eventually
consistent (converges over time). Used for: social, analytics, IoT.
[Link] 14/15
04/04/2026, 12:57 SQL vs NoSQL — Part 2: NoSQL Deep Dive & ACID vs BASE CAP THEOREM
Shopping cart = BASE (show stale cart, never fail to load page).
Payment processing = ACID (money must never be inconsistent).
Real systems use BOTH. Pick per use case.
[Link] 15/15
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics
SQL V S NOSQL — PA RT 3
1
The Complete Comparison Table
Data Model Tables, rows, columns Documents, KV, Graph, Column Structured → SQL; Flexible → NoSQL
Scalability Vertical (scale up) Horizontal (scale out) <10TB → SQL; >100TB → NoSQL
Query Power Rich JOINs, aggregations Relationships Foreign keys, JOINs Embedded / denormalised
Simple key lookups Ad-hoc analytics → SQL Complex relations → SQL
[Link] 1/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics DIMENSION SQL NOSQL INTERVIEW
TIP
Write Speed Moderate (ACID overhead) High (no coordination) High write volume → NoSQL
→→→→
[Link] 2/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics SQL OR NoSQL? — DECISION
FLOWCHART
YES YES NO NO
The answer is ALWAYS: "It depends on the consistency requirements, expected scale, team
expertise, and nature of the data." Show nuance. Most large systems use BOTH — SQL for
transactional paths, NoSQL for scale, caching, and analytics.
1
Normalisation vs Denormalisation
Two opposite database design strategies. Understanding when to use each is a classic
interview question.
WHAT IS IT?
[Link] 3/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics Normalisation organises tables to reduce
data duplication. The same piece of information is stored only once, and other tables reference it.
There are formal levels called Normal Forms.
NORMAL
FORMRULE EXAMPLE
1NF Atomic values only. No repeating groups or arrays. Denormalisation — Optimise for Read Speed WHAT
3NF Meets 2NF + no transitive dependency (non key → Split "phone1, phone2" into separate rows
non-key)
Customer name depends on customer_id alone
→ Data stored once, referenced by FK → Writes are → Data duplicated for fast access → Writes slower
fast (update one place) → Reads are slower (JOIN (update many copies) → Reads are lightning fast (no
needed) → Best for OLTP: banking, inventory → JOIN) → Best for OLAP / high read workloads →
[Link] 4/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics To show "Amit bought Laptop
for 5000" → JOIN all 3 tables. Change Amit's name? Update ONE row in Users. Done.
To show "Amit bought Laptop for 5000" → read ONE document. No JOIN! Change
Amit's name? Must update EVERY document that has his name.
NORMALISED 2
Users Orders Products
Sharding — Horizontal Scaling for SQL
Data stored ONCE
JOINs needed to combine
✓ Consistent ✗ Slower reads WHAT IS SHARDING?
DENORMALISED
One Big Document / Table
Data DUPLICATED
No JOINs — read one doc
✓ Fast reads ✗ Risk of inconsistency
Sharding splits a large table across multiple database servers. Each server holds a subset (shard)
of the data. This is how SQL databases can scale horizontally despite not being built for it natively.
┌──────────────────┐
│ Shard 1: Users │ │ Shard 2: Users │
│ A-F (25M users) │ │ G-M (25M users) │
└──────────────────┘ └──────────────────┘
┌──────────────────┐ ┌──────────────────┐
│ Shard 3: Users │ │ Shard 4: Users │
│ N-S (25M users) │ │ T-Z (25M users) │
└──────────────────┘ └──────────────────┘
Range Shard by value ranges. Simple. Range uneven (more users start with
Based Users A-M → Server 1; N-Z → queries stay on one shard. "S" than "X")
Server 2
Even distribution. No hot Range queries span ALL
Hash hash(shard_key) % shards. shards. Adding shards =
Based num_shards → determines reshuffling data.
shard
Most flexible. Can move Lookup table itself can be
Directory Based A lookup table maps each individual keys. bottleneck + single point of
key → specific shard Hot shards if distribution is failure.
✓ Simple logic
✓ Range queries local HASH-BASED
DIRECTORY-BASED lookup_table[key] → shard
✗ Hot shards risk
hash(key) % N → shard
✗ Uneven distribution
✓ Most flexible
✓ Can move individual keys ✗ Lookup table = bottleneck ✗
✓ Even distribution
✓ No hot shards Single point of failure
Cross-shard JOINs are very slow or impossible — you can't easily JOIN data that lives on
different servers.
ACID transactions across shards require expensive two-phase commit (2PC) protocol — all shards
must agree before committing.
[Link] 6/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics This is why sharding is a last resort for
SQL databases — try vertical scaling, read replicas, and caching first.
3
Database Federation
WHAT IS FEDERATION?
Federation (also called functional partitioning) splits different databases by function — a Users DB,
an Orders DB, a Products DB — each on separate servers. A federation layer sits on top and
provides a unified query interface.
┌─────────────────────┐
│ FEDERATION LAYER │ ← routes queries to correct DB │ (unified endpoint) │
└──────┬──────┬───────┘
│││
┌────────────┤ │ ├────────────┐
↓↓↓↓↓
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Users DB │ │ Orders DB │ │ Products DB │
│ (PostgreSQL) │ │ (MySQL) │ │ (MongoDB) │
└──────────────┘ └──────────────┘ └──────────────┘
BENEFITS:
• Transparency: App queries one endpoint, federation routes to correct DB • Heterogeneity: Each DB can
be a different type!
• Autonomy: Each database is self-contained, updated independently
• Natural fit for MICROSERVICES (each service has its own DB)
TRADE-OFFS:
• Cross-database JOINs are slow
• Global transactions across federated DBs are complex
• Added complexity in the federation layer
[Link] 7/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics
1
Data Replication
WHAT IS REPLICATION?
Copying data from one location (the primary/source) to one or more other locations
(replicas/secondaries). Replication can be synchronous or asynchronous, and creates
multiple copies across different servers or geographic locations.
Replication is like sending a mass email. You write one email and it gets delivered to 10 people.
Each recipient gets their own copy. The delivery can be instant (sync) or delayed by a few seconds
(async). You control who gets updates and when.
Semi Primary waits for at least ONE Middle ground — reduced risk, MySQL semi-sync plugin (used
Synchronous replica to confirm reasonable speed at Facebook)
[Link] 8/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics THREE REPLICATION MODES
SYNCHRONOUS Replica 1 ⟳ Replica 2 ⟳
Use Cases for Replication 5 KEY
Primary Confirms IMMEDIATELY
✓ Fast writes
✗ Risk of data loss
USE CASES FOR REPLICATION MySQL, MongoDB
Replica 1 ✓ Replica 2 ✓ SEMI-SYNCHRONOUS Primary
2. GEOGRAPHIC DISTRIBUTION
Replicate to data centres in Asia, Europe, US
for low-latency local reads.
3. DISASTER RECOVERY
If primary data centre fails, promote a replica to primary.
5. ZERO-DOWNTIME MIGRATIONS
Replicate to new server, then switch over without downtime.
2 Data Mirroring
WHAT IS MIRRORING?
Creating an exact, real-time replica of a database. Mirroring is almost always synchronous. Every
write to the primary is simultaneously written to the mirror. The mirror is an exact,
[Link] 9/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics current duplicate — not a slightly-
behind copy. Used specifically for high availability and instant failover.
SHADOW ANALOGY
Mirroring is like a live shadow. When a dancer moves, the shadow moves at exactly the same
instant. There is never a gap between the two. If the dancer "fails," the shadow can immediately
take over without any missed frames.
ARCHITECTURE:
┌──────────────┐ ┌──────────────┐
│ PRINCIPAL │ ──────→ │ MIRROR │
│ (Primary) │ sync │ (Standby) │
│ Server │ write │ Server │
└──────────────┘ └──────────────┘
↑↑
│ ┌──────────────┘
││
┌─────────────────┐
│ WITNESS │ ← optional: automates failover decisions
│ (monitors both) │ if principal fails, witness + mirror
└─────────────────┘ vote to promote mirror to principal
FLOW:
1. Client sends write to Principal
2. Principal writes AND sends to Mirror simultaneously
3. Mirror applies transaction, sends ACK
4. ONLY after both have the data → client gets confirmation
5. If Principal fails → Witness detects → Mirror promoted → zero downtime
[Link] 10/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics
3
Replication vs Mirroring — Head-to-Head
DIMENSION REPLICATION MIRRORING
Synchronisation Sync OR Async (flexible) Always synchronous (real-time) Number of Copies Multiple (1 to
many) Usually one mirror (1-to-1) Primary Purpose Availability, load balancing, analytics High availability,
instant failover Data Lag Async: seconds of lag possible No lag — always current
Flexibility Very flexible — many configurations More rigid — exact replication only Failover Manual promotion
Use Cases Read scaling, reporting, geo distribution Consistency Async replicas may serve stale reads
HA systems, financial, critical data Mirror always has same data
Cost Lower per replica (async = cheap) Higher (synchronous = double write cost)
INTERVIEW SUMMARY
Replication = copies for scale + backup (can be async). Flexible, many copies, may have lag.
Mirroring = always-on exact copy for zero-downtime failover (always sync). Rigid, 1-to-1, zero lag.
Most enterprise systems use BOTH: mirroring for HA failover + async replication for read
scaling.
[Link] 11/12
04/04/2026, 12:57 SQL vs NoSQL — Part 3: Comparison, Sharding, Replication & Advanced Topics �� SQL vs NoSQL
ACID needed? → SQL. Complex JOINs? → SQL. >100TB or 1M+ writes/sec? → NoSQL.
Schema evolving? → NoSQL. Most systems use BOTH.
NORMALISATION VS DENORMALISATION
Normalised: no duplication, FK references, JOINs needed, consistent. Best for OLTP (banking).
Denormalised: data duplicated, no JOINs, fast reads, risk of inconsistency. Best for OLAP / reads.
Range: shard by value range (simple but hot shards). Hash: even distribution (no range queries).
Directory: lookup table (flexible but bottleneck).
Cross-shard JOINs are slow. Distributed ACID needs 2PC. Sharding = last resort — try vertical
scaling, replicas, caching first.
FEDERATION
Split by function: Users DB, Orders DB, Products DB on separate servers. Federation layer
routes queries. Natural fit for microservices. Cross-DB JOINs are complex.
REPLICATION
Sync: wait for all replicas → zero data loss, slower. Async: confirm immediately → fast, risk
of loss. Semi-sync: wait for ONE → middle ground.
MIRRORING
Always synchronous, 1-to-1, zero lag. Principal + Mirror + Witness. Instant automatic failover. Used
for HA, financial, healthcare.
REPLICATION VS MIRRORING
[Link] 12/12
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies
SQL V S NOSQL — PA RT 4
Interviewers don't just want to know SQL vs NoSQL theory — they want to see that you
understand how these decisions play out in real systems at real scale. Referencing actual
company decisions shows depth and credibility. These four case studies cover the four most
common architectural patterns.
CASE STUDY 1
The Problem
Instagram launched with PostgreSQL and scaled to 300 million daily active users. Two very different
data needs emerged:
→ User profiles and account data → → Activity feeds (your home feed) → Likes (millions
→
Photo metadata (which photo belongs to
whom)
→ Notifications
[Link] 1/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies
PostgreSQL (SQL)
Why This Design Works — Deep Analysis
Apache Cassandra (NoSQL)
What it stores:
• User profiles & account data
• Follower/following relationships
What it stores:
• Photo metadata • Activity feeds (home feed)
• Likes (millions per second)
Why PostgreSQL: • Notifications
• ACID transactions (critical data)
• Complex JOINs for relationships
Why Cassandra:
• Sharded by user_id for locality • Massive write throughput
Strong consistency guaranteed • Horizontal scaling (100s of nodes)
• Simple key-based lookups
Eventual consistency acceptable
[Link] 2/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies 4. HORIZONTAL SCALE: Add Cassandra
INTERVIEW TAKEAWAY
Instagram is the gold-standard polyglot persistence example. The lesson: don't force one database to
do everything. Use SQL where consistency matters and NoSQL where scale matters. When
interviewers ask "Would you use SQL or NoSQL?", the best answer is often: "I'd use BOTH — here's
how I'd split the data."
CASE STUDY 2
The Problem
Discord initially chose MongoDB for message storage — attracted by its flexible JSON schema and
easy development setup. But as they scaled past 100 million messages per channel, serious problems
emerged:
→ Read latency degraded badly at scale → Append-only (never updated after posting)
→
Messages are always queried by TIME
range, not by flexible fields
→ Predictable low latency at massive scale
[Link] 3/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies DISCORD'S DATABASE MIGRATION
BEFORE: MongoDB Flexible JSON docs migrated to massive scale Partition: (channel_id, bucket) ✅ Right NoSQL
NOT optimised for time-series Read latency grew with scale AFTER: Apache Cassandra Write-optimised LSM Tree type!
engine Built-in time-series capabilities Predictable latency at
Sharding was a nightmare ❌ Wrong NoSQL type!
QUERY PATTERN:
SELECT * FROM messages
WHERE channel_id = 'abc123'
AND bucket = '2024-03-15'
AND message_id > [timestamp_for_9am]
AND message_id < [timestamp_for_10am];
→ Reads EXACTLY the messages needed, nothing more.
NoSQL is not one thing. MongoDB's document flexibility was NOT the right NoSQL type for time-
series messages. Cassandra's write-optimised wide-column model was. The lesson: don't just
choose "NoSQL" — choose the right TYPE of NoSQL for your specific access pattern.
[Link] 4/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies CASE STUDY 3
The Problem
Twitter started with Ruby on Rails and MySQL for all data, including tweets. As Twitter grew from
thousands to hundreds of millions of users, MySQL couldn't handle the volume:
TWEET CHARACTERISTICS:
• 300K+ tweets posted per hour
• Tweets are IMMUTABLE (never edited after posting — at that time)
• Accessed primarily by tweet_id or user_id
• Need enormous write throughput + read throughput
• Timeline generation requires reading tweets from 100s of followed users
Twitter Application
Apache Kafka:
→ Event streaming between services
→ When a tweet is posted, Kafka distributes the event to:
- Timeline service (add to followers' feeds)
- Notification service (notify mentioned users)
- Search index service (make tweet searchable)
- Analytics pipeline (count impressions)
Redis:
→ Caching layer for timelines
→ "Get timeline for user X" → check Redis first (O(1))
→ Cache miss → query Manhattan → store in Redis
INTERVIEW TAKEAWAY
Twitter kept ACID where it mattered (accounts, authentication — you can NEVER have
two users with the same username) and adopted NoSQL for high-volume, read-
optimised data. The fan-out problem (1 tweet → millions of timeline updates) is a
classic system design question. Know the trade-off between fan-out-on-write vs fan-
out-on-read.
[Link] 6/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies CASE STUDY 4
Uber — MySQL + Schemaless (Custom NoSQL on SQL) The
most creative solution — getting NoSQL benefits WITHOUT leaving MySQL
The Problem
Uber had a unique constraint that the other companies didn't face:
[Link] 7/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies
Uber Application
"Schemaless" — NoSQL Abstraction Layer
Accepts JSON documents · Key-based lookups · Schema-free writes
stores as blob
The JSON goes into a BLOB column — MySQL doesn't care about
the schema of the JSON. You can add any field anytime.
4. SHARDING by entity_id:
Each trip's data stays on ONE MySQL shard.
ACID transactions work because data is local to one shard.
[Link] 8/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies ✓ MySQL operational tooling (backups,
Uber realised their access pattern was always key-based ("get trip by trip_id"). They never
needed SQL's JOINs for trip data. So the lack of JOIN power wasn't a constraint. By storing
JSON in MySQL blob columns, they got NoSQL's flexibility while keeping MySQL's battle tested
reliability. This shows that you can achieve NoSQL benefits on SQL if your access patterns allow
it.
INTERVIEW TAKEAWAY
Uber's approach teaches a critical lesson: always match the solution to the actual query patterns,
not the hype. They didn't need a fancy NoSQL cluster — they needed schema flexibility + key
lookups + ACID. MySQL with a JSON blob layer gave them all three. This is the kind of nuanced
thinking interviewers love.
5
All 4 Case Studies — Comparison Table
Instagram SQL + NoSQL side- Users, relationships, photos Feeds, likes, Use BOTH — each for what it
by-side (ACID) notifications (scale) does best
Twitter SQL + custom NoSQL SQL Tweets, timelines (Manhattan ACID where it
Accounts, auth (ACID)
built KV) matters, NoSQL for volume
[Link] 9/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies FOUR ARCHITECTURAL PATTERNS FOR
SQL + NoSQL
NoSQL A → NoSQL B
SQL NoSQL SQL + Custom KV
MySQL underneath
PostgreSQL for users, relationships, photos (ACID needed). Cassandra for feeds, likes,
notifications (scale needed). PG sharded by user_id. Gold-standard example of using BOTH.
MongoDB → Cassandra for messages. MongoDB's document model was wrong for time-series
messages. Cassandra's LSM Tree + wide-column model = perfect fit. Partition key:
(channel_id, bucket) . Lesson: right TYPE of NoSQL matters.
MySQL kept for accounts/auth (ACID). Manhattan (custom KV store) built for tweets/timelines. Plus
Kafka for event streaming, Redis for caching. Lesson: ACID where it matters, NoSQL for volume.
Schemaless = NoSQL API layer on top of MySQL. JSON in blob columns. Schema flexibility + ACID
+ MySQL operational maturity. Lesson: match solution to access patterns, not hype.
UNIVERSAL LESSON
[Link] 10/11
04/04/2026, 13:01 SQL vs NoSQL — Part 4: Real-World Case Studies
No company uses just one database at scale. They all use SQL for critical data + NoSQL for scale.
The interview answer is always: "It depends — and real systems use both."
[Link] 11/11
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
DEFINITION
MongoDB is a document-oriented NoSQL database. Instead of storing data in tables with rows and columns (like
SQL), MongoDB stores data as JSON-like documents (technically BSON — Binary JSON). Each document is a self-
contained unit that can have any structure — no two documents need to look the same.
Imagine SQL as a spreadsheet — every row has the same columns, strictly enforced. Now imagine MongoDB as a
folder of papers. Each paper (document) can have different information on it. One paper has name + email + phone.
Another has name + address + age. No one forces them to match. You just put whatever you need on each paper
and file it in a folder (collection).
Row Document One user's data (JSON object) Column Field name , email , age
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert SQL CONCEPT MONGODB EQUIVALENT
EXAMPLE
JOIN Embedding or $lookup Nest related data inside document Schema (enforced) Schema (optional) Can
Document 1:
SQL TABLE — Fixed Schema { name:"Amit", email:"amit@..", age:28,
address:{city:"Mumbai"}, phone:"9876" }
id name email age 1 Amit amit@.. 28 2 Priya priya@.. 25 3 Sara sara@.. 30
What Is a Document?
A document is a JSON-like object that holds all the data for one entity. Unlike SQL where you split
data across multiple tables and JOIN them, MongoDB encourages you to embed related data inside
one document.
{
"_id": ObjectId("507f1f77bcf86cd799439011"), / auto-generated unique ID "name": "Amit Sharma",
"email": "amit@[Link]",
"age": 28,
"created_at": ISODate("2024-03-15T10:30:00Z"),
[Link] 2/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
"state": "Maharashtra",
"pin": "400001"
},
In SQL, to show "Amit's profile with his address and orders," you'd need to JOIN 3 tables (users
+ addresses + orders). That's 3 separate disk lookups and a computation to merge them.
In MongoDB, ALL of Amit's data is in ONE document. One read from disk → you have
everything. This is why MongoDB is often faster for "get everything about this entity"
queries.
What Is BSON?
MongoDB doesn't actually store raw JSON text. It converts JSON to BSON (Binary JSON) — a
binary-encoded format that's more efficient to store and faster to parse. BSON also supports
additional data types that JSON doesn't have: Date , ObjectId , Decimal128 , Binary , RegExp .
You write JSON in your application code, but MongoDB stores BSON on disk. This is
transparent — you never need to think about the conversion.
[Link] 3/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
Every document MUST have an "_id" field. If you don't provide one,
MongoDB auto-generates an ObjectId:
ObjectId("507f1f77bcf86cd799439011")
├────┤├──┤├──┤├────────┤
││││
│ │ │ └─ Counter (3 bytes) — unique within same second │ │ └────── Process ID (2 bytes, since
v3.4: random)
│ └─────────── Machine ID (3 bytes, since v3.4: random)
└───────────────── Timestamp (4 bytes) — seconds since epoch
The storage engine is the component that manages how data is stored on disk and read into
memory. It's the lowest layer — everything else (queries, indexes, replication) sits on top of it. Since
MongoDB 3.2, the default storage engine is WiredTiger.
1. DOCUMENT-LEVEL LOCKING
In older MongoDB (MMAPv1 engine): collection-level lock.
One writer blocks ALL other writers on the same collection.
2. COMPRESSION
WiredTiger compresses data on disk using:
[Link] 4/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
4. IN-MEMORY CACHE
WiredTiger maintains a cache in RAM (default: 50% of RAM).
Hot data stays in memory → reads don't hit disk.
Eviction policies manage what stays and what gets flushed.
5. B-TREE INDEXES
Indexes are stored as B-Tree structures (similar to B+ Tree).
Default index on _id is created automatically.
Additional indexes must be created manually.
[Link] 5/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
Step 5: Write is logged to JOURNAL (write-ahead log on disk)
→ At this point, write is DURABLE (survives crash)
Step 6: Periodically, WiredTiger CHECKPOINTS — flushes dirty
pages from cache to the data files on disk
Step 7: If the document has INDEXED fields, the index B-Trees
are also updated (in memory, then flushed)
MongoDB uses its own query language (not SQL). Here are the core operations:
[Link] 6/20
INSERT OPERATIONS
/ Find with OR
[Link]({ $or: [{ city: "Mumbai" }, { age: { $gt: 30 } }] })
[Link] 7/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
/ COUNT
[Link]({ city: "Mumbai" })
UPDATE OPERATIONS
/ INCREMENT a number
[Link](
{ _id: ObjectId("507f1f77bcf86cd799439011") },
{ $inc: { age: 1 } } / age = age + 1
)
/ PUSH to an array
[Link](
{ email: "amit@[Link]" },
{ $push: { tags: "verified" } } / add "verified" to tags array )
[Link] 8/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
{ email: "new@[Link]" },
{ $set: { name: "New User", age: 20 } },
{ upsert: true } / creates document if not found )
DELETE OPERATIONS
MongoDB's equivalent of SQL's GROUP BY , HAVING , JOIN , and subqueries — all in one. Data flows
through a series of stages, each transforming the data. Like a factory assembly line where each
station does one operation.
/ SQL: SELECT city, COUNT(*) as count FROM users GROUP BY city ORDER BY count DESC
[Link]([
{ $group: { _id: "$city", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
/ SQL: SELECT city, AVG(age) FROM users WHERE age > 20 GROUP BY city HAVING COUNT(*) > [Link]([
{ $match: { age: { $gt: 20 } } }, / Stage 1: WHERE age > 20
{ $group: { / Stage 2: GROUP BY city [Link] 9/20
_id: "$city",
avgAge: { $avg: "$age" },
count: { $sum: 1 }
}},
{ $match: { count: { $gt: 5 } } }, / Stage 3: HAVING count > 5 { $sort: { avgAge: -1 } } / Stage 4: ORDER BY
avgAge DESC ])
5
Indexing in MongoDB
Just like SQL databases, MongoDB uses B-Tree indexes to speed up queries. Without indexes,
MongoDB must scan every document in a collection (collection scan). With indexes, it jumps directly to
matching documents.
[Link] 10/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
[Link]({ email: 1 }) / 1 = ascending, -1 = descending
1. Same left-prefix rule as SQL: compound index {a:1, b:1, c:1} works for queries on a , a+b , a+b+c — NOT b alone.
2. Each index slows down writes (same trade-off as SQL).
3. MongoDB has a 64 index limit per collection.
4. Multikey indexes on arrays can get very large — index each array element.
5. TTL indexes are amazing for session data, cache entries, and logs — auto-cleanup!
[Link] 11/20
6
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert Scaling MongoDB —
Replica Sets & Sharding
A replica set is a group of MongoDB servers that maintain the same data. One server is the primary
(handles all writes), and the others are secondaries (receive copies of the data). If the primary fails, a
secondary is automatically elected as the new primary — automatic failover.
Application
PRIMARY
All WRITES go here
READ SCALING:
Route read queries to secondaries to spread the load.
But reads from secondaries may return SLIGHTLY STALE data
(async replication lag — usually milliseconds to seconds).
When your data is too large for one server (or write throughput exceeds one server's capacity),
you shard — split data across multiple servers. MongoDB has built-in sharding, unlike SQL
databases where sharding is an afterthought.
Application
1. mongos (Router)
→ Your app connects to mongos (not directly to shards)
→ mongos knows which shard has which data
→ Routes queries to the correct shard(s)
→ Multiple mongos instances for load balancing
[Link] 13/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert Bad shard key = hot spots, uneven distribution,
terrible performance.
STRATEGIES:
• Hashed shard key: hash(user_id) → even write distribution
But range queries on user_id now span all shards.
• Range shard key: user_id ranges → range queries are efficient
But risk of hot shards if distribution is uneven.
• Compound shard key: { region: 1, user_id: 1 }
→ Queries for one region go to one shard group
→ Create proper indexes for hot queries → Use covering → Minimize indexes (each slows writes) → Use bulk
indexes (all fields in index) → WiredTiger cache keeps inserts (insertMany) for batching → Lower write concern
→ → WiredTiger journal handles burst writes → Example: Product catalog, user profiles
Consider embedding related data (avoid →
→ Example: IoT data, event logs, analytics
$lookup) Real-world: CERN uses MongoDB for
→ product catalog reads particle physics data
Real-world: eBay uses MongoDB for
[Link] 14/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert READ-HEAVY VS WRITE-HEAVY —
ARCHITECTURE PATTERNS
[Link] 15/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
Geo-spatial queries — location- Developer experience — JSON after creation Write amplification — indexes +
based search = natural
journal + replication
A single MongoDB document cannot exceed 16 MB. This means you can't embed unlimited data
inside one document. If an array (like comments on a post) can grow unboundedly, eventually the
document hits 16MB and writes fail. For unbounded arrays, use a separate collection with a
reference — don't embed.
EMBED when: data is always accessed together, data is bounded (won't grow forever), one-to few
relationship (user has 1-3 addresses).
✅ USE MONGODB WHEN ❌ DON'T USE MONGODB WHEN → You need complex JOINs across many
Data is document-shaped (user profiles, catalogs) entities
→
[Link] 16/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
→ You need horizontal scaling built-in → Data is highly relational (many-to-many Real-time analytics with aggregation
everywhere) pipeline
Primary access = key-based lookups →
read
Data has nested structures (JSON-like) You need strong consistency on every
→ Financial/banking data (use SQL +
→→ → ACID)
→ Mobile/web apps with JSON APIs → IoT data with varying sensor schemas Time-series metrics at extreme scale (use
→ Cassandra/InfluxDB)
eBay Product catalog, search suggestions Handles 100B+ events/day. Schema-less events with varying
properties.
Forbes Content management system
MongoDB's geospatial indexes for finding nearby drivers.
Adobe User data platform, analytics Location data changes constantly.
Uber Geospatial data, trip matching Rapidly evolving data models as new cryptocurrencies and
features are added.
Coinbase Cryptocurrency portfolio data
Flexible product schemas (electronics vs clothing have different EA
fields). Billions of listings. Games
Player profiles, game state Each game has different data
structures. Player state varies widely between games.
Articles have varying structures (text, video, galleries). Schema
flexibility is critical.
Toyota Connected vehicle data IoT sensor data from vehicles with varying sensor configurations per
model.
[Link] 17/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
Notice the common thread: every company chose MongoDB because their data was naturally
document-shaped with varying fields. A product listing for a laptop has "RAM, CPU, GPU" fields. A
product listing for a shirt has "size, color, material" fields. In SQL, you'd need either a massive table
with hundreds of nullable columns or complex EAV (Entity-Attribute-Value) patterns. In MongoDB,
each document simply has the fields it needs.
11
Interview Questions & Model Answers
When my data is naturally document-shaped with nested structures (user profiles, product catalogs),
when the schema evolves frequently (startup, rapid iteration), when I need built-in horizontal sharding
for scale, and when my primary access pattern is key-based lookups rather than complex multi-table
JOINs. I'd choose PostgreSQL when I need ACID transactions across multiple entities, complex
relational queries, or the data has many-to-many relationships.
Through replica sets — a group of MongoDB instances maintaining the same data. One
primary handles all writes, secondaries replicate asynchronously. If the primary fails, an
automatic election promotes a secondary within 10-30 seconds. The driver automatically
reconnects. For read scaling, you can route reads to secondaries using read preferences.
Q3: "HOW WOULD YOU HANDLE A USE CASE WHERE A DOCUMENT KEEPS GROWING (E.G., COMMENTS ON A
POST)?"
I would NOT embed comments inside the post document because it can grow unboundedly and hit
the 16MB document limit. Instead, I'd use a separate comments collection with a reference: each
comment has a post_id field. I'd create an index on { post_id: 1, created_at: -1 } for efficient retrieval.
This is the "reference" pattern vs the "embed" pattern — use embedding for bounded, always-
accessed-together data, and referencing for unbounded or independently-accessed data.
A good shard key has high cardinality (many distinct values for even distribution), even write distribution (writes don't
all go to one shard), and query isolation (most queries can be routed
[Link] 18/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
to a single shard). Example: user_id is often a good shard key. A bad shard key is something
monotonically increasing like created_at because all new writes go to the latest shard, creating a hot
spot.
Yes, since MongoDB 4.0 (2018), it supports multi-document ACID transactions across replica sets,
and since 4.2, across sharded clusters. However, transactions are slower than SQL transactions and
should be used sparingly. MongoDB's philosophy is to design your data model so that most
operations affect a single document (which is always atomic) and only use multi-document
transactions when absolutely necessary.
Embed when: the data is always read together, the relationship is 1-to-few (user has 2-3
addresses), the embedded data is bounded and won't grow forever.
Reference when: the data is accessed independently, the relationship is 1-to-many or many to-
many, the array could grow unboundedly (comments, orders), or the referenced data changes
frequently and you don't want to update it everywhere.
WHAT IS MONGODB?
CORE CONCEPTS
STORAGE ENGINE
WiredTiger: document-level locking, compression (Snappy/Zstd), journal (WAL for durability), in-
memory cache (50% RAM), B-Tree indexes.
QUERIES
[Link] 19/20
04/04/2026, 13:14 MongoDB — Complete Deep Dive from Zero to Expert
INDEXES
B-Tree indexes. Single, compound (left-prefix rule!), unique, text, TTL (auto-expire), partial,
multikey (arrays). 64 index limit per collection.
SCALING
Replica Sets: 1 primary + N secondaries. Automatic failover. Read scaling via secondaries. Async
replication.
Sharding: Built-in. mongos router → config servers → shards (each a replica set). Shard key
= most critical decision. Hashed (even writes) vs ranged (efficient range queries).
Read-heavy: add secondaries, proper indexes, embedding, caching. Write-heavy: shard with hashed
key, fewer indexes, bulk inserts, lower write concern.
EMBED VS REFERENCE
WHEN TO USE
Use: document-shaped data, evolving schema, horizontal scale, key-based lookups, nested data,
CMS, catalogs, mobile apps.
Don't use: complex JOINs, multi-doc ACID critical, highly relational, financial/banking, graph
traversals.
COMPANIES
eBay (catalogs), Forbes (CMS), Adobe (analytics), Uber (geospatial), Coinbase (crypto), EA
Games (player state), Toyota (IoT).
[Link] 20/20
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert
NOSQL D EEP D I V E — WI D E- COL UMN STORE
DEFINITION
Apache Cassandra is a distributed, wide-column NoSQL database designed for massive write
throughput, high availability, and linear horizontal scaling. It was originally built at Facebook to power
their inbox search feature and later open-sourced. It's now used by Apple, Netflix, Instagram,
Discord, and Uber to handle billions of events per day.
Imagine a postal system with hundreds of post offices across a country. When you send a letter
(write), it goes to the nearest post office — no single central office handles everything. Each post
office stores letters for its local area. If one post office burns down, the others still work. Letters are
eventually delivered to all relevant offices (eventual consistency). No single
post office is the "boss" — they're all equal (peer-to-peer). That's Cassandra — no single
master, every node is equal, writes go to the nearest available node.
[Link] 1/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert WHY SQL CAN'T HANDLE THIS:
Write Speed Moderate Fast Extremely fast (LSM Tree) Read Speed Fast (B+ Tree) Fast (B-Tree) Moderate
(multi-level reads)
Query Power Rich JOINs, SQL Aggregation pipeline Limited (no JOINs, restricted WHERE)
Availability Single point of failure Automatic failover Always available (no master)
Best For Complex queries, ACID Flexible docs, catalogs Massive writes, time-series, IoT
[Link] 2/19
2
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert Cassandra's Data Model —
Partitions, Rows & Columns
Cassandra's data model looks like SQL tables on the surface, but underneath it works completely
differently. The partition key determines which node stores the data. The clustering key determines
the sort order within a partition. Getting this right is the single most critical design decision.
CASSANDRA TERMINOLOGY MAPPED TO SQL
[Link] 3/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert
RESULT:
Partition: channel_id = "gaming-chat"
┌──────────────────────────────────────────────────────┐
│ message_id (sorted!) │ author │ content │
├──────────────────────┼─────────┼─────────────────────┤
│ 2024-03-15T09:00:01 │ Amit │ "Hello everyone!" │
│ 2024-03-15T09:00:05 │ Sara │ "Hey Amit!" │
│ 2024-03-15T09:01:12 │ Rahul │ "Good morning" │
│ 2024-03-15T09:03:45 │ Amit │ "How's it going?" │
│ .millions more . │ │ │
└──────────────────────┴─────────┴─────────────────────┘
All sorted by time within this partition → range reads are FAST
All data with the same partition key lives on the same node. If a partition grows too large (100MB+),
it becomes a hot spot — one node handles disproportionate traffic while others are idle. This is why
Discord uses (channel_id, bucket) — the bucket splits very active channels into time-based chunks so
no single partition gets too big.
[Link] 4/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert PRIMARY KEY (user_id, year, month)
3
Internal Architecture — Why Writes Are So Fast
Cassandra uses a Log-Structured Merge Tree (LSM Tree) instead of a B+ Tree. This is the
fundamental reason Cassandra writes are so fast. In a B+ Tree (SQL/MongoDB), writes require
finding the right position on disk and updating in-place — this means random disk I/O. In an LSM
Tree, writes go to an in-memory table first, then get flushed sequentially to disk. Sequential writes
are 100x faster than random writes.
The Write Path — Step by Step
[Link] 5/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert
Query: SELECT * FROM messages WHERE channel_id = 'abc' AND message_id = 'xyz';
[Link] 6/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert
B+ Tree (SQL/MongoDB): Reads are fast (sorted tree, one lookup). Writes are slower (find
position, update in-place, random I/O). Read-optimised.
LSM Tree (Cassandra): Writes are fast (append to log + RAM, sequential I/O). Reads are
slower (check memtable + multiple SSTables). Write-optimised.
This is the fundamental trade-off. Cassandra chose to optimise for writes because its target use
cases (IoT, feeds, messaging) are write-heavy.
4
Distributed Architecture — No Master, Everyone Is Equal
PEER-TO-PEER ARCHITECTURE
Unlike SQL (single master) or MongoDB (primary + secondaries), Cassandra uses a peer-to peer
architecture. Every node is equal — there is no master, no primary, no leader. Any node
[Link] 7/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert can accept reads and writes. If one node goes
down, the others continue serving traffic seamlessly. No failover election needed because
there's nothing to fail over FROM.
Node A
Node F is DOWN
Other nodes handle its
data via replication → no downtime! Node F Node B Write: user_id = "amit" hash("amit") = 37
DOWN! ✗
→ Token range 25-49
Token: 25-49
→ Goes to Node B
Node E Node D
Token: 100-124
Token: 75-99
Node CToken: 50-74
ARE DISTRIBUTED
MULTI-DATACENTER REPLICATION:
CREATE KEYSPACE myapp WITH replication = {
'class': 'NetworkTopologyStrategy',
'us-east': 3, - 3 copies in US East
'eu-west': 3, - 3 copies in EU West
'ap-south': 3 - 3 copies in Asia
};
→ Data automatically replicated across 3 continents
→ Local reads are fast (nearby datacenter)
→ If entire US datacenter goes down, EU and Asia still work
Unlike SQL (always strong) or most NoSQL (always eventual), Cassandra lets you choose per
query how consistent you want the response to be. You control the trade-off between consistency
and latency/availability for each individual read and write operation.
[Link] 9/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert CONSISTENCY LEVELS — FROM WEAKEST
TO STRONGEST
6
Querying Cassandra — CQL (Cassandra Query Language)
[Link] 10/19
04/04/2026, 13:39 Apache Cassandra — Complete Deep Dive from Zero to Expert CQL looks like SQL but has strict constraints on
what you can query. This is by design — Cassandra trades query flexibility for performance.
USE myapp;
- CREATE TABLE
CREATE TABLE messages (
channel_id UUID,
message_id TIMEUUID,
author TEXT,
content TEXT,
created_at TIMESTAMP,
PRIMARY KEY (channel_id, message_id)
) WITH CLUSTERING ORDER BY (message_id DESC); - newest first
- INSERT
INSERT INTO messages (channel_id, message_id, author, content, created_at) VALUES (uuid(), now(),
'Amit', 'Hello world!', toTimestamp(now()));
[Link] 11/19