Sub topic 2
EBS versus instance store, EFS and S3
This chapter assumes you know what an EC2 instance, an Availability Zone (AZ) and an AMI are. It also assumes you can use a Linux shell and the AWS CLI. The first chapter covered what EBS is. Here you will learn when to use EBS and when another storage service fits the workload better.
Four services, three kinds of storage
AWS offers three storage models, and each of the four services belongs to one of them.
- Block storage presents a raw disk to the operating system. You format it with a file system such as XFS and mount it. Amazon EBS and instance store are block storage.
- File storage presents a shared directory tree over a network protocol. Amazon EFS is file storage and uses NFS.
- Object storage stores whole objects, each with a key, through an HTTP API. Amazon S3 is object storage.
The model decides how your software reaches the data. A database engine that expects to write blocks to a device needs block storage. A fleet of web servers that share uploaded files needs file storage. A data lake or a backup archive fits object storage.
Amazon EBS
An EBS volume is network-attached block storage. It lives in one AZ, and EBS replicates it within that AZ. It is separate from the instance, so its data persists when you stop, start or terminate the instance. By default only the root volume is deleted on termination. You can change this with the DeleteOnTermination attribute, which a later chapter covers.
Key properties:
- Persistence. The volume exists until you delete it.
- Single AZ. A volume attaches only to instances in its own AZ. To move data to another AZ, you snapshot the volume and create a new volume from the snapshot.
- One instance at a time. A volume attaches to one instance, with one exception. Multi-Attach allows
io1andio2volumes to attach to several instances in the same AZ. - Provisioned size. You choose a size and pay for all of it, whether or not the file system is full.
- Adjustable. You can change size, type and performance while the volume is attached, using Elastic Volumes.
- Backups. Snapshots are incremental and stored in S3-backed storage that spans the Region.
Access goes over the instance's network path to storage. That adds latency compared with a local disk. A gp3 volume typically delivers single-digit millisecond latency, and io2 Block Express volumes can deliver sub-millisecond latency.
Instance store
Instance store volumes are disks that are physically attached to the host computer. They come with certain instance types, such as the d variants (m5d, c5d) and storage-optimized families like i3 and i4i. Instance store has no separate charge. The cost is part of the instance price.
Instance store is fast. NVMe instance store on storage-optimized types can reach very high IOPS with low latency, because there is no network hop. The trade-off is durability. Data is lost when:
- the instance stops or hibernates,
- the instance terminates,
- the underlying drive fails.
Data does survive an operating system reboot.
Instance store has other limits too:
- You cannot detach a volume and attach it to another instance.
- You choose the volumes at launch. You cannot add them later.
- You cannot snapshot them.
- The size is fixed by the instance type.
Use instance store for data you can rebuild or that is replicated elsewhere. Typical cases are caches, buffers, scratch space for sorting, and nodes of a distributed system that replicates data itself, such as Cassandra or Kafka.
You can tell the two kinds of disk apart on a Nitro-based instance with lsblk. You can also check from the CLI which instance types include instance store:
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,MODEL
aws ec2 describe-instance-types \
--instance-types m5.large m5d.large \
--query 'InstanceTypes[].[InstanceType,InstanceStorageSupported]' \
--output tableIn the lsblk output, the MODEL column reads Amazon Elastic Block Store for EBS volumes and Amazon EC2 NVMe Instance Storage for instance store disks. The CLI call reports False for m5.large and True for m5d.large.
Amazon EFS
Amazon Elastic File System is a managed NFS file system. You do not provision capacity. It grows and shrinks as you add and remove files, and you pay for the storage you use.
EFS differs from EBS in these ways:
- Shared access. Thousands of instances, containers and Lambda functions can mount the same file system at the same time.
- Regional scope. Data is stored across multiple AZs, and each AZ gets a mount target (a network endpoint). An instance in any AZ of the Region can mount it.
- File protocol. Clients use NFS v4. You do not format anything. The service manages the file system.
- Operating systems. EFS works with Linux clients. For Windows file shares, look at Amazon FSx for Windows File Server.
- Latency. Each operation crosses the network to a distributed service, so latency is higher than EBS. General Purpose mode gives low single-digit millisecond latency for the most common operations.
To mount EFS on Amazon Linux 2023, install the mount helper and mount by file system ID:
sudo dnf install -y amazon-efs-utils
sudo mkdir -p /mnt/efs
sudo mount -t efs -o tls fs-0123456789abcdef0:/ /mnt/efsThe instance's security group must allow NFS traffic (TCP port 2049) to the mount target. This is a frequent cause of mounts that hang.
EFS suits content management systems, shared home directories, build artifacts and machine learning training data that many workers read. It is a poor choice for a single-instance database. EBS is cheaper per IOPS there and has lower latency.
Amazon S3
S3 stores objects in buckets. An object is a blob of data of up to 5 TB, plus metadata, identified by a key. You reach it through HTTPS API calls (GET, PUT, DELETE) or SDKs. S3 does not present a disk, and you cannot format it with XFS or run a database engine on it.
Key characteristics:
- Capacity. A bucket has no size limit.
- Durability. S3 is designed for 99.999999999% (eleven nines) durability. It stores data across multiple AZs, except in the one-zone classes.
- Whole-object writes. You replace an object as a whole. You cannot modify bytes in the middle of an existing one.
- Cost. Storage per GB is lower than EBS or EFS, and storage classes such as Glacier lower it further. You pay per request and for data transfer out.
- Access. Many clients can read concurrently, from inside or outside AWS.
Tools such as Mountpoint for Amazon S3 let a Linux instance present a bucket as a directory. This is useful for reading large datasets. It supports only part of POSIX file semantics, so it does not replace a real file system for applications that edit files in place.
S3 and EBS often work together. EBS snapshots are stored in S3-managed storage, and a common pattern is to run a database on EBS and export backups or logs to S3.
Side-by-side comparison
| Property | EBS | Instance store | EFS | S3 |
|---|---|---|---|---|
| Storage model | Block | Block | File (NFS) | Object (HTTP API) |
| Scope | One AZ | One host | Region, multiple AZs | Region (default) |
| Survives instance stop | Yes | No | Yes | Yes |
| Concurrent instances | One (Multi-Attach: several, same AZ) | One | Thousands | Unlimited |
| Capacity | Provisioned, resizable | Fixed by instance type | Elastic | Unlimited |
| Typical latency | Single-digit ms or lower | Lowest | Low ms | Tens of ms |
| Billing basis | GB provisioned (plus IOPS and throughput for some types) | Included in instance price | GB used | GB used plus requests |
| Can boot an instance | Yes | Yes, for some AMIs | No | No |
Common errors when choosing
- Putting persistent data on instance store. The instance works until the first stop. Then the data is gone.
- Using EBS for shared data. Teams create a volume per instance and copy files between them. Use EFS or S3 instead, or Multi-Attach with a cluster-aware file system if you need block access.
- Using S3 as a disk. Applications that rewrite parts of files run slowly or fail on S3-backed mounts.
- Forgetting the AZ boundary. An EBS volume in
us-east-1acannot attach to an instance inus-east-1b. Plan failover with snapshots or with replication at the application level. - Paying for provisioned EBS space that is empty. EFS and S3 bill for use. EBS bills for what you provision.
Exercise
A team runs a PostgreSQL database on one EC2 instance. It also has a fleet of web servers that need to read the same set of uploaded images, and a nightly job that archives old logs cheaply. Which storage service fits each of the three needs, and why?
Show answer
PostgreSQL data: EBS (for example a gp3 or io2 volume), because the database needs a persistent block device with low latency that survives stop and start, and it has a single writer. Shared images: EFS, because many web servers can mount the same NFS file system at once across AZs. Log archive: S3, because it is the cheapest, effectively unlimited, durable object storage, and archived logs are written once and read rarely, which suits a lower-cost storage class.
Exercise
You launch an m5d.large instance, write a file to its instance store disk, then stop and start the instance. Is the file still there? What if you only reboot from the operating system?
Show answer
After stop and start the file is gone, because instance store data is lost when the instance stops, hibernates or terminates, and the instance may start on a different host. After an operating system reboot the data remains, because the instance stays on the same host.