TechBlog
system-design

SQL vs NoSQL — How to Choose

A practical guide to choosing between SQL and NoSQL databases. Understand the trade-offs, the different NoSQL types, and how to make the right call for your system.

5 min read

The Core Difference

SQL databases (relational) store data in tables with a fixed schema. Relationships between data are defined upfront. They enforce ACID transactions.

NoSQL databases store data in flexible formats — documents, key-value pairs, wide columns, or graphs. Schema is flexible. They typically trade consistency for scale and performance.

Neither is universally better. The right choice depends on your data model, access patterns, and scale requirements.


SQL Databases

How they work

Data is organized into tables (rows and columns). Relationships are expressed through foreign keys and joins.

-- Users table
id | name  | email
1  | Alice | alice@example.com
 
-- Orders table
id | user_id | total | created_at
1  | 1       | 99.99 | 2025-01-01

Strengths

Weaknesses

Best for

Examples: PostgreSQL, MySQL, SQLite, Oracle, SQL Server


NoSQL Databases

NoSQL is not one thing — it's four distinct types, each optimized for different use cases.

1. Document Stores

Store data as JSON/BSON documents. Each document can have a different structure.

{
  "_id": "user_123",
  "name": "Alice",
  "address": { "city": "Mumbai", "zip": "400001" },
  "orders": [{ "id": "ord_1", "total": 99.99 }]
}

Strengths: Flexible schema, natural fit for object-oriented data, easy to scale reads Weaknesses: No joins (must denormalize), eventual consistency by default Use when: Content management, user profiles, product catalogs Examples: MongoDB, CouchDB, Firestore


2. Key-Value Stores

The simplest NoSQL type — a giant hash map. Store and retrieve values by key.

SET user:123:session "abc123xyz" EX 3600
GET user:123:session → "abc123xyz"

Strengths: Extremely fast (O(1) reads/writes), simple, scales easily Weaknesses: No query capability beyond key lookup, no relationships Use when: Caching, sessions, rate limiting, leaderboards Examples: Redis, DynamoDB (as key-value), Memcached


3. Wide-Column Stores

Store data in tables but columns can vary per row and are grouped into column families. Optimized for writes and time-series data.

Row key: user_123
  activity:2025-01-01 → "login"
  activity:2025-01-02 → "purchase"
  activity:2025-01-03 → "logout"

Strengths: Massive write throughput, excellent for time-series, scales to petabytes Weaknesses: Limited query patterns, no joins, eventual consistency Use when: IoT data, activity feeds, analytics, time-series Examples: Cassandra, HBase, Google Bigtable


4. Graph Databases

Store data as nodes and edges. Optimized for traversing relationships.

(Alice) -[FOLLOWS]→ (Bob)
(Bob) -[FOLLOWS]→ (Charlie)
Query: Who does Alice follow transitively?

Strengths: Natural for relationship-heavy data, fast graph traversals Weaknesses: Niche use case, harder to scale, less tooling Use when: Social networks, recommendation engines, fraud detection, knowledge graphs Examples: Neo4j, Amazon Neptune, ArangoDB


Decision Framework

Ask these questions in order:

1. Do you need ACID transactions?

2. What is your data model?

3. What are your scale requirements?

4. How flexible is your schema?


Common Combinations in Production

Most large systems use multiple databases:

DataDatabaseWhy
Users, orders, paymentsPostgreSQLACID, relational
Sessions, rate limitsRedisFast key-value
Product searchElasticsearchFull-text search
Activity/eventsCassandraHigh write throughput
RecommendationsNeo4jGraph traversal

Key Takeaway

SQLNoSQL
SchemaFixedFlexible
TransactionsACIDEventual (usually)
ScalingVertical (mostly)Horizontal
QueriesFlexible (any JOIN)Limited to access patterns
Best forRelational, transactionalScale, flexibility, specific patterns

Default to SQL (PostgreSQL) unless you have a specific reason not to. It handles most use cases well and is easier to reason about. Add NoSQL databases when you hit specific limitations.