|
| 1 | +# Block device caching strategies |
| 2 | + |
| 3 | +Firecracker offers the possiblity of choosing the block device caching |
| 4 | +strategy. Caching strategy affects the path data written from inside the |
| 5 | +microVM takes to the host persistent storage. |
| 6 | + |
| 7 | +## How it works |
| 8 | + |
| 9 | +When installing a block device through a PUT /drives API call, users can choose |
| 10 | +the caching strategy by inserting a `cache_type` field in the JSON body of the |
| 11 | +request. The available cache types are: |
| 12 | + |
| 13 | +- `Unsafe` |
| 14 | +- `Writeback` |
| 15 | + |
| 16 | +### Unsafe mode (default) |
| 17 | + |
| 18 | +When configuring the block caching strategy to `Unsafe`, the device will |
| 19 | +advertise the VirtIO `flush` feature to the guest driver. If negotiated when |
| 20 | +activating the device, the guest driver will be able to send flush requests |
| 21 | +to the device, but the device will just acknowledge the request without |
| 22 | +actually performing any flushing on the host side. The data which was not |
| 23 | +yet committed to disk will continue to reside in the host page cache. |
| 24 | + |
| 25 | +### Writeback mode |
| 26 | + |
| 27 | +When configuring the block caching strategy to `Writeback`, the device will |
| 28 | +advertise the VirtIO `flush` feature to the guest driver. If negotiated when |
| 29 | +activating the device, the guest driver will be able to send flush requests |
| 30 | +to the device. When the device executes a flush request, it will perform an |
| 31 | +`fsync` syscall on the backing block file, committing all data in the host |
| 32 | +page cache to disk. |
| 33 | + |
| 34 | +## Supported use cases |
| 35 | + |
| 36 | +The caching strategy should be used in order to make a trade-off: |
| 37 | + |
| 38 | +- `Unsafe` |
| 39 | + - enhances performance as fewer syscalls and IO operations are performed when |
| 40 | + running workloads |
| 41 | + - sacrifices data integrity in situations where the host simply loses the |
| 42 | + contents of the page cache without committing them to the backing storage |
| 43 | + (such as a power outage) |
| 44 | + - recommended for use cases with ephemeral storage, such as serverless |
| 45 | + environments |
| 46 | +- `Writeback` |
| 47 | + - ensures that once a flush request was acknowledged by the host, the data |
| 48 | + is committed to the backing storage |
| 49 | + - sacrifices performance, from boot time increases to greater |
| 50 | + emulation-related latencies when running workloads |
| 51 | + - recommended for use cases with low power environments, such as embedded |
| 52 | + environments |
| 53 | + |
| 54 | +## How to configure it |
| 55 | + |
| 56 | +Example sequence that configures a block device with a caching strategy: |
| 57 | + |
| 58 | +```bash |
| 59 | +curl --unix-socket ${socket} -i \ |
| 60 | + -X PUT "http://localhost/drives/dummy" \ |
| 61 | + -H "accept: application/json" \ |
| 62 | + -H "Content-Type: application/json" \ |
| 63 | + -d "{ |
| 64 | + \"drive_id\": \"dummy\", |
| 65 | + \"path_on_host\": \"${drive_path}\", |
| 66 | + \"is_root_device\": false, |
| 67 | + \"is_read_only\": false, |
| 68 | + \"cache_type\": \"Writeback\" |
| 69 | + }" |
| 70 | +``` |
0 commit comments