Skip to content

NoSQL Overview

Document, key-value, wide-column, and graph databases — when to look beyond SQL.

What you'll learn

  • What NoSQL means and why it emerged
  • The four main NoSQL types: document, key-value, wide-column, graph
  • When NoSQL is a better fit than a relational database
  • The CAP theorem and the trade-off between consistency and availability
  • Popular NoSQL databases and their use cases

Concept

What Is NoSQL?

NoSQL ("Not Only SQL") refers to databases that do not use the relational table model. They emerged in the 2000s to address needs that relational databases struggled with: massive horizontal scale, flexible/unstructured data, and rapid schema evolution. NoSQL databases trade some guarantees (like strict consistency and joins) for flexibility, scale, or performance.

The Four Types

| Type | Example | Data model | Best for | |------|---------|------------|----------| | Document | MongoDB, CouchDB | JSON-like documents | Content management, catalogs, flexible schemas | | Key-Value | Redis, DynamoDB | Key → value pairs | Caching, sessions, real-time lookups | | Wide-Column | Cassandra, HBase | Column families | Time-series, heavy writes, large scale | | Graph | Neo4j, Amazon Neptune | Nodes and edges | Social networks, recommendations, fraud detection |

When to Choose NoSQL

NoSQL is a good fit when: your data is naturally hierarchical or unstructured (JSON documents), your schema evolves rapidly, you need horizontal scale across many servers, or you have extremely high write throughput. Relational databases are better when: your data is highly structured, you need complex joins and transactions, or strong consistency is non-negotiable.

The CAP Theorem

The CAP theorem states that a distributed database can provide at most two of three guarantees: Consistency (every read sees the latest write), Availability (every request gets a response), and Partition tolerance (the system works despite network failures). Since partitions are inevitable in distributed systems, the real choice is CP (consistency + partition tolerance) vs AP (availability + partition tolerance). Most NoSQL databases choose AP; relational databases traditionally choose CP.

BASE vs ACID

NoSQL databases often follow the BASE model: Basically Available, Soft state, Eventually consistent. This contrasts with ACID — instead of immediate consistency, the database converges to consistency over time. This is acceptable for many use cases (social feeds, product catalogs) but not for financial transactions.

Example

              -- MongoDB: insert a flexible document (no fixed schema)
db.users.insertOne({
  name: "Ada Lovelace",
  email: "ada@example.com",
  roles: ["admin", "editor"],
  profile: {
    bio: "Mathematician and writer",
    skills: ["math", "logic"],
    location: "London"
  },
  createdAt: new Date()
});

-- Redis: set and get a key-value pair
SET session:abc123 "user_id=42"
EX 3600  -- expire in 1 hour

GET session:abc123

-- Cassandra: a wide-column insert
INSERT INTO events (event_id, user_id, event_type, timestamp)
VALUES (uuid(), 42, 'page_view', toTimestamp(now()));
            

Three NoSQL patterns: a MongoDB document insert with nested data, a Redis key-value with TTL, and a Cassandra wide-column insert.

Try it

Common mistakes

The mistake

Choosing NoSQL just because it is trendy

The fix

Start with a relational database. Choose NoSQL only when you have a concrete need that SQL cannot meet: flexible schema, massive horizontal scale, or a specific data model (graph, time-series).

The mistake

Assuming NoSQL means no schema at all

The fix

Even document databases benefit from a schema — for validation, indexing, and code clarity. Use JSON Schema or database-level validators to enforce structure.

Practice NoSQL Overview

3 exercises

Practice NoSQL Overview

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors