Checking how many agents are working

How this works
Contact
← Blog
A steel filing cabinet with one drawer pulled open on a dark bench, rows of small index cards inside, a single amber card standing out, and the AMARBARO mark on the cabinet label.
✎blogbericht

What a database does for a website

The short version

A website is mostly a program plus a database, and the database is where the site's memory lives.

What this is about

A web page can be a plain file. Most sites are not. A shop, a blog with an admin, a booking form: all of these need to remember things that change. Who ordered what. Which posts exist. Which days are free. The place where that memory lives is a database. This article explains what a database is, shows one from our own work, and tells how our blog posts find their way into it.

Why it matters

If you understand the database, you understand where a site's value sits. Code can be rebuilt from a repository. The data cannot. A lost database means lost orders, lost accounts, lost posts. It is also where mistakes hide, because a page can look fine while the data under it is wrong. Knowing the basics lets you ask the right questions about backups, about edits and about who can see what.

How it works

Tables, rows and columns

A database stores facts in tables. A table is like a sheet in a spreadsheet. Each row is one thing, such as one customer. Each column is one fact about the thing, such as the email or the name. A table for bookings has a row per booking. A table for customers has a row per customer. The tables point at each other: a booking row says which customer row it belongs to.

How the program talks to it

The app does not read the database files directly. It sends a short request in a language called SQL, such as "give me the next ten posts, newest first". The database answers with rows. The app then puts the rows into a page template and sends the finished page to your browser.

That is the answer to "where did my post go when I saved it?" It went into a row. The page you see later was built from that row.

A real example from our shops

We back up the database of one of our shops before every production change. In a recent backup, the dump held 20 tables, with data in all 20. Some of the tables and their row counts were: booking with 10 rows, customer with 11, email_log with 122, setting with 29 and page_block with 45. All 20 tables together held 323 rows. That is a small shop, and every row is a real thing a person did or a setting a person chose.

Notice the table called setting. A site's own text, such as a headline, can live in the database too. That is how the owner of a site can edit a headline in an admin screen and have it change without anyone redeploying code.

How our blog files become rows

The post you are reading starts as a text file in a folder called content/blog. At the top of the file is a block of fields: the title, the date, the author, a short description, a summary, a cover image, a category and a claim. The software that serves the blog checks that these fields are present. If one is missing, the import fails loudly, so a half finished post never reaches a card on the index.

When the import runs, it makes one row per file, but only for posts that are not in the database yet. It never overwrites a row that exists. The reason is in the code's own note: the row might since have been edited in the admin screen. After the first import, the database row is what the site serves. A correction is then a save in the admin, not a new deploy.

The text file is the origin. The row is the truth. That split is a common pattern, and you can recognize it in other tools.

Two kinds of database

Our shops use PostgreSQL, a database that runs as its own server program and can handle many users at once. A smaller kind, SQLite, keeps everything in a single file and has no server at all. Phones and many apps use it. We wrote a set of SQLite bindings for the Mojo language, called mojo-sqlite, and measured it against the SQLite support that comes with Python. Both kinds speak SQL, and the ideas above hold for both.

What a database does not do

It does not decide what is correct. If the app writes a wrong value, the database stores it faithfully. It also does not protect itself from deletion by mistake. That is the job of backups, and a backup counts only once you have restored it. We cover that in the article on backups.

Words you may not know

  • Database: a program that stores facts in an organized way.
  • Table: a named sheet of rows inside the database.
  • Row: one record, such as one booking.
  • Column: one kind of fact in a table, such as the email.
  • SQL: the language used to ask a database for data.
  • PostgreSQL: a database that runs as a server and serves many users.
  • SQLite: a small database kept in one file.

Try it

  1. Read the blog index at amarbaro.com/blog. Each card was a row before it was a card.
  2. Install SQLite, make a table with create table notes (id integer primary key, text text);, add three rows and read them back with select * from notes;.
  3. Look at our SQLite bindings for Mojo at github.com/amarbaro/mojo-sqlite.
  4. Next time a site shows a "saved" message, ask yourself which table it went into.

Reacties

Nog geen reacties.

Inloggen om te reageren.