Part I · Chapter 1 · The Diagram · lesson 3
Why it needs a database and not just a file
14 min
Part I · Chapter 1 · The Diagram · lesson 3
14 min
Your first program that had to remember something probably wrote to a file. Open the file, read what is in it, change it, write it back. It works, it is obvious, and you can look at the result in a text editor.
So the honest question, before accepting the box on the whiteboard: what exactly goes wrong with the file?
The answer is not "files are for beginners". It is one specific thing, it happens the moment two people use your program at the same time, and once you have seen it you will recognise it for the rest of your career.
Predict
It keeps concurrent writes from destroying each other. Two requests changing the same row do not silently overwrite one another. The database serialises the dangerous part, and the guarantee has a name — a transaction — which is a group of changes that either all happen or none of them do.
It answers questions without reading everything. "All orders from this customer last month" over a large file means reading the whole file. A database keeps indexes, which let it go more or less straight to the rows.
It survives being switched off at the wrong moment. A process killed halfway through writing a file leaves half a file. A database is built so that an interrupted change is either completed or discarded on restart, and never half-applied. This is the property people are relying on when they say their data is "safe", and it is a property, not a feeling.
All three are about the same underlying problem from different angles: more than one thing is happening at once, and some of those things are allowed to fail. A file assumes a tidy world with one writer who never gets interrupted. A database assumes the world you actually have.
A database holds tables. A table has columns — the fields, fixed for
every entry — and rows, one per thing. A users table has a column for
the email and a row per person.
You ask it questions in a language called SQL, and the shape of the question is always the same: which columns, from which table, matching what.
SELECT email, created_at
FROM users
WHERE created_at > '2026-01-01';
Which columns, from which table, matching what. That is most of SQL, and the rest of it is variations on the same three slots. You do not need to be fluent to read a system; you need to recognise that a query is a question with those parts, because the next few chapters are about why a particular question is slow.
The word for the thing that makes that question fast is an index: a separate structure the database maintains so it can find matching rows without reading every one. The cost is that it has to be kept up to date on every write. That trade — faster reads paid for with slower writes and more storage — is the whole of chapter four.
Check
Reach for a database as soon as two things can write. Not because files are bad, but because the day you add a second user is the day a file stops being correct, and that day arrives without an announcement.
Start with the boring one. For almost anything that is not already decided for you, a single Postgres or MySQL instance is the right first answer, and it stays the right answer for far longer than people expect. Chapter ten compares products properly, on a criterion written down first.
Do not design the tables around the screens. The screens change every few months and the data outlives them. Write down the things that exist — users, orders, messages — and what each one needs to be true about it.
Learn to read a query before learning to write one. Most of what you will do early in a career is work out why somebody else's query is behaving strangely, and that reading is a skill you can start building today.
The condition under which this whole lesson inverts: when only one process ever writes, and losing the most recent write is acceptable.
That is not a rare situation. A build artefact, a generated report, a cache on disk, a log that a single process appends to, configuration that a human edits and a program only reads — all of these are files, correctly, and putting them in a database buys nothing and costs a dependency.
The test is the one from the predict at the top. Ask: can two things write this at the same time? If the honest answer is no, and will stay no, the file is not a shortcut. It is the simpler correct answer, and simpler correct answers are the ones this subject is trying to teach you to keep.