SQL vs NoSQL: Choosing a Database With a Reason
What relational and non-relational databases each guarantee, the four NoSQL families, how SQL scales, and a decision rule based on access patterns.
Every system design includes the question 'which database will you use?' The expected answer is not a product name. It is a choice followed by a reason, and the reason comes from the data and how it will be accessed.
'It is scalable' is not a reason: both families can scale, and no database is best in general. These notes compare the two families and give a rule for deciding between them.
SQL (relational) databases
A relational database stores data in tables with a fixed schema: every row in a table has the same columns, with defined types. Tables are linked by keys, and the database can combine them with joins. Examples are PostgreSQL and MySQL.
The defining feature is the transaction with ACID guarantees.
- Atomicity: all the statements in a transaction take effect, or none do. A money transfer never debits one account without crediting the other.
- Consistency: every transaction leaves the data in a valid state according to the declared constraints (unique keys, foreign keys, checks).
- Isolation: concurrent transactions do not interfere with each other; the result is as if they ran one after another.
- Durability: once a transaction is committed, it survives a crash.
When to choose SQL
Choose a relational database when correctness is more important than raw scale, when the data is naturally relational, and when you need flexible queries. Payments, orders, bank accounts, inventory and bookings belong here. A double-spent rupee or a seat sold twice is a business failure, and transactions with constraints are the simplest way to prevent it.
SQL is also the safe default when the query patterns are not yet known, because joins and secondary indexes let you ask new questions of the data without restructuring it.
NoSQL databases
NoSQL is a family of databases that give up some relational features, usually joins and multi-row transactions, in exchange for easier horizontal scaling and a flexible schema. There are four main types, and they are quite different from each other.
- Key-value stores (Redis, DynamoDB): a value is fetched by its key. Extremely fast and simple. Suitable for sessions, caches, counters and user preferences.
- Document stores (MongoDB): each record is a JSON-like document that can contain nested fields and arrays, and documents in one collection need not share a structure. Suitable for product catalogues, content and user profiles.
- Wide-column stores (Cassandra, HBase): rows are grouped by a partition key and sorted within the partition. Designed for very high write throughput and large datasets. Suitable for chat messages, activity logs, time-series and sensor data.
- Graph databases (Neo4j): store nodes and relationships, and traverse relationships efficiently. Suitable for social graphs, recommendations and fraud detection.
When to choose NoSQL
Choose NoSQL when the scale is very large, when the access pattern is simple and known in advance (most often 'get by key' or 'get a range within a partition'), when the workload is write-heavy, or when the structure of the data varies from record to record. Chat messages, event logs, sessions and feeds fit these conditions.
The price is that the data model must be designed around the queries. In a relational database you model the entities and query them however you like. In most NoSQL databases you first list the queries and then design tables whose keys answer them directly, often duplicating data to avoid joins.
RELATIONAL (normalised, joined at read time)
users (id PK, name, email UNIQUE)
orders (id PK, user_id FK -> users.id, total, status, created_at)
items (id PK, order_id FK -> orders.id, product_id, qty, price)
SELECT o.id, o.total, i.product_id, i.qty
FROM orders o JOIN items i ON i.order_id = o.id
WHERE o.user_id = 42;
DOCUMENT (denormalised, read in one lookup)
{
"_id": "order_981",
"userId": 42,
"status": "PAID",
"total": 1499,
"items": [
{ "productId": "p_12", "qty": 1, "price": 999 },
{ "productId": "p_40", "qty": 2, "price": 250 }
]
}
WIDE-COLUMN (query-first design)
messages_by_conversation
PRIMARY KEY ((conversation_id), sent_at) -- one partition = one chat, sorted by timeDoes SQL scale?
Yes. A relational database scales reads with read replicas and scales writes and storage with sharding, where the data is split across several database servers by a shard key. Many very large products run on sharded relational databases.
The difference is in the effort. In most NoSQL systems, partitioning is built in: you choose a partition key and the database distributes and rebalances the data. In a traditional relational setup, sharding needs more planning: choosing the shard key, routing queries, and accepting that joins and transactions across shards become difficult. Distributed SQL databases (sometimes called NewSQL) aim to provide both the relational model and automatic horizontal scaling.
Consistency models
Relational databases default to strong consistency: after a write commits, every read sees it. Many NoSQL databases default to eventual consistency: replicas converge over time, and a read immediately after a write may return the old value. This is often described as BASE (basically available, soft state, eventually consistent) in contrast to ACID.
The distinction is less rigid than it once was. Several NoSQL databases offer tunable consistency or even transactions, and relational replicas can lag. The question to ask of any database is what it guarantees for the specific operations you need.
Polyglot persistence
Large systems rarely use a single database. Each kind of data is stored in the system that suits it: orders and payments in a relational database, the product catalogue in a document store, sessions and carts in Redis, messages or events in a wide-column store, product search in a search engine, and images in object storage. This approach is called polyglot persistence. The trade-off is operational: more systems to run, monitor and keep in sync.
A decision rule
- List the main queries and how often each runs. This is the access pattern.
- If you need multi-row transactions, strong consistency or joins across entities, choose SQL.
- If you need massive write throughput or storage with simple key-based access, choose NoSQL.
- If you choose NoSQL, name the type and why: key-value, document, wide-column or graph.
- If the system has several kinds of data, use more than one database and say which data goes where.
Interview summary
- Start from the access pattern, not from a product name.
- Strong consistency and relationships: SQL with ACID transactions.
- Very large scale, flexible schema, simple access: NoSQL.
- Specify the NoSQL type: key-value, document or wide-column.
- Use both when the data calls for it: polyglot persistence.
Summary
SQL databases offer a fixed schema, joins and ACID transactions, and are the right choice where correctness and relationships matter. NoSQL databases offer flexible schemas and built-in horizontal scaling for simple, high-volume access patterns. The choice should always be justified by how the data is read and written, and large systems commonly use several databases side by side.
Puneet Tiwari
Full Stack Developer
