Sub topic 11
Restoring from snapshots and initializing volumes
This chapter assumes you can create snapshots, attach and mount volumes on Linux, and use the AWS CLI. It covers restoring a volume from a snapshot, why the restored volume can be slow at first, and how to avoid that.
How a restore works
A snapshot is stored in Amazon S3. When you create a volume from a snapshot, EBS makes the volume available immediately. The blocks are not copied up front. EBS loads each block from S3 the first time the instance reads it.
Reads of blocks that have not been loaded yet have higher latency than normal. This is the "first-touch penalty." Writes and later reads of the same block run at normal speed.
Restoring a volume
You can restore into any Availability Zone in the Region. The new volume must be at least as large as the snapshot.
aws ec2 create-volume \
--snapshot-id snap-0123456789abcdef0 \
--availability-zone us-east-1b \
--volume-type gp3 \
--size 200An encrypted snapshot always produces an encrypted volume. After you attach the volume, do not run mkfs. The file system already exists, and formatting would erase it. Mount it directly.
sudo mkdir -p /restore
sudo mount /dev/nvme1n1 /restoreIf you attach the restored volume to the same instance as the original, an XFS mount can fail because both file systems share a UUID. Mount with -o nouuid, or give the copy a new UUID with xfs_admin -U generate on the unmounted device.
Initializing a volume
Initialization means reading every block once so that all of them are loaded. Run it before production use if first-touch latency would hurt. Read the whole device with fio:
sudo fio --filename=/dev/nvme1n1 --rw=read --bs=1M \
--iodepth=32 --ioengine=libaio --direct=1 \
--name=volume-initializeInitialization takes time proportional to volume size, and it consumes IOPS and throughput. Run it before the volume goes into service.
Fast snapshot restore
Fast snapshot restore (FSR) removes the first-touch penalty. You enable it per snapshot and per Availability Zone. Volumes created from that snapshot in that zone get full performance immediately.
aws ec2 enable-fast-snapshot-restores \
--availability-zones us-east-1b \
--source-snapshot-ids snap-0123456789abcdef0
aws ec2 describe-fast-snapshot-restores \
--filters Name=snapshot-id,Values=snap-0123456789abcdef0Wait until the state is enabled before you rely on it. FSR is billed for each hour it is enabled, for each snapshot and each zone. Disable it when you no longer need it.
| Approach | Cost | Best for |
|---|---|---|
| Do nothing | None | Test volumes, non-critical data |
| Initialize with `fio` | Time and I/O | One-off restores before go-live |
| Fast snapshot restore | Hourly fee per snapshot per AZ | Frequent restores that must be fast |
Common errors
- Running
mkfson a restored volume, which destroys the data. - Mounting the restored volume on the original instance without handling the duplicate XFS UUID.
- Enabling FSR in one zone and then restoring in another, so no benefit is gained.
- Leaving FSR enabled on old snapshots and paying for it.
Exercise
You restore a database volume from a snapshot in us-east-1b. Queries run slowly for the first hour, then speed up. What causes this, and name two ways to avoid it.
Show answer
EBS loads blocks from S3 lazily, so the first read of each block is slow. Avoid it by initializing the volume (reading all blocks with fio before use), or by enabling fast snapshot restore for the snapshot in us-east-1b before creating the volume.
Exercise
After restoring a volume and attaching it, which command must you not run, and why?
Show answer
mkfs. The restored volume already holds the snapshot's file system, and formatting it would erase the data. Mount the device directly.