What survives a restart

Reading · 5 min · Module 3, lesson 1 of 315 min left in this module

Module 3 · StorageLesson 1 of 3

Goal: Predict which of your app's data survives a restart or a replaced server, and move what must survive out of harm's way.

3:26 · captions and chapters · narrated with an AI-generated voice
Transcript

Narration uses an AI-generated voice.

[00:00] Where we're going

By the end of this video, you'll be able to predict which of your app's data survives a restart, and which doesn't. And you'll know where to put the things you need to keep.

[00:10] Restarted

Picture your app, running on a server. It keeps some things in memory, like who's logged in, or a counter. And it writes some files to the server's own disk. Two different things can happen to it. And they lose different amounts.

First, a restart. The program stops, and starts again on the same server. Everything it held in memory is gone. Sessions, counters, anything in a variable. But the files it wrote to disk are still there.

[00:41] Replaced

Second, a replacement. The app comes back on a fresh server, or in a fresh container built from its image. Now the files it wrote are gone too. They were on the old server. On your laptop, that almost never happens. In the cloud, it's routine. Every deploy, many crashes, every scale-up, and every hardware repair can put your app on a fresh server.

[01:07] Ephemeral and persistent

Here's the whole idea, in three boxes. The server is replaced. What's still there? The server's local disk? Lost. It was tied to that one server. A mounted volume? Kept. It's reattached to the new server. And object storage? Kept. It lives outside any server. Storage that disappears with its server is called ephemeral. Storage that outlives any one server is persistent.

So is a server's own disk useless? Not at all. It's fine for scratch work. A file you create, use and delete within one request. Or a cache you can rebuild. It's the wrong place for anything a user would miss.

[01:52] A second trap

There's a second trap, once you run more than one copy of your app. Each copy has its own local disk. Say a photo is uploaded through copy A. It's saved on copy A's disk. Now a request for that photo reaches copy B. And copy B can't show it. It isn't on its disk.

[02:13] Where the keepers go

So where do the things you need to keep belong? Records, like users and orders, go in a database. It runs separately from your app. Files, like uploads, go in persistent storage. A mounted volume, or object storage. And configuration goes in environment variables, not in files your app edits while it runs.

On ComputeSphere, each spherelet starts with a fresh filesystem from your image. What it writes there is lost on every redeploy and restart. A SphereStor volume lives in the environment instead. It reattaches when the spherelet using it is replaced.

[02:55] Recap

So, a quick recap. Memory goes when your app restarts. A server's own disk goes when the server is replaced. So anything you need to keep lives somewhere outside the server.

Now try it with an app of your own. List what it writes to disk, and ask: would a user miss it? Next, you'll learn to pick the right kind of storage for what you keep. I'll see you in the next lesson.

Key idea

Memory is lost when your app restarts, and a server's own disk is lost when the server is replaced. In the cloud, servers are replaced all the time, so anything you need to keep must live somewhere outside the server.

Storage at a glance

Ephemeral disk
Gone when its server is replaced.
Block storage
A raw disk for one server.
File storage
A folder many servers share.
Object storage
Whole files by key, over HTTP.
The four ideas in this module.

Restarted or replaced

Two different things can happen to a running app, and they lose different amounts.

  • Restarted: the program stops and starts again on the same server. Everything it held in memory is gone: logged-in sessions, counters, anything in a variable. Files it wrote to disk are still there.
  • Replaced: the app comes back on a fresh server, or in a fresh container built from its image. Now the files it wrote are gone too, because they were on the old one.

On your laptop, replacement almost never happens, so saving files next to your code seems fine. In the cloud it's routine: every deploy, many crashes, every scale-up and every hardware repair can put your app on a fresh server.

Ephemeral and persistent

Storage that disappears with its server is ephemeral. Storage that outlives any one server is persistent. The strip at the bottom of this diagram is the whole lesson in three boxes:

A server's own disk is fine for scratch work: a file you create, use and delete within one request, or a cache you can rebuild. It's the wrong place for anything a user would miss.

There's a second trap once you run more than one copy of your app. Each copy has its own local disk, so a file saved by one copy is invisible to the others. A photo uploaded through copy A can't be shown by copy B.

Where the keepers go

  • Records such as users and orders go in a database, which runs separately from your app (Module 9).
  • Files such as uploads go in persistent storage: a mounted volume, or object storage. The next lesson compares the kinds.
  • Configuration goes in environment variables, not in files your app edits at runtime.

The top of the diagram previews those kinds of storage. Next, you'll learn to pick one.

Check yourself

Your app saves uploaded avatars to an uploads folder next to its code. After the next deploy, they're gone. Why?
Which of these is safe to keep on a server's local disk?

In the docs