← Level 4 – Data pro

Lesson 1 of 6 ⏱ 20 minutes Free preview

Why data disappears on restart

Find out why the guestbook forgets messages when the server stops, and what we'll do about it.

What’s happening under the hood

In Level 3, we stored messages in an array in memory:

const messages = [
  { name: "Martin", text: "First message!" },
];

That works great, as long as the server keeps running. But when you stop it (Ctrl+C) and start it again, the array is empty. The messages are gone. Why?

Because RAM (the computer’s memory) is like a whiteboard in a classroom: when you leave, someone wipes it clean. For permanent storage, you need a disk: a file or a database.

💡

Disk vs. memory: a file on disk survives a restart. An array in server.js doesn’t. That’s why big websites use databases: MySQL, PostgreSQL, SQLite. We’ll start with SQLite: a database in a single file, no extra server needed.

Show me: what happens after a restart

  1. Start the server from the guestbook lesson (node server.js).
  2. Send a new message through React.
  3. Stop the server (Ctrl+C) and start it again.
  4. Refresh the page in your browser: the message is gone.

This isn’t a bug in the code. It’s a property of memory. And now you know why databases exist.

Your turn 💪

  1. Repeat the experiment above and write down (maybe in notes in your project) exactly what happened.
  2. Open server.js and find the line with const messages = [...]. Add a comment: // WARNING: this disappears after a server restart!
  3. Think about your own project: what shouldn’t disappear on your school/club/journal website? (schedule? photos? messages?): list 2–3 things.
✅

Check: You understand the difference between memory (temporary) and disk (permanent). In the next lesson you’ll learn Git to back up code on disk and online. Then SQLite to store messages forever.

What to take away from this lesson