Skip to content
EL
Contents

What data is, and why we need a database

Where a program's data lives

ConceptFoundation4 min read5 min hands-on#fundamentals#storage

The idea in 30 seconds

A running program keeps its data in memory, which is fast but forgets everything when the program stops. Writing the data to a file keeps it, but the moment two writers or a crash enter the picture, a plain file stops being trustworthy. Databases exist to close that gap.

On this page

State: what your program remembers

Every useful application remembers things: the users who signed up, what is in each cart, how much money each account holds. That remembered information is called state.

While the program runs, its state lives in memory (RAM): variables, lists and dictionaries. Memory is fast, but it is volatile. When the process exits, crashes, or the machine loses power, everything in it is gone.

Try it. Save this as counter.py, run it a few times, then stop it and run it again:

visits = {"home": 0}

while True:
    input("Press Enter to visit the home page ")
    visits["home"] += 1
    print("Visits so far:", visits["home"])

Every restart begins at zero. Nothing the program learned survived.

Writing it down

The obvious fix is to write the state to disk: to a file that is still there after a restart. Load the file when the program starts, save it after each change, and the count survives.

That works, and for one program on one machine making one change at a time it is often enough. Trouble starts with questions a toy program never asks:

  • What if two copies of the program save at the same moment?
  • What if the program crashes halfway through saving?
  • How do you find one record quickly when the file holds millions?
  • What stops someone saving an age of "banana" or two users with the same email?

PredictTwo writers, one file

Two copies of a program each add 1,000 users to the same users.json. Each addition reads the whole file, appends one user, and writes the whole file back. Both copies run at the same time. How many users are in the file when they finish?

Kinds of data

Not all data has the same shape, and the shape affects where it is easiest to keep.

KindWhat it looks likeExample
StructuredEvery record has the same fieldsA table of users with id, name, email
Semi-structuredRecords have fields, but they varyJSON events where each type carries different properties
UnstructuredNo fields at all, just contentImages, PDFs, audio, free text

Most application state is structured or semi-structured, and that is what this whole field is about. Unstructured content is often kept as files in object storage, with a database holding the facts about each file.

What every application does with data

Whatever the data, applications do four things with it: create new records, read them, update them and delete them. These are often shortened to CRUD. A storage system is only as good as how safely and quickly it does those four things when many requests do them at once, and when a crash interrupts them halfway.

Check yourself

  1. Why is memory alone not enough to hold an application’s state?

    Show answer

    Memory is volatile: its contents disappear when the process exits, crashes or the machine loses power. State that must outlive the process has to be written to durable storage.

  2. In the two-writer experiment, why did a program crash instead of just losing an update?

    Show answer

    Opening a file for writing truncates it to zero bytes before the new contents are written. The other program read the file in that window, found empty or partial JSON, and failed to parse it.

  3. Give an example of structured, semi-structured and unstructured data from an app you use.

    Show answer

    For example: a bank statement’s transactions (structured), app analytics events with varying properties (semi-structured), and profile photos (unstructured).

Finished reading?

Saved in this browser only.

Sources

Where each claim on this page comes from.

  1. 1.
    open(2) manual page, macOS
    Primary· O_TRUNC· man 2 open

    Opening an existing file with truncation empties it to size 0 before anything new is written.

Go further

What to read next, and why it is worth the time.

  1. 1.
    Designing Data-Intensive Applications
    ReferenceMartin Kleppmann· Chapter 1: Reliable, Scalable, and Maintainable Applications· location not yet verified

    Why data, not computation, is usually the hard part of an application, and what "reliable" has to mean.

Applies to PostgreSQL 18. Draft: not yet fact-checked end to end.