Elegir una Base de Datos
SQL vs NoSQL
Escrito por Darjus Kazakevic · revisado el
Start from the queries you must answer, then pick the store.
Every database is a set of trade-offs. A relational database like Postgres gives you joins, transactions and queries you did not plan for. A key-value store like DynamoDB gives you single-digit-millisecond reads at any size, but only for the queries you designed its keys around. Document, wide-column, graph and time-series stores each serve one more access pattern well.
The common mistake is to choose by trend, such as "NoSQL scales", instead of by access pattern. Write down your top queries, how fresh each answer must be, and how big the data will grow. The right store is then usually clear, and for most new systems it is a relational one.
En esencia — Each database family makes some queries fast and others slow. Match the store to your top queries, how fresh they must be and how big the data grows.
Cuándo recurrir a él
Recurre a él cuando
- Starting a new service and choosing its system of record
- One query is slow or costly and the current store cannot fix it
- Data will grow past what one relational node can store or write
Reconsidéralo cuando
- A new store for a query that an index or a cache would fix
- No one on the team can run, back up and upgrade a second database
- Choosing by trend, before you know the top queries
Conceptos clave
- Relational (SQL · Postgres · MySQL)
- Tables with a fixed schema, joined at query time, with ACID transactions (a group of changes that succeeds or fails as one). One well-sized Postgres node serves tens of thousands of simple queries per second.
- Key-value and document (DynamoDB · Redis · MongoDB)
- Store a value, or a JSON document, under a key and fetch it by that key. Reads by key stay fast at any size. A query you did not plan for needs a new index or a full scan.
- Wide-column (Cassandra · ScyllaDB · Bigtable)
- Rows grouped by a partition key and sorted by a clustering key, spread over many nodes. Built for very high write rates, such as millions of events per second, read back one partition at a time.
- Graph (Neo4j · Amazon Neptune)
- Nodes and edges stored as first-class data, so a query can follow relationships many steps deep. Use it when the question is about connections, like "friends of friends who bought this".
- Time-series (TimescaleDB · InfluxDB · Prometheus)
- Data that is mostly appended and always read by time range. These stores compress it, roll old points up into averages, and delete it after a set retention period.
Reglas de diseño
- List the top queries before choosing
- Write down the five queries that matter most, with their rate and latency target. A store that serves all five by key or index is a candidate. A store that needs a full scan for one of them is not.
- Default to relational
- Postgres or MySQL handles most products up to terabytes of data. It also answers queries nobody planned for. Leave it only for a reason you can name, such as a write rate one node cannot take.
- Decide how fresh each read must be
- A balance or stock count needs strong consistency. A like count can be seconds old. The strictest read rules out stores that only offer eventual consistency (copies agree only after a delay).
- Keep one system of record
- A search index, cache or analytics store can sit beside the main database. Feed them from it, so each one can be dropped and rebuilt. Two stores that both claim to own the same data will disagree.
Errores habituales
- Picking a NoSQL store "for scale" at launch, then rebuilding joins by hand in application code.
- Designing DynamoDB tables like SQL tables, then finding that the main query needs a full table Scan.
- Putting relational data in a document store, so one change must update many copied documents.
- Two systems of record for the same entity, with no rule for which one wins when they disagree.