Episode Notes: What Happens When You Click Save

These notes are written in a relaxed, chatty style, like the notes for a podcast episode. There is no audio and no guests. We just follow one click of a Save button all the way to the database and back.

So, I click Save. What happens first?

Your browser collects what you typed in the form, say a new book title and its author, and sends it to the server as an HTTP request. At this point the database has no idea anything is happening. The data is just travelling across the internet.

And the server?

The server is a program, written in Python, JavaScript, Java or another language, waiting for requests. It receives yours and runs the piece of code meant for "save a book". That code usually checks the data first: is the title empty? Is the author too long? If something is wrong, it sends back an error message and the database is never touched.

Okay, the data looks fine. Now what?

Now the server needs to talk to the database. It uses a driver, a library that knows how to speak to that particular database. If the app is well set up, there is already an open connection waiting in a connection pool, so it does not have to log in again from scratch.

And then the SQL?

Right. The code sends something like this, with placeholders for the values:

conn.execute(
    "INSERT INTO books (title, author) VALUES (?, ?)",
    (title, author),
)
conn.commit()

The SQL and the values travel separately, which is what keeps the app safe from SQL injection.

What does the database do with it?

It checks the rules on the table. Is title allowed to be empty? Does the author have to exist in another table? If any rule is broken, it refuses and sends back an error. If everything is fine, it writes the new row and gives it an id.

What is that commit line about?

The commit says "I am done, make it permanent". Until then, the change is not final. If the server crashed halfway, the database would throw away the unfinished change. That is part of what makes databases trustworthy.

And then it comes back?

Yes, the trip in reverse. The database tells the driver "one row inserted". The server code decides what to send back, maybe a "Saved!" message or a redirect to the book list page. The browser shows it to you. All of that typically happens faster than you can blink.

The whole journey at a glance

StepWhereWhat happens
1BrowserForm data sent to the server
2Server codeData checked
3DriverSQL and values sent over a connection
4DatabaseRules checked, row written
5DatabaseCommit makes it permanent
6Server and browserSuccess message shown

One-line takeaway

A Save button is a little relay race: browser, server, driver, database, and back again. When something goes wrong, asking "which runner dropped the baton?" is the fastest way to find the bug. For more on the driver's part, try our essay on database drivers.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.

Related pages on this site