A Algo3DVerse
03 — System Design
System Design — Lesson 3 of 6

Databases:
SQL vs NoSQL

Rigid tables and foreign keys vs flexible document blobs. A side-by-side 3D animation reveals the architectural trade-offs that drive the choice.

The database choice defines your system's structure. SQL enforces a rigid schema, guarantees ACID transactions, and excels at complex joins. NoSQL trades those guarantees for flexibility and horizontal scalability. Neither is universally better — context decides.

SQL vs NoSQL — Side by Side

Left: SQL structured tables with JOIN lines. Right: NoSQL document clusters. Fire a query on each side and watch the difference.

drag — scroll to zoom

Code example

The same data operation — SQL JOIN versus MongoDB document query.

SQL vs MongoDB
-- SQL: structured schema with JOIN
-- Tables defined upfront, schema is strict

CREATE TABLE users (
    id   INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    role VARCHAR(50)
);

CREATE TABLE orders (
    id      INT PRIMARY KEY,
    user_id INT REFERENCES users(id),
    total   DECIMAL(10,2)
);

-- Query: user with all their orders
SELECT u.name, o.total
FROM  users  u
JOIN  orders o ON o.user_id = u.id
WHERE u.id = 42;
// MongoDB: flexible document store
// No schema — embed or reference freely

// A user document — orders embedded
const userDoc = {
    _id: ObjectId("..."),
    name: "Naser",
    role: "admin",
    orders: [           // embedded array
        { total: 99.99, date: "2026-07-01" },
        { total: 149.50, date: "2026-06-15" }
    ]
};

// Query: find user and all embedded orders
const user = await db.collection('users')
    .findOne({ _id: ObjectId("...") });

// No JOIN needed — orders are already there
console.log(user.orders); // [{ total: 99.99 }, ...]

Key concepts

🗄

SQL

relational — schema — JOIN

Structured Query Language. Data lives in tables with defined schemas. Foreign keys enforce relationships. Great for structured data and complex queries.

📦

NoSQL

flexible — document — horizontal

Catches all non-relational models: document (MongoDB), key-value (Redis), column-family (Cassandra), graph (Neo4j). Schema-optional, often horizontally scalable.

🔒

ACID

atomicity — consistency — isolation

Atomic (all-or-nothing), Consistent (valid state), Isolated (concurrent transactions don't interfere), Durable (committed = persisted). SQL DBs guarantee this; most NoSQL don't.

⚖

CAP Theorem

consistency — availability — partition

A distributed system can guarantee at most 2 of: Consistency, Availability, Partition tolerance. NoSQL systems often choose AP (available + partition-tolerant) over CA.

→ APIs & REST System Design — Algo3DVerse Caching ?
Press ? to begin the narration walkthrough.
0 / 8