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.
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:
Three kinds of storage
Block
You addressBlocks on a disk/dev/sdb
Used forA database's data files
File
You addressPaths in a shared folder/shared/q3.pdf
Used forFiles several servers share
Object
You addressObjects by key, over HTTPGET /uploads/cat.jpg
Used forUploads, backups, assets
The server is replaced. What's still there?
Server's local diskLostTied to that one server
Mounted volumeKeptReattached to the new server
Object storageKeptLives outside any server
Anything you need to keep belongs on a volume or in object storage. A server's own disk is scratch space: it goes when the server goes.
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.