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
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:
Three kinds of storage
The server is replaced. What's still there?
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