Relational Database vs NoSQL: Data Model, Consistency, Scaling
A relational database (RDB) represents data as tables of rows and columns and is queried with SQL. It supports complex queries that join tables, and keeps data strictly consistent through the ACID properties of transactions and constraints such as primary and foreign keys. NoSQL is an umbrella term for databases other than relational ones, including key-value, document, wide-column and graph databases. Most of them make the data format flexible and make it easier to scale out across many servers, at the cost of complex queries such as joins and some degree of strict consistency. Rather than one being better, the choice depends on the shape of the data and the properties you need.
| Aspect | Relational database (RDB) | NoSQL |
|---|---|---|
| Data representation | Tables (relations) of rows and columns, linked to each other by shared values (foreign keys) | Depends on the type: key-value pairs, JSON-like documents, groups of columns, graphs of vertices and edges and so on |
| Schema | Column names and types are defined in advance; changing them means altering the table definition, for example with ALTER TABLE | Many products do not require a strict schema in advance (schemaless), so records with different fields can be stored together |
| Queries | Searching by conditions, aggregation, sorting and so on can be written declaratively in SQL, a standard language | Usually product-specific APIs or query languages; some products offer SQL-like languages |
| Joins | JOIN allows queries that combine several tables | Many handle joins poorly; data that is read together is often stored together in one document or value |
| Transactions and consistency | Transactions with the ACID properties can commit updates to several rows and tables together | Many products limit updates to a single key or document, or allow eventual consistency, where replicas do not agree immediately; some products do offer ACID transactions |
| How to scale | Mainly scaling up by making one server more powerful; reads can be spread across replicas, but spreading writes over many servers is hard | Easy to scale out by partitioning data by key across many servers |
| Normalization | Tables are normalized to remove duplicated data and prevent update anomalies | The same data is often duplicated to favor fast reads |
| Suitable uses | Business systems where integrity matters and data relationships are complex, such as accounting, inventory and ordering | Sessions and caches for very high-traffic web services, data without a fixed shape, large volumes of logs, connections between people or things (graphs) |
Figures are typical orders of magnitude and vary by process generation and product.