Blogprojectstips

What is a flat-file database and why would you use one?

Not everything needs a database server

When most people hear "database" they picture a separate server running in the background, something you have to install, configure, and connect to. That's one way to do it — but it's not the only way.

What is a flat-file database?

A flat-file database is just a plain file on disk — typically JSON, CSV, or XML — that your code reads and writes directly. There's no database engine, no connection string, no server process. The file is the database.

This site actually uses flat-file storage. Blog posts and projects live in .json files. When a page loads, PHP opens the file, reads the data, and renders it. When something is saved, PHP writes the file back. That's the whole system.

What is a "real" database?

A relational database like MySQL or PostgreSQL is a dedicated server process that sits between your code and your data. You send it a query — a structured question written in SQL — and it finds and returns the matching records. It handles multiple users simultaneously, enforces relationships between tables, and can search millions of rows in milliseconds using indexes.

The tradeoff is complexity. You need to install it, configure it, manage users, and write queries instead of just reading a file.

The trade-offs

Flat-file pros:

  • Zero setup — it's just a file
  • Trivially easy to back up or move
  • Human-readable in any text editor
  • No dependencies or running processes

Flat-file cons:

  • Doesn't scale to thousands of records
  • No built-in querying — your code does all the filtering
  • No concurrent write protection — two simultaneous saves can corrupt the file

Database pros:

  • Handles large datasets efficiently with indexing
  • SQL gives you powerful filtering, sorting, and joining
  • Built-in concurrency control

Database cons:

  • Requires installation and ongoing maintenance
  • More moving parts to break or misconfigure
  • Overkill for small, low-traffic projects

When to switch to MySQL

If your flat file starts feeling slow, you're doing complex filtering in code, or multiple users are writing data at the same time — that's your signal. For a personal site or small tool with light traffic, a flat file will serve you just fine for years.

All posts