✎ Edit content·DAY 081 · POST 3 OF 5 · How It Works

SQL vs NoSQL

SQL Databases · 12 slides
DAY 081 · POST 3 OF 5
(REMINDER)
DAY 081
How SQL and NoSQL Work Under the Hood
@saurav_dnj_24github.com/SauravDnj · linkedin.com/in/sauravdnj
1 / 12

Theme

Palette

Download

4K — sharpest, slowest
🎬 Video options
Preparing preview…
Live preview · loops the “none” effect
All rendering runs in your browser. No server, no cost, no upload. MP4/WebM = full motion + effects · GIF = lightweight loop · PNG/PDF = static for the Instagram & LinkedIn carousel.

Caption (tap to copy)

📖 Deep dive (full written explanation)

The slides stay clean and scannable. Here's the in-depth explanation behind each one — great for the blog version, show notes, or studying the topic properly.
Slide 1 · How SQL and NoSQL Work Under the Hood

Post three is the mechanical core of the day. Posts one and two stayed at the level of concepts and consequences; here we open the engine. The thesis is that almost every practical trade-off between SQL and NoSQL falls out of one design decision: do you store each fact once and reassemble it with joins, or do you pre-assemble data into one object and accept duplication.

Understanding this lets you predict a database's behavior instead of memorizing it. Once you see why a join exists or why a document read is fast, the rest of the comparison stops being a list of facts and becomes a single coherent story.

Slide 2 · Normalize then join

Normalization is the relational engine's foundational move. Data is decomposed into tables so that each fact lives in exactly one place — users in one table, orders in another, connected by a foreign key. Nothing is duplicated, so there is never a question of which copy is correct because there's only one copy.

The price is paid at read time: answering a question that spans tables requires a join, where the engine matches rows by their keys and stitches them together. That per-query cost is the deliberate trade — a little work on every read in exchange for never storing the same fact twice and never letting copies drift apart.

Slide 3 · Embed then read

Document stores make the opposite trade and it's worth seeing the symmetry. Instead of decomposing data, they embed: an order's data is nested directly inside the user document. A read fetches one document and gets everything, no join, no key-matching — often a single, fast lookup.

The cost moves to write time and to consistency. Because a fact may now be embedded in many documents, any change to that fact must locate and update every copy. The model is optimized for reading data that naturally travels together as a unit; it strains the moment the same data needs to be shared or queried across the whole collection.

Slide 4 · Two paths to the same answer

The two read-path columns make the mechanical difference visible. The SQL path is heavier per query: parse the SQL, build a plan, choose indexes, perform the join across tables, and return assembled rows. That machinery is what lets you ask questions the schema designer never anticipated — the planner adapts.

The document path is leaner for its intended pattern: locate the document by id or index, fetch it, return it as-is. No join, no assembly. The lesson isn't that one path is better — it's that each is optimized for a different question. SQL is built for flexible, cross-cutting queries; the document store is built for fast retrieval of pre-shaped data.

Slide 5 · ACID vs BASE

ACID and BASE name the two consistency philosophies. ACID — Atomicity, Consistency, Isolation, Durability — is the relational guarantee: transactions complete fully or not at all, never violate constraints, don't interfere with each other, and survive crashes once committed. It's the gold standard when correctness is non-negotiable.

BASE — Basically Available, Soft state, Eventual consistency — is the posture many distributed NoSQL stores adopt to achieve scale and availability. It accepts that, for a short window, different nodes may disagree, and the system converges to a consistent state soon after. 'Eventual' doesn't mean wrong; it means correct-soon rather than correct-instantly, which is a perfectly fine trade for many workloads and an unacceptable one for money.

Slide 6 · Indexes on both sides

Indexes are common ground and the single biggest practical lever for performance in both worlds. The default structure is usually a B-tree, which lets the engine find matching records in logarithmic time instead of scanning every record. Relational databases index columns; document stores index fields, including fields nested deep inside objects.

The most common cause of a slow query in either model is a missing index forcing a full scan. The takeaway is identical regardless of which side you're on: know your query patterns, index the fields you filter and sort on, and verify with the engine's query planner that your indexes are actually being used.

Slide 7 · How writes spread out

This pipeline traces what happens to a single write in a scale-out store. The client sends a record; the system hashes the shard key to decide which node owns it; that node and its replicas store copies; and once a quorum of replicas acknowledges, the write is confirmed to the client.

Every stage is a tunable trade-off. Requiring more replicas to acknowledge before confirming raises durability and consistency but adds latency; requiring fewer is faster but riskier. This is exactly the consistency-versus-availability dial CAP describes, made concrete at the level of an individual write.

Slide 8 · SQL: the join in action

These two code slides are the mechanical heart of the post: the same logical question answered two ways. The SQL version joins users and orders on the foreign key at read time — the engine does the work of matching and combining rows, paying the per-query cost in exchange for keeping each fact stored once.

This is normalization in action. The order's total isn't copied into the user row; it lives in the orders table and is fetched on demand. Change it once and every join thereafter sees the new value automatically.

Slide 9 · Document: the embed in action

The document version shows the embedded counterpart. The order already lives inside the user document, so a single findOne returns it with no join at all — the projection just selects which fields come back. This is dramatically simpler and faster for this specific access pattern.

The hidden cost, consistent with the model, is on writes and on cross-cutting queries: if that order total also needs to appear elsewhere, it's duplicated, and ranking orders across all users means unwinding arrays from every document. The code makes tangible why embedding is a bet on how you'll read the data.

Slide 10 · Sharding and replication

Sharding and replication are how scale-out stores achieve both capacity and durability. Sharding splits the dataset by a shard key so each node owns a disjoint slice, spreading load. Replication copies each slice to several nodes so a single machine failing loses no data and the system keeps serving.

The shard key is the most consequential schema decision in this world. A poor key — one where most records share a few values — sends most traffic to a few nodes, creating a hot spot while other nodes idle. A good key has high cardinality and even distribution. And it must be chosen early: re-sharding a large live dataset is one of the most painful operations in all of data engineering.

Slide 11 · The mechanics in a nutshell

The summary ties the mechanics into one retainable model. SQL stores each fact once and joins on read, trading query-time work for guaranteed consistency. Document NoSQL pre-joins by embedding and duplicates on write, trading write-time and consistency cost for fast, simple reads. ACID is the strict, all-or-nothing guarantee; BASE is the eventual, scale-friendly relaxation of it.

The two universal truths: indexes save both models from full scans and are your first stop for performance, and in any scale-out store the shard key makes or breaks your ability to grow. Hold this and you can reason about a new database you've never used by asking which of these choices it made.

Slide 12 · Save this. Follow for Day 82.

Post three exposed the mechanics: normalize-then-join versus embed-then-read, ACID versus BASE, indexing on both sides, and how sharding and replication distribute data. You can now explain why each trade-off exists rather than just listing it.

Post four makes it tactile. We'll model the same small application — users and their posts — in both Postgres and MongoDB, writing the actual schema, inserts, and queries for each. Seeing identical requirements produce two genuinely different designs is where the whole comparison finally clicks into intuition you can use on your own data.

🎨 AI image prompt (matches this theme + palette)

Paste into Midjourney, DALL·E, Ideogram, etc. to generate an on-brand image, then upload it on the Edit content page. The prompt updates automatically with the selected theme + palette.