All articles
165 articles · updated weekly See our Tools
All articles
Comparisons

Relational vs NoSQL: How to Actually Choose

SQL or NoSQL? Use relational for relationships, reports and transactions; NoSQL for cache, write scale or graphs. Real tradeoffs and why to start with SQL.

Relational vs NoSQL: How to Actually Choose
COVER · Comparisons

The architecture meeting ran two hours. At the end, someone said "let's use MongoDB because it's more flexible" and everyone agreed without discussing what "flexible" meant in that context. Six months later, the team was doing manual joins in application code for a report that would have taken two seconds in SQL. I've seen this movie a few times.

The short answer to "SQL or NoSQL?": use a relational database when your data relates to itself, you need reports, and you need transactions that can't be left half-done — which describes most business systems. Use a non-relational (NoSQL) database for a specific problem relational handles poorly: cache and sessions, distributed write volume at very high scale, graph traversal. When in doubt, start with SQL and add NoSQL when the problem actually shows up.

This post is about database families. If you're choosing between two relational databases, its sibling is PostgreSQL vs MySQL; if it's specifically MongoDB, read when to use MongoDB.

Relational vs non-relational, in one table

Relational (SQL) Non-relational (NoSQL)
Data model Tables with rows and columns, relations via foreign keys Document, key-value, wide-column or graph — depends on the database
Schema Defined in the database (DDL, constraints) Flexible in the database; in practice it lives in the application
JOINs Native, optimized by the planner Usually absent or limited; you denormalize
Transactions ACID across any rows and tables Varies: from none to ACID with limits (per partition, per region, per item count)
Scaling Vertical + read replicas; sharding needs extra tooling Horizontal as a design premise in many of them
Consistency Strong by default Often eventual by default, sometimes tunable
Examples PostgreSQL, MySQL, SQLite, SQL Server MongoDB, Redis, DynamoDB, Cassandra, Neo4j

The table is an honest simplification, not a rule. Several NoSQL databases have gained transactions in recent years, and PostgreSQL stores JSON documents better than many people assume. What still holds is the axis of the decision: how your data relates and how you'll query it.

When to use a relational database (SQL)

A relational database is the right choice for most business systems. If the data has a known structure, relationships between entities, and needs transactional consistency, relational handles it better than the alternatives.

  • Financial data: transfers, invoices, balances. You need ACID — atomicity, consistency, isolation, durability. A transfer that debits one account without crediting the other is a disaster. The relational model dates from 1970 (Codd's paper); transaction theory and the ACID acronym took shape between the late 70s and 1983. This problem has been solved for almost 50 years.
  • Systems with many relationships: users have orders, orders have line items, items have products, products have categories. SQL was designed for this, and JOINs are efficient when indexes are right — your choice of natural or surrogate primary key matters here too.
  • Reporting and analysis: GROUP BY, window functions, CTEs. Replicating that in application code is reinventing the wheel, poorly.
  • Schema that evolves in a controlled way: migrations with ALTER TABLE are predictable and auditable. Knowing status only accepts four values because there's a constraint in the database is a guarantee a schema-free document won't give you.

For small projects and desktop or mobile apps you may not even need a database server — that's the SQLite case.

PostgreSQL with JSONB: "flexible" without switching databases

The flexibility argument tends to ignore that PostgreSQL stores binary JSON (JSONB) with indexing. But the index has to match the query's operator, and that's a mistake I've made myself — including in an earlier version of this post.

CREATE TABLE events (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  type TEXT NOT NULL,
  payload JSONB NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX events_payload_gin ON events USING GIN (payload);

-- Does NOT use the GIN index: ->> is not indexable by jsonb_ops
SELECT * FROM events WHERE payload->>'user_id' = '123';

-- Uses the GIN index: containment (@>) is supported
SELECT * FROM events WHERE payload @> '{"user_id": "123"}';

-- Alternative: an expression (B-tree) index for the field you always filter on
CREATE INDEX events_user_id ON events ((payload->>'user_id'));

I ran this on PostgreSQL 18 with 500,000 rows. With only the GIN index, the ->> query did a Parallel Seq Scan (~21 ms); the same lookup with @> did a Bitmap Index Scan on events_payload_gin (~0.4 ms). After creating the expression index, the ->> query switched to it. The PostgreSQL documentation lists the operators supported by the default jsonb_ops operator class: ?, ?|, ?&, @>, @? and @@. Always check with EXPLAIN before assuming the index is doing its job.

When the payload is large and you need to read what arrived, pasting it into a JSON formatter saves time finding the exact key path that goes into the @>.

Types of non-relational (NoSQL) databases

NoSQL isn't one category. It's an umbrella over quite different models, each good at one thing.

Document (MongoDB, Firestore, CouchDB)

Makes sense when the data is variable and you almost always read it as a whole document. A product catalog is the classic example: sneakers have sizes and soles, laptops have CPUs and RAM. MongoDB guarantees atomicity per document and has had multi-document ACID transactions since 4.0 (replica sets) and 4.2 (sharded clusters) — but its own documentation warns they cost more than single-document writes and don't replace good schema design. The full tradeoff, including the direct comparison with Postgres, is in when to use MongoDB.

Key-value (Redis, DynamoDB) — and Memcached

Redis shines as cache, sessions, rate limiting, light queues and pub/sub, with in-memory operations. The simple model is what makes it fast and what makes it bad at complex queries. The use cases are in Redis: cache, sessions, queues and other use cases. Memcached usually shows up on the same list, but it isn't a database: it describes itself as a "distributed memory object caching system". Restart it and everything is gone — which is the point.

DynamoDB isn't just key-value: AWS documents that it supports both key-value and document models, and it has ACID transactions (TransactWriteItems, launched in 2018) covering up to 100 items, in one or more tables, in the same account and region, with no guarantee across regions in global tables. What it lacks is JOIN — and that's where the rigidity lives: you need to know your access patterns before modeling the table.

Wide-column (Cassandra, HBase)

Cassandra exists for very high write volume distributed across datacenters: IoT metrics, clickstream, logs at billions-of-records scale. Consistency is tunable per operation, but the design favors availability.

Saying "Cassandra has no transactions" is inaccurate. It has lightweight transactions — INSERT ... IF NOT EXISTS and UPDATE ... IF, implemented with Paxos at a non-negligible performance cost — and BATCH, which is isolated within a single partition. The DML documentation says outright that batches are "not a full analogue for SQL transactions". What's missing is the arbitrary transaction across partitions and tables that SQL gives you for free. And the schema is modeled from the queries: change the query, change the table.

Graph (Neo4j, Amazon Neptune)

For data where the relationship is the main information: social networks, connection-based recommendations, fraud detection, dependency graphs. SQL can model a graph with an adjacency table and a recursive CTE, but "friends of friends of friends" gets more expensive at every hop. A graph database solves traversal by design.

The tradeoffs nobody mentions in the sales pitch

Eventual consistency: many distributed NoSQL databases read from replicas by default, and a read right after a write can come back stale. Even DynamoDB, after a transaction, may return the old state on an eventually consistent read — AWS recommends ConsistentRead when that matters. For a like, fine. For a balance, no.

No schema = schema in the application: no schema in the database doesn't mean no schema. When the format changes, documents in format A and format B coexist in the same collection, and every piece of code that reads them has to handle both.

Atomicity across records has limits: when it exists, it comes with rules — per partition in Cassandra, up to 100 items and one region in DynamoDB, extra cost in MongoDB. In relational, BEGIN ... COMMIT covers whatever you want.

Horizontal scaling is an argument for very few: most business systems fit on one well-sized relational server with read replicas and connection pooling. If you don't have a scale problem Postgres can't solve, a distributed database is complexity with no payoff.

Frequently asked questions

Is MongoDB faster than PostgreSQL?

It depends on the access pattern; a generic benchmark doesn't answer it. Reading a whole document by key is fast in both; joins and reports favor relational. The detailed comparison is in when to use MongoDB.

Can I use PostgreSQL as a document database?

Yes, and it's often a good decision. JSONB stores the document, accepts a GIN index, and lives alongside typed columns and transactions in the same database. Just remember the default GIN serves @> and the key-exists operators, not ->> — for an equality filter on a field, use @> or an expression index.

When should I use NoSQL instead of SQL?

When there's a concrete problem it solves better: cache and sessions with TTL (Redis), massive distributed writes (Cassandra), graph traversal (Neo4j), known access patterns with managed scale (DynamoDB). "Flexibility" alone isn't a reason — JSONB covers most of that argument.

Can I mix SQL and NoSQL in the same system?

Yes, and it's common: PostgreSQL for orders, payments and inventory; Redis for sessions and cart. The cost is operational — two monitoring setups, two backups, two sets of expertise. Only add the second database when the gain is measurable.

SQL as the default, NoSQL when the problem shows up

My rule: start with PostgreSQL. If a case appears where a specialized database makes sense — Redis for caching, Elasticsearch for text search, Cassandra for time series at industrial volume — add that database for that case. It complements relational; it doesn't replace it.

The database decision is expensive to reverse. Migrating from MongoDB to PostgreSQL with production data is a months-long project; realizing you need Redis for caching is a sprint. Pick relational by default, run EXPLAIN on the queries that matter, and let NoSQL in when there's a number that justifies it. When modeling queries start getting long, I run them through a SQL formatter before sending them for review — nobody reviews an unindented nested CTE properly.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile