DataUpdated 2026-11-20

Databases

Choosing a database from access patterns instead of product names: SQL versus NoSQL, indexes, and the scaling path before sharding.

The master ledger in the back room. Every sale and customer is written here.

That is the primary database. It is the one place the business trusts as the record. Everything else, such as caches and copies, is built from it or in front of it.

How it works

Start from access patterns, not product names. The question is not whether to use MongoDB or PostgreSQL. It is how the application will read and write its data.

Write down:

  • read patterns and write patterns
  • relationships between entities
  • query frequency
  • data size and growth
  • consistency and transaction needs

Then choose.

SQL or NoSQL

SQL databases such as PostgreSQL fit structured data, relationships, transactions, strong consistency and complex queries with joins. Money, inventory and anything that touches several tables in one transaction belong here.

NoSQL stores such as document or key-value databases fit flexible shapes, very high write volume, or scale by partitioning on one key with no joins. NoSQL is not simply better than SQL. Payments, ledgers and reservations need multi-record consistency, and that is where SQL is the safer default.

Code
User → Orders → Order items → Products
         └── Payments

The scaling path

Code
Single database
   ↓ indexes
   ↓ caching
   ↓ read replicas
   ↓ partitioning
   ↓ sharding

Do not jump to the end. Find the real bottleneck first.

Indexes

An index lets the database find rows without scanning the table. It speeds reads on columns used in WHERE, JOIN and ORDER BY. It costs storage and slows writes, since each write updates the index too.

Code
CREATE INDEX users_email_idx ON users (email);

Index from real query patterns. Indexing every column, or indexing "just in case", slows writes for nothing.

A checklist for a new project

  • List every entity and its relationships.
  • Mark each entity as read-heavy or write-heavy.
  • Ask whether any operation spans several entities in one transaction, such as an order, a payment and an inventory change. If so, favour SQL and its transaction guarantees.
  • Ask whether the schema will change often early on. Flexible stores can help, but disciplined migrations on SQL often do the same job.
  • Estimate data volume in one year and in three.
  • Decide whether large files should live outside the database, in object storage.

Answering these before choosing a product usually makes the choice obvious.

Where I used it

IMS is a modular monolith on one Postgres database. I chose that so stock, reservations and documents commit atomically. The cost is discipline: module boundaries need care, and database contention has to be measured instead of assumed away. Quantities and money are stored as decimal strings with rounding in one place, which keeps physical quantity and monetary precision.

Trade-offs

  • One database is a single point of failure. It needs backups, and ideally failover. Write that risk down instead of hiding it.
  • Contention shows up late. A single database is simple until many writers fight over the same rows. Measure it before reaching for sharding.
  • Indexes are not free. Each one costs write speed and storage.
  • Schema flexibility cuts both ways. A flexible schema ships fast early and makes consistency your problem later. A strict schema slows the first week and protects the next year.
  • Migrations need care. A change that is trivial on a small table can lock a large one.

A useful rule: pick the database for the access pattern and the transaction needs, keep it as simple as those needs allow, and move down the scaling path only when a measurement says to.