Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Modern Databases
DBMS

Modern Databases

Understand the landscape of modern databases beyond traditional RDBMS — NoSQL, NewSQL, time-series, graph, and multi-model databases — and when to use each.

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.

LimitationProblem
Rigid schemaAdding columns requires ALTER TABLE (downtime or locks).
Vertical scalingScaling up (bigger machine) is expensive and has limits.
Single-master writesMost RDBMS have one write master — bottleneck for global apps.
Complex replicationSetting up replication and sharding is manual and error-prone.
JSON/Unstructured dataRDBMS struggles with nested, variable-schema data.
High write throughputRDBMS 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

TypeData ModelBest ForExamples
DocumentJSON/BSON documentsCMS, catalogs, user profilesMongoDB, CouchDB
Key-ValueKey → Value pairsCaching, sessions, leaderboardsRedis, DynamoDB
Column-FamilyRows with variable columnsTime-series, analytics, IoTCassandra, HBase
GraphNodes and edgesSocial networks, recommendationsNeo4j, 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

DatabaseKey Feature
CockroachDBGeo-distributed, auto-sharding, SQL
Google SpannerGlobally distributed, TrueTime clocks
TiDBMySQL-compatible, horizontal scaling
VoltDBIn-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

DatabaseDescription
InfluxDBPurpose-built TSDB, continuous queries
TimescaleDBPostgreSQL extension, hybrid
PrometheusCloud-native monitoring, pull model
QuestDBHigh-performance, columnar

Query Example (InfluxDB)

SELECT MEAN(temperature) FROM sensors
WHERE time > now() - 1h
GROUP BY time(5m), device_id

Database Selection Guide

WorkloadRecommended Database
E-commerce (orders, payments)PostgreSQL, MySQL (ACID required)
Real-time analyticsClickHouse, Druid
User sessions, cachingRedis (in-memory)
IoT sensor data (write-heavy)Cassandra, InfluxDB
Social network, recommendationsNeo4j (graph)
Content management, catalogsMongoDB (flexible schema)
Global, multi-region appCockroachDB, Spanner
Messaging, event streamingKafka (log-based)
Vector similarity searchPinecone, 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.

My Private Notes

Notes are auto-saved locally to this device.