Everything we have studied so far, including tables, keys, normalization, SQL, ACID transactions and B+ tree indexes, belongs to the world of relational databases. They have powered banks, railways and universities for decades, and they still do.
But in the 2000s, companies like Google, Amazon and Facebook ran into problems of a new size: billions of users, data spread across thousands of servers in many countries, and data shapes that kept changing every week. Squeezing all of that into one big relational server became painful. Out of that pressure came a new family of databases called NoSQL.
NoSQL ("Not only SQL") is a broad term for non-relational databases that store data in models other than tables with fixed schemas, such as key-value pairs, documents, wide columns or graphs, and that are usually designed to scale horizontally across many machines.
NoSQL does not mean "SQL is bad" or "never use SQL". Many NoSQL systems even offer SQL-like query languages. It means "a different data model, chosen for a specific kind of problem".
Why Did NoSQL Appear?
1. Scaling Out Instead of Scaling Up
Vertical scaling (scale UP) Horizontal scaling (scale OUT)
+------------------+ +----+ +----+ +----+ +----+
| one bigger, | | S1 | | S2 | | S3 | | S4 | ...
| costlier server | +----+ +----+ +----+ +----+
+------------------+ data split (sharded) across
many cheap servers
A traditional relational database usually runs on a single primary server. To handle more load you buy a bigger machine, and eventually there is no bigger machine. Splitting relational data across many servers is hard, mainly because joins and multi-row transactions then need coordination between machines.
Most NoSQL databases were designed from day one to shard data (split it into partitions by a key) across many servers and add servers as data grows.
2. Flexible Schema
In a relational table every row has the same columns, and changing the schema of a huge table (ALTER TABLE) can be slow and risky. Many applications have data whose shape varies: a laptop has RAM and ports, a T-shirt has size and colour. NoSQL databases (especially document stores) let each record have its own structure.
3. Data Modelled Around Access Patterns
Instead of normalising data and joining at query time, NoSQL designs often store data the way it will be read, even if that means duplicating it (denormalisation). One read fetches everything needed, without joins.
4. High Availability Across Data Centres
Large web services want to keep accepting reads and writes even when servers or network links fail. Many NoSQL systems relax strict consistency to achieve this, which leads to the BASE model below and to the CAP theorem discussed in the next chapter, Distributed Databases & CAP Theorem.
The Four Main Types of NoSQL Databases
1. Key-Value Stores
The simplest model: a giant, distributed hash map. Each item is a unique key and a value that the database usually treats as an opaque blob.
Key Value
-------------------------- ----------------------------------
session:a1b2c3d4 {"user_id": 42, "cart_items": 3}
otp:+91-98xxxxxx10 "482913"
page:home:views 1938471
Operations are basically GET key, PUT key value and DELETE key. Example in Redis:
SET session:a1b2c3d4 "user:42" EX 1800 # store with a 30-minute expiry
GET session:a1b2c3d4 # -> "user:42"
INCR page:home:views # atomic counter
Examples: Redis, Amazon DynamoDB (key-value and document), Memcached (cache only), Riak.
Good for: caching, sessions, OTPs, shopping carts, rate limiting, leaderboards.
Weak at: querying by anything other than the key, relationships between items.
This is Hashing in DBMS applied at the scale of a cluster: the key is hashed to decide which server (and which bucket) holds the value.
2. Document Databases
Data is stored as documents, usually JSON or a binary form of it (MongoDB uses BSON). A document can contain nested objects and arrays, and documents in the same collection can have different fields.
{
"_id": 1007,
"order_date": "2024-03-28",
"status": "Pending",
"customer": { "id": 101, "name": "Rahul Verma", "city": "Delhi" },
"items": [
{ "product": "Keyboard", "qty": 1, "price": 1800 },
{ "product": "Mouse", "qty": 2, "price": 1900 }
],
"amount": 5600
}
In a relational design, this order would be spread over orders, customers and an order_items table and reassembled with joins. Here, one read returns the whole order. Example queries in the MongoDB shell:
db.orders.insertOne({ _id: 1007, status: "Pending", customer: { id: 101, city: "Delhi" }, amount: 5600 })
db.orders.find({ "customer.city": "Delhi", amount: { $gt: 2000 } })
db.orders.createIndex({ "customer.city": 1 })
Examples: MongoDB, Couchbase, Google Firestore, Amazon DocumentDB.
Good for: product catalogues, content management, user profiles, mobile app back ends, any data that is naturally a self-contained "object".
Weak at: many-to-many relationships and queries that need to join many collections.
3. Wide-Column (Column-Family) Stores
Inspired by Google's Bigtable paper. Data is organised into tables of rows, but each row is identified by a partition key, rows within a partition are kept sorted by clustering columns, and different rows may have different columns. Tables are designed per query: "which question will I ask?" comes before "what are my entities?".
Example in Cassandra's CQL (it looks like SQL, but there are no joins, and you must query by the partition key):
CREATE TABLE orders_by_customer (
customer_id int,
order_date date,
order_id int,
amount decimal,
status text,
PRIMARY KEY ((customer_id), order_date, order_id)
) WITH CLUSTERING ORDER BY (order_date DESC, order_id ASC);
-- Efficient: all orders of one customer live together, newest first
SELECT * FROM orders_by_customer WHERE customer_id = 101;
All rows with customer_id = 101 form one partition stored on the same set of nodes, so this query touches only those nodes. If you also need "orders by date", you typically create a second table with a different partition key and write each order to both.
Examples: Apache Cassandra, Apache HBase, ScyllaDB, Google Bigtable.
Good for: huge write volumes and time-series data: IoT sensor readings, logs, messaging, activity feeds.
Weak at: ad-hoc queries that do not match the partition key, joins, aggregations across partitions.
Do not confuse wide-column stores with columnar analytics databases (like ClickHouse or Amazon Redshift), which store each column separately on disk to speed up aggregations. Different idea, similar name.
4. Graph Databases
Data is stored as nodes (entities), edges (relationships) and properties on both. Relationships are first-class: following an edge is cheap, so queries like "friends of friends" do not need expensive multi-way joins.
(Aarav) --FOLLOWS--> (Priya) --FOLLOWS--> (Rohan)
| ^
+---------------FOLLOWS--------> (Sneha) -+
Example in Cypher (Neo4j's query language): people followed by the people Aarav follows, excluding Aarav himself.
MATCH (a:Person {name: 'Aarav'})-[:FOLLOWS]->(:Person)-[:FOLLOWS]->(fof:Person)
WHERE fof <> a
RETURN DISTINCT fof.name;
Examples: Neo4j, Amazon Neptune, ArangoDB (multi-model), JanusGraph.
Good for: social networks, recommendation engines, fraud detection (rings of linked accounts), knowledge graphs, network topology.
Weak at: bulk aggregation over all records, very high write throughput of simple records.
Summary of the Four Types
Type | Data model | Examples | Typical use |
|---|---|---|---|
Key-value | Key → opaque value | Redis, DynamoDB | Cache, sessions, counters |
Document | Key → JSON-like document | MongoDB, Firestore | Catalogues, profiles, app back ends |
Wide-column | Partition key → sorted rows with flexible columns | Cassandra, HBase | Time-series, logs, huge write loads |
Graph | Nodes + edges + properties | Neo4j, Neptune | Social graphs, recommendations, fraud |
ACID vs BASE
In Transactions & ACID Properties we saw that relational databases guarantee Atomicity, Consistency, Isolation and Durability. Many distributed NoSQL systems were built around a looser model, jokingly named BASE:
Basically Available: the system keeps responding, even if some nodes fail or some answers are slightly stale.
Soft state: the state of the data may change over time even without new writes, as replicas synchronise in the background.
Eventually consistent: if no new updates are made, all replicas will eventually converge to the same value.
Example of eventual consistency: you change your profile photo. For a second or two, a friend in another region still sees the old photo because their read hit a replica that has not received the update yet. Then it updates. For a profile photo that is fine. For a bank balance it is not.
ACID | BASE |
|---|---|
Strong consistency after every transaction | Eventual consistency |
Correctness first | Availability and scale first |
Pessimistic: coordinate before committing | Optimistic: accept writes, reconcile later |
Typical of relational databases | Typical of early distributed NoSQL systems |
Important modern nuance: the line has blurred. MongoDB added multi-document ACID transactions in version 4.0 (for replica sets) and extended them to sharded clusters in 4.2. DynamoDB offers transactional APIs. Cassandra offers tunable consistency per query. So the honest statement is: "NoSQL systems often trade some consistency for scale and availability, and many let you choose per operation".
SQL vs NoSQL
Aspect | Relational (SQL) | NoSQL |
|---|---|---|
Data model | Tables with rows and columns | Key-value, document, wide-column, graph |
Schema | Fixed, defined up front (schema-on-write) | Flexible; structure often checked by the application (schema-on-read) |
Relationships | Foreign keys and joins | Embedding / duplication; graph DBs use edges |
Design approach | Normalise around entities | Denormalise around queries |
Query language | Standard SQL | Product-specific APIs and languages |
Transactions | Full ACID, multi-table | Varies; often single-item atomicity, some offer multi-item ACID |
Scaling | Traditionally vertical; replicas for reads | Horizontal sharding built in |
Strength | Complex queries, integrity, ad-hoc reporting | Scale, flexibility, specific access patterns |
When to Choose What
Choose relational when data is structured and related, integrity matters (money, inventory, bookings, marks), and you need flexible queries and reports. This covers most applications, and a well-indexed MySQL or PostgreSQL server handles far more load than beginners expect.
Choose NoSQL when you have a clear, specific access pattern at very large scale (key lookups, time-series writes), rapidly changing or highly varied data shapes, or relationship-heavy graph queries.
Many real systems use both, which is called polyglot persistence. A typical e-commerce site might keep orders and payments in MySQL or PostgreSQL, sessions and carts in Redis, the product search in Elasticsearch, and recommendations in a graph database.
Relational Databases Learned from NoSQL
Relational databases have absorbed many NoSQL ideas. For example, MySQL 8 has a native JSON type, so a single table can hold semi-structured attributes. In a scratch copy of our sample database:
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
attrs JSON
);
INSERT INTO products VALUES
(1, 'Laptop', '{"brand": "Lenovo", "ram_gb": 16, "ports": ["USB-C", "HDMI"]}'),
(2, 'T-shirt', '{"brand": "CottonCo", "size": "M", "color": "black"}'),
(3, 'Smartphone', '{"brand": "Samsung", "ram_gb": 8, "dual_sim": true}');
SELECT name, attrs->>'$.brand' AS brand, attrs->>'$.ram_gb' AS ram_gb FROM products;
+------------+----------+--------+
| name | brand | ram_gb |
+------------+----------+--------+
| Laptop | Lenovo | 16 |
| T-shirt | CottonCo | NULL |
| Smartphone | Samsung | 8 |
+------------+----------+--------+
Each product has different attributes, and missing ones simply come back as NULL. The ->> operator extracts a value from the JSON document as text.
In the other direction, a group of databases called NewSQL or distributed SQL (for example Google Spanner, CockroachDB and TiDB) offer SQL and ACID transactions while sharding data across many machines like NoSQL systems. How they balance consistency and availability is exactly the topic of the next chapter.
Common Mistakes
"NoSQL means no schema at all." The data still has a structure; it is just enforced by the application (or by optional schema validation) instead of the database up front.
"NoSQL is always faster." It is fast for the access patterns it was designed for. For ad-hoc queries and joins it can be much slower or simply unable to answer.
"NoSQL databases never support transactions." Many now do, with limits.
Designing a document or wide-column database like a normalised relational schema. You end up doing joins in application code. Model around queries instead.
Choosing NoSQL "for scale" too early. Most applications never outgrow a single well-tuned relational server with read replicas.
Interview Questions
1. What is NoSQL? Name its main types.
NoSQL refers to non-relational databases with flexible data models that usually scale horizontally. The main types are key-value (Redis), document (MongoDB), wide-column (Cassandra) and graph (Neo4j).
2. What does BASE stand for?
Basically Available, Soft state, Eventually consistent: a looser model than ACID that favours availability and scale, with replicas converging over time.
3. What is eventual consistency?
A guarantee that if no new updates occur, all replicas will eventually return the same, latest value. Reads in between may return stale data.
4. When would you use MongoDB over MySQL?
When data is naturally document-shaped and varies between records (e.g. product catalogues, content, user profiles), is mostly read and written as whole objects, and needs flexible schema or easy horizontal scaling. For heavily relational, transaction-critical data, MySQL is usually the better fit.
5. What is denormalization in NoSQL and why is it used?
Storing duplicated or embedded data so that a single read returns everything a query needs without joins. It speeds up reads at the cost of extra storage and more complex updates.
6. What is sharding?
Splitting data horizontally into partitions, by a shard (partition) key, and storing different partitions on different servers so that storage and load are spread across a cluster.
7. Which NoSQL type suits a "people you may know" feature?
A graph database, because the feature is a multi-hop traversal of relationships (friends of friends), which graph databases handle efficiently.
Summary
NoSQL = non-relational databases built for flexible data and horizontal scaling. It means "not only SQL", not "anti-SQL".
Four main types: key-value, document, wide-column and graph, each suited to different access patterns.
NoSQL designs often denormalise around queries instead of normalising around entities.
BASE (basically available, soft state, eventually consistent) contrasts with ACID, though many NoSQL systems now offer transactions too.
Relational databases remain the right default for structured, integrity-critical data; many systems combine both (polyglot persistence).
Pick the data model that matches how your data is read and written, not the one that is fashionable.