NoSQL (Not Only SQL) databases handle unstructured/semi-structured data at massive scale. Unlike rigid relational tables, NoSQL offers flexible schemas for big data, real-time apps. Popular: MongoDB, Cassandra, Redis. Born from web 2.0 needs (Facebook, Google).

What is NoSQL Database?

NoSQL = Non-relational databases prioritizing:

  • Scalability (horizontal - add cheap servers).

  • Flexibility (schema-on-read).

  • Performance (for massive reads/writes).

Key Difference: No fixed tables/keys—data as JSON docs, key-value pairs, graphs.

Real-life analogy: NoSQL is a flexible warehouse with mixed boxes vs relational's rigid filing cabinets.

Why NoSQL? (Problems with RDBMS)

text
RDBMS Struggles: - Joins kill performance at scale - Schema changes = downtime - Vertical scaling = $$ hardware - ACID too slow for 1M writes/sec NoSQL Solves: - Horizontal scaling (add servers) - Eventual consistency (BASE vs ACID) - Handle variety (text, images, logs)

Types of NoSQL Databases (4 Main Categories)

1. Document Stores

Data: JSON/BSON documents (flexible schema).
Use: Content management, e-commerce catalogs.

text
MongoDB Example: { "_id": 1, "name": "iPhone 15", "price": 79900, "specs": { "ram": "8GB", "camera": "48MP" }, "reviews": ["Great!", "Battery ok"] }

Examples: MongoDB, CouchDB.

2. Key-Value Stores

Data: Simple key → value (fastest lookups).
Use: Caching, sessions, leaderboards.

text
Redis Example: SET user:101 '{"name":"Aman","score":8500}' GET user:101 → Returns JSON

Examples: Redis, DynamoDB.

3. Column-Family (Wide-Column) Stores

Data: Columns grouped by row key (for analytics).
Use: Time-series, logs, recommendations.

text
Cassandra Example: RowKey: user_101 - purchases: [item1,time1], [item2,time2] - views: [page1,time1], [page2,time2]

Examples: Cassandra, HBase.

4. Graph Databases

Data: Nodes + edges (relationships).
Use: Social networks, fraud detection.

text
Neo4j Example: (Aman)-[:FRIENDS_WITH]->(Riya) (Riya)-[:WORKS_AT]->(Google) Query: Aman's friends at Google?

Examples: Neo4j, JanusGraph.

NoSQL vs SQL: Complete Comparison

FeatureSQL (Relational)NoSQL
SchemaFixed (schema-on-write)Dynamic (schema-on-read)
Query LanguageSQL (JOINs, transactions)Varies (no standard JOINs)
ScalingVertical (bigger server)Horizontal (more servers)
ConsistencyACID (Strong)BASE (Eventual)
Data StructureTables/RowsDocs, KV, Columns, Graphs
Best ForTransactions, complex queriesBig data, real-time, variety

CAP Theorem (NoSQL Reality Check)

In distributed systems, pick 2 of 3:

text
C = Consistency (all see same data) A = Availability (every request gets response) P = Partition Tolerance (network splits) SQL: CA (single server) NoSQL: AP/CP (distributed)

BASE Properties (NoSQL's ACID Alternative)

  • Basically Available: Always responds.

  • Soft state: Data may change.

  • Eventual consistency: Updates propagate eventually.

text
Example: Facebook posts - you see them fast, likes sync in seconds.
DatabaseTypeFamous UsersBest For
MongoDBDocumentAdobe, eBayContent, catalogs
RedisKey-ValueTwitter, GitHubCache, real-time analytics
CassandraColumnNetflix, AppleTime-series, IoT
Neo4jGraphWalmart, NASARecommendations, networks

Creating NoSQL Example (MongoDB)

javascript
// Insert flexible documents db.products.insertMany([ { name: "Laptop", price: 55000, specs: { ram: "16GB", storage: "1TB" } }, { name: "Phone", price: 25000, // No specs? No problem! } ]) // Query db.products.find({ price: { $lt: 30000 } })

When to Choose NoSQL Over SQL?

text
USE NoSQL WHEN: ✅ Unstructured/varied data (logs, IoT) ✅ Massive scale (>1TB, high writes) ✅ Rapid schema changes ✅ Global distribution STICK TO SQL WHEN: ✅ Complex transactions (banking) ✅ Strict consistency needed ✅ Known schema upfront ✅ <100GB data

Hybrid Approach: Microservices = SQL for orders + NoSQL for recommendations.

Advantages & Limitations

Pros:

  • Horizontal scaling (cheap commodity servers).

  • Handle schema evolution.

  • High throughput.

  • Big data friendly.

Cons:

  • Eventual consistency risks.

  • No standard query language.

  • Complex distributed operations.

  • Learning curve per type.

Summary

NoSQL = Flexible, scalable databases for modern big/unstructured data. 4 types: Document, KV, Column, Graph. Follows CAP/BASE vs SQL's ACID