Executive Summary
Restoring an Amazon Elastic Block Store (EBS) volume from a snapshot does not necessarily mean every block will be ready when an application first accesses it. Depending on the volume and its initialization state, early filesystem operations and file reads can incur additional latency as data is made available. Consider restoring an Appian environment from a snapshot. When services start and begin accessing files on the restored volume, these initial operations can take longer than expected, delaying environment readiness. In our investigation, we explored whether we could reduce this overhead by preparing the filesystem metadata and file data the application needs, rather than always reading an entire volume.
This led us to Hydrator, an approach that combines filesystem scanning and targeted file prewarming. Using this approach, we reduced our Appian Engine startup time on a cold EBS volume from up to 90 minutes down to 10 minutes for customer sites with large volumes (>40GB) of engine data.
Understanding the cold EBS problem
To understand the problem, we need to look at two distinct sources of delay.
- Unresponsive File System on Cold EBS
When I ran ls -al on a freshly started EBS Volume, it took 6m 38s. Rerunning the same command took 11.4s. The first time it took 6m 38s seconds because it had to restore inode information for all files from S3 and store it in local EBS blocks, and it restored them sequentially, one block at a time. Each S3 read IO call takes 100ms! This means a script that lists files in a folder on a cold volume can take a long time. View detailed logs. - Slow Reading a Large File on Cold EBS
The file and the file system are slowly restored from S3 block by block. So if a program scans large files, it can take a long time to scan the whole file.
Solutions Offered by AWS
- Using dd/fio to read the entire volume — Prewarming the entire disk is inefficient just to start some apps on cold EBS, many apps just need some files to be warm to start faster.
- Fast snapshot restore (FSR) — AWS can provide a fully hot EBS volume snapshot using FSR, but it takes 1hr per TiB snapshot to be FSR ready. We weren’t able to afford to wait for the snapshot to be restored.
- Faster volume initialization rate — AWS can initialize EBS volume faster by increasing volume initialization rate from 100MiB to 300MiB, but this doesn’t guarantee the files that you need would be initialized faster.
Since the above methods were not helpful for Appian, we developed Hydrator CLI.
Basics: Understanding Limits of EBS
Press enter or click to view image in full size
Only 4 variables control EBS and its behavior.
- Volume Type (io2/gp3).
- IOPs (GP3 => 3000–80,000).
- Throughput (125 MBPS — 2000 MBPS).
- Initialization Rate (100MiB — 300MiB).
IOPs = Number of I/O calls executed in one second.
Throughput = Amount of data you can read from EBS.
Volume Type = We have 2 volume types: io2 — costly, gp3 — cheaper.
We should maximize the use of these variables, which we can control.
How to Prewarm a Cold File System on EBS ?
The solution is to use fd. Fd is a multi-threaded find; we can use it to scan all required directories faster. Once we look up the directory entries, the subsequent reads are faster. If you have a script that doesn’t work well with a cold file system, you can run this find command to maximize IOPS and keep the file system you’re interested in warm.
$ fd — threads 64 — full-path /path/to/pre-warm
Why do we need to scan parallelly?
We need to understand XFS, the file system. The XFS file system stores metadata in multiple disk blocks scattered throughout the disk. So, when we run ls, we must scan this metadata across multiple blocks. Each block read takes 100ms for AWS to load the data from S3. The way XFS stores data and how AWS restores block data from S3 lazily is what makes such a simple operation, ls, run for 60 seconds.
Would fd solve all the problems?
If all the files are located in a single directory, scanning directory entries cannot be parallelized, causing prewarming to take longer. It is recommended to split the files into multiple directories so they can be scanned in parallel much faster.
How to Prewarm a Cold File on EBS ?
Press enter or click to view image in full size
We did some performance latency analysis experiments. In those experiments, we measured the latency of reading a large file sequentially in 2 KB chunks. We learned that when we request 2 KB of data, AWS actually restores 512 KB from S3. Using this 2KB-512 KB leverage, we can make many parallel I/O calls, maximize I/O calls, and reduce the throughput limit. The core idea is to do one 2 KB read I/O call for every 512 KB block in parallel. This reduces the throughput bottleneck and increases the pre-warming gains for each I/O call. Previously, using dd/fio, we could prewarm a 60 GB file in 25 minutes; now, using this technique, we prewarmed it in just 8 minutes.
Press enter or click to view image in full size
With default GP3 storage, we could only run 3000 IO per second, and each I/O call could pre-warm 512 KB. With a 125 MB throughput limit, we could force AWS’s EBS Systems to prewarm the disk at full capacity of 125 MB per second. This method has worked better than AWS’s suggested methods because it lets us forcefully prewarm only the necessary files for starting the site faster instead of prewarming the entire EBS Volume.
IDEA: Why warm entire files when partial initialization is enough?
Sometimes, for a service to start, we don’t need the whole file warm; for example, MySQL reads only some header details and doesn’t parse the entire file, so we prewarm the first few bytes to start faster.
Results
By using the below recommended prewarming file method, we reduced Appian Engine (60GB — multiple engine files) startup times on cold EBS volumes from 90 minutes to 10 minutes. Prewarming file system metadata also improved scan speeds across the file system.
In conclusion, it is recommended to leverage Appian’s Hydrator CLI or execute fio alongside fd (using the appropriate configurations) to effectively prewarm both files and the file system, thereby mitigating cold EBS volume latency issues. Hydrator CLI code can be found in https://github.com/appian/tech-blog-examples/tree/main/hydrator-cli