System Design · Concept Library · Free article
What Is a Database, and How Do You Choose One?
Start with the guarantees your data needs, then choose a store from the reads and writes your product actually performs.
A database is storage with enforceable guarantees. It coordinates concurrent work, makes committed data durable, finds records without scanning everything, and rejects data that breaks declared rules. Start with a relational database when the facts relate to one another and correctness spans several rows. Choose a specialized store only when a measured access pattern needs a different shape. For Hatch, Postgres fits because one short code must identify one durable link, and the redirect must retrieve it quickly.
What is a database, and why not just a file?
A database is storage that enforces guarantees a file cannot: it settles concurrent writers, survives a crash without losing committed work, and finds a record without reading everything. Before launch, Hatch can keep links in memory or append them to a file. That works while one process owns every read and write. The first restart loses memory. Two concurrent writers can race over a file. Finding one code among millions becomes a scan. Nothing prevents two rows from claiming the same public code.
A database packages answers to those problems. It coordinates concurrent transactions, records changes durably, maintains indexes for lookup, and enforces constraints at the point where data is written.
The interactive version of this example is available when JavaScript loads.
The four jobs Hatch needs
- Durability: once Hatch confirms a link, a process restart must not erase it.
- Concurrency: two creates must not overwrite each other or expose half-written state.
- Integrity: a uniqueness constraint must reject a second row with the same short code.
- Querying: the redirect must locate one code without reading the entire table.
Pages, logs, and indexes do different work
A relational database stores table data in pages. Frequently used pages stay in a memory buffer so every read does not have to reach storage. Before PostgreSQL writes a changed data page, it records enough information in its write-ahead log to recover committed changes after a crash. An index is a separate structure that narrows where the engine must look.
Those mechanisms support different promises. The write-ahead log supports crash recovery. Constraints protect declared rules. Transactions define which changes commit together. Indexes accelerate particular queries and add work to writes.
What does a transaction guarantee?
A transaction groups changes that must commit together. ACID is a compact description of that contract: atomicity makes the unit all-or-nothing; consistency preserves declared invariants; isolation controls what concurrent transactions can observe; durability keeps a committed result after a crash.
Isolation is not one universal setting. PostgreSQL Read Committed gives each statement a snapshot of committed rows. Repeatable Read keeps a stable transaction snapshot but can still reject a conflicting write. Serializable also rejects executions that cannot match some one-at-a-time order. Applications using either stronger level must be ready to retry the whole transaction after a serialization failure.
How do you choose a database?
“SQL or NoSQL?” is too broad to answer from traffic alone. Write down the access patterns, invariants, retention, and failure tolerance first. A relational database is a strong default when facts relate to one another, transactions cross rows, and the team needs flexible queries. A key-value, document, wide-column, graph, search, or analytical store earns its place when its data model and operating trade-offs fit a specific workload better.
| Dominant requirement | Useful starting point | Reason |
|---|---|---|
| Related records and multi-row invariants | Relational database | Transactions, constraints, joins, and flexible queries |
| Simple lookup by a stable key | Key-value store | A narrow read/write contract with low lookup overhead |
| Self-contained records whose fields vary | Document store or relational JSON | Read the aggregate together while allowing controlled shape changes |
| Search by terms and relevance | Search index | An inverted index is built for token lookup and ranking |
| Large analytical scans | Columnar warehouse | Separates reporting work from the transactional path |
Why Hatch starts with Postgres
Hatch has a small, connected model: a creator owns links; each link has one public code and one destination; later, clicks refer back to that link. Creating a link needs a uniqueness rule. Resolving it needs a lookup by code. A relational table expresses both without adding another system.
The write path generates a candidate code and inserts the row. The database accepts exactly one row for that code. In PostgreSQL, the unique constraint creates a unique index that can also serve the read path’s lookup by code; a second index on the same column is unnecessary. The short-code pattern explains how identifiers are generated. Database indexing explains why the lookup stays fast.
| Path | Operation | Database mechanism |
|---|---|---|
| Create | INSERT links(code, long_url, creator_id) | Transaction plus unique constraint on code |
| Resolve | SELECT long_url FROM links WHERE code = ? | Index on code |
One database is a beginning, not a promise forever
A larger machine, connection limits, query tuning, and indexes can carry a relational database a long way. Read replicas can spread reads and improve recovery options, but replicas introduce lag. Sharding spreads data and writes, but makes cross-shard transactions and joins harder. Caches remove repeated reads from the database, but copies can become stale.
Add those pieces only after a measured bottleneck appears. Hatch’s next chapter does exactly that: the indexed lookup remains correct, then growing read volume overwhelms the one database box.
Primary references
- PostgreSQL: transaction isolation
- PostgreSQL: write-ahead logging
- PostgreSQL: indexes
- PostgreSQL: constraints
Choose the guarantees, then the store
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.