GOAT EngineerPreparing your learning space…

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.

A requirement points to a store shape; the product name comes later.
Dominant requirementUseful starting pointReason
Related records and multi-row invariantsRelational databaseTransactions, constraints, joins, and flexible queries
Simple lookup by a stable keyKey-value storeA narrow read/write contract with low lookup overhead
Self-contained records whose fields varyDocument store or relational JSONRead the aggregate together while allowing controlled shape changes
Search by terms and relevanceSearch indexAn inverted index is built for token lookup and ranking
Large analytical scansColumnar warehouseSeparates 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.

The first Hatch data model is small because it follows the two product paths.
PathOperationDatabase mechanism
CreateINSERT links(code, long_url, creator_id)Transaction plus unique constraint on code
ResolveSELECT 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

Choose the guarantees, then the store

By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.