Skip to main content

📦 NoSQL

Flexible storage without rigid tables

The Filing Cabinet Analogy

SQL: Strict Filing Rules

In a strict system, you might expect every document to have:
  - Name in slot 1
  - Date in slot 2
  - Amount in slot 3

If a document is different, you usually need to change the structure first.

NoSQL: Flexible Folders

Each folder can contain different things:
  - Folder 1: Invoice with 5 fields
  - Folder 2: Invoice with 3 fields + attachments
  - Folder 3: Completely different document

Whatever you need!

NoSQL databases let you store data without predefined schemas.


Why NoSQL?

The Limitations of SQL

Challenge 1: Schema changes
  Adding a column to 1 billion rows = nightmare

Challenge 2: Scaling horizontally
  SQL databases prefer vertical scaling (bigger server)
  NoSQL designed for horizontal scaling (more servers)

Challenge 3: Flexible data
  User profiles with varying fields
  Nested objects, arrays

SQL forces everything into rigid tables.

NoSQL's Answer

âś“ Flexible schema: Change data structure easily
âś“ Horizontal scaling: Add more servers
âś“ Specialized: Right database for right job
âś“ Performance: Optimized for specific use cases

Types of NoSQL Databases

1. Document Stores

Store JSON-like documents:

{
  "_id": "user123",
  "name": "Alice",
  "email": "alice@example.com",
  "addresses": [
    { "city": "NYC", "zip": "10001" },
    { "city": "LA", "zip": "90001" }
  ],
  "preferences": {
    "theme": "dark",
    "notifications": true
  }
}

Nested data, arrays - no problem!

Examples: MongoDB, CouchDB, Firestore
Commonly used for: Content management, catalogs, user profiles

2. Key-Value Stores

Simple: key → value

user:123 → "Alice"
session:abc → { "userId": 123, "expires": "..." }
cache:page:home → "<html>...</html>"

Super fast! O(1) lookups.

Examples: Redis, DynamoDB, Memcached
Commonly used for: Caching, sessions, real-time data

3. Column-Family Stores

Group related columns together:

Row key: user123
  Personal: { name: "Alice", email: "alice@..." }
  Settings: { theme: "dark", notifications: true }
  Activity: { last_login_days_ago: 12, posts_last_30_days: 47 }

Optimal for reading specific column groups.

Examples: Cassandra, HBase, ScyllaDB
Commonly used for: Time-series, analytics, IoT data

4. Graph Databases

Nodes and relationships:

(Alice) --[FRIENDS_WITH]--> (Bob)
(Alice) --[WORKS_AT]--> (Tech Corp)
(Bob) --[WORKS_AT]--> (Tech Corp)

Query: "Friends of friends who work at same company"
Often easier with graphs than with multiple SQL JOINs.

Examples: Neo4j, Amazon Neptune, ArangoDB
Commonly used for: Social networks, recommendations, fraud detection

NoSQL Comparison

TypeData ModelStrengthsExamples
DocumentJSON documentsFlexibilityMongoDB
Key-ValueKey → ValueSpeed, simplicityRedis
ColumnColumn familiesAnalyticsCassandra
GraphNodes + edgesRelationshipsNeo4j

Schema-Less Doesn't Mean "No Structure"

The Reality

"Schema-less" is misleading:

Your application still expects certain fields
You'll have implicit schemas in your code
Just not enforced by the database

Better term: "Schema-flexible"

Validation Options

Many NoSQL databases support optional schemas:
  - MongoDB Schema Validation
  - JSON Schema
  - Application-level validation

It's a good idea to validate important data.

When to Use NoSQL

Good Fit

âś“ Rapidly evolving data structure
âś“ Need to scale horizontally
âś“ Working with hierarchical/nested data
âś“ High write throughput needed
âś“ Caching and session management
âś“ Real-time analytics
âś“ Graph-shaped problems

Not a Good Fit

âś— Complex transactions across multiple tables
âś— Strong consistency requirements
âś— Heavy use of JOINs
âś— Financial/banking systems (usually)
âś— Team primarily knows SQL and isn't ready to support a new system

CAP Theorem Trade-offs

A Common Framing: Pick Two

C - Consistency: Reads tend to reflect the latest write
A - Availability: The system keeps responding
P - Partition Tolerance: Works despite network splits

SQL typical: CP (Consistent + Partition Tolerant)
NoSQL typical: AP (Available + Partition Tolerant)

Eventual Consistency

NoSQL often uses "eventual consistency":

Write to one node
  → Eventually propagates to all nodes
  → Brief window where reads might be stale

Fine for: Social media, analytics
Not fine for: Bank balances, inventory

MongoDB

Document store, JSON-like documents
Good for: General purpose, startups
Query language: MQL (MongoDB Query Language)

Redis

Key-value with rich data structures
Good for: Caching, sessions, real-time
In-memory, blazing fast

Cassandra

Column-family store
Good for: Massive scale, time series
Used by: Netflix, Apple, Uber

Neo4j

Graph database
Good for: Social networks, recommendations
Query language: Cypher

Common Mistakes

1. NoSQL for Everything

NoSQL isn't automatically better:
  - Banking? Use SQL
  - Complex queries? Use SQL
  - Need JOINs? Use SQL

2. Ignoring Data Modeling

Flexible doesn't mean unplanned.
Model for your query patterns!

3. Expecting SQL Features

No transactions (usually)
No complex JOINs
Different query optimization

4. Not Planning for Consistency

Eventual consistency can be surprising!
Test your application's behavior.

FAQ

Q: Can I use SQL and NoSQL together?

Yes! Often called "polyglot persistence." Use each where it fits well.

Q: How does NoSQL performance compare to SQL?

Depends on use case. NoSQL often wins for simple reads/writes at scale. SQL wins for complex queries.

Q: Do I still need to learn SQL?

Yes! SQL is everywhere and not going away. Learn both.

Q: What about migrations?

NoSQL makes schema changes easier, but you still need data migration strategies.


Summary

NoSQL databases offer flexible, scalable alternatives to traditional SQL databases.

Key Takeaways:

  • Four types: Document, Key-Value, Column, Graph
  • Flexible schema vs fixed tables
  • Horizontal scaling friendly
  • Often trades consistency for availability
  • Choose based on your data and queries
  • Not a replacement for SQL - a complement

The right database depends on your problem!

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.