Databases: SQL vs NoSQL

Postgres or MongoDB or Cassandra? Database types, their strengths, and how to choose.

Intermediate8 min read
Language
Theme
SQLPostgres: ACID + joinsKey-valueRedis, DynamoDBDocumentMongoDBWide-columnCassandraGraphNeo4jChoose with 3 questions:1. What shape is the data? 2. How much scale? 3. Need transactions?Real systems are polyglot: Postgres + Redis + Elasticsearch together
Key idea at a glance

SQL: what you actually get

PostgreSQL, MySQL: tables with a fixed schema, and three superpowers:

  • ACID transactions: transfer money = debit + credit; both happen or neither. Atomicity, Consistency, Isolation, Durability. Non-negotiable for payments, bookings, inventory.
  • Joins + ad-hoc queries: ask new questions of old data without redesigning it.
  • 50 years of maturity: tooling, replication, backups, edge cases already solved.

The costs: horizontal scaling (sharding) is manual, and schema changes on huge tables are operational events.

NoSQL is four different databases wearing a trenchcoat

  • Key-value (Redis, DynamoDB): get/put by key, single-digit-ms at any scale. Sessions, carts, rate-limit counters, caching. No joins, no rich queries: you design around the key.
  • Document (MongoDB): JSON documents, flexible schema, secondary indexes. Product catalogs, user profiles, content with varying shapes. Related data is embedded in the document instead of joined.
  • Wide-column (Cassandra, HBase, Bigtable): rows partitioned across nodes, tuned for massive write volume and time-series: chat messages, IoT sensor data, event logs. Multi-datacenter replication built in; queries must match the partition key design.
  • Graph (Neo4j): relationships are first-class: friends-of-friends, fraud rings, recommendations are one traversal instead of 14 joins.

The decision framework (use it out loud in interviews)

  1. Data shape: highly relational with transactions (orders, payments) = SQL. Self-contained flexible records = document. Pure key lookups = key-value. Time-series/firehose writes = wide-column. The queries ARE relationships = graph.
  2. Scale: sustained millions of writes/sec or multi-region writes = Cassandra/DynamoDB. Normal web app = Postgres, which scales much further than people think (tens of thousands of reads/sec with replicas).
  3. Transactions: money and inventory = ACID SQL. Counters, likes, presence = NoSQL is fine.

Polyglot persistence: the real-world answer

One production e-commerce system might run: Postgres for orders and payments (ACID), Redis for sessions + cart + hot cache, Elasticsearch for product search (it is a search engine, not a primary store), Cassandra or Kafka for the event/clickstream firehose, S3 for images. "Which database is best?" is the wrong question; "which is best for THIS data and access pattern?" is the job.

Worked example: chat app storage

Messages: write-heavy, time-ordered, read by conversation, billions of rows: wide-column (Cassandra), partition key = conversation_id, clustering by timestamp. User profiles and contacts: Postgres. Online presence: Redis with TTL heartbeats. Media: S3 + CDN. Four databases, each chosen by access pattern.

Common interview traps

  • MongoDB "because it scales" for clearly relational, transactional data.
  • "NoSQL can't do transactions" (many now offer scoped ones) or "SQL can't scale" (read replicas go very far).
  • Picking by hype instead of access pattern.

Interview Q&A

Q: When would you pick Cassandra over Postgres?
A: When the workload is write-dominated time-series at a scale a single primary cannot absorb (hundreds of thousands of writes/sec), multi-region writes are required, and queries are known in advance and partition-key friendly. Chat messages and sensor data are the textbook fits.

Q: Why not just use Elasticsearch as the main database?
A: It is a search index: near-real-time refresh, weaker durability and transactional guarantees, expensive as a source of truth. Pattern: source of truth in Postgres, search index in Elasticsearch, synced via a pipeline.

Key takeaways

Interview pointer

When picking a database, give the reason, not just the name: 'Postgres for orders because ACID matters, Cassandra for the feed because it is write-heavy time-series, Elasticsearch for search.' The justification is what scores.