Menu

Earn Premium with Referrals

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

See how it works and start inviting friends.

SQL vs NoSQL
SQL

SQL vs NoSQL

Compare relational and non-relational databases to choose the right tool for your application.

Choosing between SQL and NoSQL is one of the most important architectural decisions in software development. There is no “perfect” database, only the right one for your specific use case.

Comparison at a Glance

FeatureSQL (Relational)NoSQL (Non-Relational)
Data ModelTabular (Rows/Columns)Document, Key-Value, Graph
SchemaRigid/PredefinedFlexible/Dynamic
ScalingVertical (Scale Up)Horizontal (Scale Out)
ConsistencyACID (Strict)BASE (Eventual Consistency)
Best ForTransactions, Complex JoinsHuge Data, Real-time feeds
Query LanguageSQL (standardised)DB-specific APIs (MQL, CQL)
JoinsNative supportApplication-level or no joins

ACID vs BASE

ACID (SQL)BASE (NoSQL)
Atomic: All or nothingBasically Available: System stays up
Consistent: Invariants preservedSoft State: Data may change over time
Isolated: Concurrent transactionsEventually Consistent: Data converges
Durable: Survives crashes

NoSQL Types in Detail

Document Databases (MongoDB, CouchDB)

Store data as JSON-like documents. Fields can vary between documents.

{
  "_id": "123",
  "name": "Alice",
  "orders": [
    { "id": 1, "amount": 50 },
    { "id": 2, "amount": 75 }
  ]
}

Best for: Catalogs, content management, user profiles.

Key-Value Stores (Redis, DynamoDB)

Simple hash table: every item has a key and a value (blob).

Key: "session:alice"
Value: { "user_id": 42, "expires": "2024-12-31" }

Best for: Caching, session management, real-time leaderboards.

Wide-Column Stores (Cassandra, HBase)

Rows can have different columns. Columns are grouped into column families.

Row Key: "user_42"
  family:profile → {name: "Alice", email: "a@b.com"}
  family:stats  → {logins: 150, joined: "2023-01-01"}

Best for: Time-series data, IoT, large-scale write-heavy applications.

Graph Databases (Neo4j)

Data is stored as nodes (entities) and edges (relationships).

(Node:Person {name: "Alice"})
  ──[:FRIENDS_WITH]──→ (Node:Person {name: "Bob"})

Best for: Social networks, recommendation engines, fraud detection.

Choosing Between SQL and NoSQL

ScenarioRecommendation
Banking / PaymentsSQL (ACID requirements)
User session cacheNoSQL (Redis — fast key-value)
Product catalog (varying attributes)NoSQL (MongoDB — flexible schema)
Social network graph queriesNoSQL (Neo4j — graph traversal)
Monthly financial reportsSQL (complex joins, aggregations)
IoT sensor data (millions of writes/s)NoSQL (Cassandra — high write throughput)
E-commerce platformBoth: SQL for orders, NoSQL for product catalog

Q: When should you use NoSQL?

A:

  • When your data is unstructured or has a fuzzy schema.
  • When you need to handle massive amounts of data (Tera/Peta bytes).
  • When you need high write throughput and don’t require immediate consistency.
  • When your data naturally fits a graph or hierarchical model.

Q: What are the types of NoSQL?

A:

  1. Document: Stores data in JSON-like docs (MongoDB).
  2. Key-Value: Simple hash tables (Redis).
  3. Columnar: Optimized for reading large amounts of data (Cassandra).
  4. Graph: Focuses on relationships (Neo4j).

Q: SQL or NoSQL for a Banking System?

A: SQL. Banking requires absolute data integrity and ACID compliance. You cannot afford “eventual consistency” when it comes to account balances.

Q: What is polyglot persistence?

A: Using different types of databases for different parts of the same application. E.g., PostgreSQL for transactions, Redis for caching, Elasticsearch for search.

1. Design a Chat App (WhatsApp/Slack).

Choice: NoSQL (Wide-Column/Key-Value). Reason: Chat produces a massive volume of small writes. Horizontal scaling is required to handle millions of concurrent users. Consistency can be eventual—seeing a message a second late is better than the system being down.

2. Design a Logging System with heavy writes.

Choice: NoSQL (Document or Wide-Column). Reason: Logs are often semi-structured and generate huge volumes of data. NoSQL engines like Elasticsearch or Cassandra are optimized for fast writes.

3. Compare MongoDB vs PostgreSQL use case.

PostgreSQL: Use when you need complex reporting, have highly inter-related data, and need strict consistency. MongoDB: Use when you need to prototype quickly without a fixed schema or when you have large amounts of data that don’t transition well to tables.

4. Design an E-commerce database architecture.

Answer: Use PostgreSQL for orders, payments, and inventory (ACID required). Use MongoDB for the product catalog (flexible attributes per category). Use Redis for cart sessions and product view counts.

My Private Notes

Notes are auto-saved locally to this device.