Modern Databases
For decades, the relational database (RDBMS) was the only option. Today, the database landscape is diverse. Different workloads require different database designs.
This chapter surveys the modern database ecosystem — why alternative databases exist, what problems they solve, and when to use them.
Learning Objectives
After completing this chapter, you will be able to:
- Describe the limitations of RDBMS for modern workloads.
- Classify NoSQL databases by their data model.
- Understand when to use document, key-value, column-family, and graph databases.
- Explain NewSQL databases and the CAP theorem tradeoffs.
- Understand time-series and multi-model databases.
- Choose the right database for a given workload.
- Answer database selection interview questions.
The Database Landscape
┌─────────────────────────────┐
│ Relational (SQL) │
│ PostgreSQL, MySQL, Oracle │
└─────────────────────────────┘
↓
┌─────────────────┼─────────────────┐
▼ ▼ ▼
┌──────────┐ ┌────────────┐ ┌──────────┐
│ NoSQL │ │ NewSQL │ │ Special │
│ Document │ │ CockroachDB│ │ Time-Ser. │
│ Key-Val │ │ Spanner │ │ Graph │
│ Column │ │ TiDB │ │ Vector │
│ Graph │ └────────────┘ └──────────┘
└──────────┘
Why Not RDBMS for Everything?
RDBMS was designed in the 1970s for a different world.
| Limitation | Problem |
|---|---|
| Rigid schema | Adding columns requires ALTER TABLE (downtime or locks). |
| Vertical scaling | Scaling up (bigger machine) is expensive and has limits. |
| Single-master writes | Most RDBMS have one write master — bottleneck for global apps. |
| Complex replication | Setting up replication and sharding is manual and error-prone. |
| JSON/Unstructured data | RDBMS struggles with nested, variable-schema data. |
| High write throughput | RDBMS handles thousands of writes/sec per server; modern apps need millions. |
NoSQL Databases
NoSQL = Not Only SQL.
NoSQL databases are designed for specific workloads that RDBMS handles poorly.
Types of NoSQL
| Type | Data Model | Best For | Examples |
|---|---|---|---|
| Document | JSON/BSON documents | CMS, catalogs, user profiles | MongoDB, CouchDB |
| Key-Value | Key → Value pairs | Caching, sessions, leaderboards | Redis, DynamoDB |
| Column-Family | Rows with variable columns | Time-series, analytics, IoT | Cassandra, HBase |
| Graph | Nodes and edges | Social networks, recommendations | Neo4j, ArangoDB |
Document Databases (MongoDB)
Data is stored as JSON-like documents. Each document can have a different structure.
{
"_id": "user_101",
"name": "Rahul",
"email": "rahul@mail.com",
"addresses": [
{ "city": "Mumbai", "type": "home" },
{ "city": "Pune", "type": "work" }
],
"preferences": {
"theme": "dark",
"notifications": true
}
}
Schema flexibility: Different documents can have different fields. Query: Rich queries on any field (similar to SQL but for JSON).
Key-Value Databases (Redis)
Data is stored as a simple key-value pair.
SET user:101:name "Rahul"
SET user:101:email "rahul@mail.com"
GET user:101:name → "Rahul"
Performance: Extremely fast (sub-millisecond) — data is in memory. Use cases: Caching, session stores, real-time counters, rate limiting. Operations: Simple read/write by key. Complex queries require application logic.
Column-Family Databases (Cassandra)
Data is stored by column family, not by row.
Users:
Row Key: "101"
Name: "Rahul"
Email: "rahul@mail.com"
Row Key: "102"
Name: "Priya"
City: "Delhi"
Flexibility: Each row can have different columns. Scalability: Linear write scaling — can handle millions of writes per second. Use cases: IoT sensor data, logs, time-series, recommendation engines.
Graph Databases (Neo4j)
Data is stored as nodes (entities) and edges (relationships).
(Employee: Rahul) -[:WORKS_IN]-> (Department: Engineering)
(Employee: Rahul) -[:MANAGES]-> (Project: DBMS)
Query: Traverse relationships efficiently.
MATCH (e:Employee {name: "Rahul"})-[:MANAGES]->(p:Project)
RETURN p.name
Use cases: Social networks (friends-of-friends), fraud detection (transaction patterns), recommendation engines (product relationships).
NewSQL Databases
NewSQL databases aim to provide the scalability of NoSQL with the ACID guarantees of SQL.
Examples
| Database | Key Feature |
|---|---|
| CockroachDB | Geo-distributed, auto-sharding, SQL |
| Google Spanner | Globally distributed, TrueTime clocks |
| TiDB | MySQL-compatible, horizontal scaling |
| VoltDB | In-memory, stored procedures |
Why NewSQL?
- SQL interface (no learning new query languages).
- ACID transactions (strong consistency).
- Horizontal scaling (auto-sharding, multi-region).
Time-Series Databases
Optimized for storing and querying time-stamped data (metrics, sensor readings, logs).
Examples
| Database | Description |
|---|---|
| InfluxDB | Purpose-built TSDB, continuous queries |
| TimescaleDB | PostgreSQL extension, hybrid |
| Prometheus | Cloud-native monitoring, pull model |
| QuestDB | High-performance, columnar |
Query Example (InfluxDB)
SELECT MEAN(temperature) FROM sensors
WHERE time > now() - 1h
GROUP BY time(5m), device_id
Database Selection Guide
| Workload | Recommended Database |
|---|---|
| E-commerce (orders, payments) | PostgreSQL, MySQL (ACID required) |
| Real-time analytics | ClickHouse, Druid |
| User sessions, caching | Redis (in-memory) |
| IoT sensor data (write-heavy) | Cassandra, InfluxDB |
| Social network, recommendations | Neo4j (graph) |
| Content management, catalogs | MongoDB (flexible schema) |
| Global, multi-region app | CockroachDB, Spanner |
| Messaging, event streaming | Kafka (log-based) |
| Vector similarity search | Pinecone, Qdrant |
Interview Deep Dive
Q: When would you choose a NoSQL database over a relational database?
A: Choose NoSQL when: (1) Your data has a variable schema (different documents have different fields), (2) You need horizontal write scaling beyond what a single RDBMS server can handle, (3) You need very low latency for simple lookups (key-value), or (4) Your data is inherently a graph. Choose SQL when you need ACID transactions, complex joins, and strong consistency.
Q: What is polyglot persistence?
A: Polyglot persistence is the practice of using multiple database types within the same application, each optimized for a specific workload. Example: An e-commerce app uses PostgreSQL for orders (ACID), Redis for the shopping cart (fast reads/writes), Elasticsearch for product search, and Neo4j for product recommendations. Each database handles what it does best.
Q: Do NoSQL databases sacrifice consistency for availability?
A: Many NoSQL databases prioritize availability and partition tolerance over strong consistency (eventual consistency). For example, DynamoDB and Cassandra offer tunable consistency — you can choose between eventual and strong consistency per query. MongoDB offers strong consistency within a single replica set. The tradeoff is part of the CAP theorem.
Q: Why would you use NewSQL instead of NoSQL?
A: Use NewSQL when you need both horizontal scaling and ACID transactions. NoSQL typically sacrifices transactions for scalability. NewSQL (CockroachDB, Spanner) provides SQL, ACID, and horizontal scale. The tradeoff is higher latency (due to distributed consensus) and operational complexity compared to a single-node RDBMS.
Key Takeaways
- RDBMS is not the only option — different workloads need different databases.
- NoSQL includes document, key-value, column-family, and graph databases.
- Document databases offer schema flexibility (MongoDB).
- Key-value databases offer extreme speed (Redis).
- Column-family databases offer write scalability (Cassandra).
- Graph databases handle relationships efficiently (Neo4j).
- NewSQL combines SQL + ACID + horizontal scaling.
- Time-series databases optimize for timestamped metrics.
- Polyglot persistence uses multiple databases in one application.
- Choose a database based on your workload requirements, not trends.
Premium Content
Unlock Modern Databases and all premium lessons with a subscription.
From ₹199.99/year — See plans