Accelerate cloud disk performance with local disk

Updated at:

Read-intensive workloads, such as inference model loading and high-concurrency database queries, are usually limited by the IOPS and latency of a single cloud disk, not its capacity. Disk data cache serves hot reads from the node's local disk while the cloud disk persists data, cutting read latency without application changes.

How it works

In a Container Service for Kubernetes (ACK) cluster, disk data cache is built on the Linux kernel's dm-cache mechanism. csi-plugin creates the cache automatically when it mounts a cloud disk volume, and the application still sees an ordinary volume: neither application code nor mount parameters need to change.

  • Cache layer: The local disk on the node. csi-plugin creates two cache files in the local disk mount directory /var/alibaba-cloud-csi/data-cache/: <volumeID>.data holds the cached data blocks and is sized by dataCacheSize; <volumeID>.meta maps those blocks to their locations on the cloud disk and is sized automatically. <volumeID> is the ID of the cloud disk behind the volume.

  • Origin: The cloud disk bound to the Persistent Volume Claim (PVC). All data eventually resides on the cloud disk, and the local disk holds no persistent data.

  • Cache device: The dm-cache device /dev/mapper/csi-datacache-<volumeID>. csi-plugin attaches each cache file to a loop device, combines the loop devices with the cloud disk as the origin, and then formats and mounts that device instead of the raw cloud disk, so all pod I/O passes through the local cache.

    Reads that hit the cache are served by the local disk, which improves read latency and IOPS. Reads that miss go to the cloud disk, and dm-cache fills the cache according to its policy. Writes reach the cloud disk at a time determined by the cache mode, as described in Cache mode.

If a node does not meet the runtime conditions (for example, the local disk is not mounted at the required directory, or the kernel does not support dm-cache), or if the cache files cannot be created, csi-plugin does not fail the pod mount. It skips the cache, mounts the raw cloud disk, and records a DataCacheFallback event on the pod. The workload reads and writes normally but gets no acceleration.

image

Prerequisites

The cluster and its nodes must meet all of the following conditions:

  • The csi-plugin add-on is v1.37.2 or later. Disk data cache is built into the Container Storage Interface (CSI) driver for cloud disks, so no extra plugin is required. Earlier versions ignore the cache parameters and mount the volume as a regular cloud disk. In the left-side navigation pane of the cluster management page, click Components and Add-ons to check the csi-plugin version and upgrade it.

  • The node runs Linux, and its kernel supports device-mapper, loop devices, and dm-cache. Cache files reach dm-cache through loop devices, so the cache device cannot be created if any of the three is missing. Check each one on the node:

    # device-mapper: available if this device file exists.
    ls /dev/mapper/control
    # Loop device: available if this device file exists.
    ls /dev/loop-control
    # dm-cache: load the kernel module and confirm that it is loaded.
    modprobe dm_cache && lsmod | grep dm_cache

    The node meets the kernel requirements when all three commands return normally. dm-cache availability depends on whether the node image includes the kernel module, not on the kernel version. If modprobe dm_cache fails, the node image does not support dm-cache, and the node must be recreated with a different image.

  • The node has a local disk mounted at /var/alibaba-cloud-csi/data-cache/. Pods that use disk data cache must be scheduled to nodes where this mount is complete. For the mount procedure, see Step 1: Mount a local disk on the node.

  • Cloud disk volumes already work in the cluster. The csi-plugin and csi-provisioner add-ons are installed, and the cloud disk type matches the node's Elastic Compute Service (ECS) instance type. For cloud disk selection, performance, and limits, see Disk volumes.

Plan the cache capacity and cache mode

The dataCacheSize and dataCacheMode parameters of the StorageClass determine the cache capacity and the write policy of disk data cache. Plan both values before you create the StorageClass.

Important

StorageClass parameters cannot be modified after the StorageClass is created. To adjust the cache capacity or the cache mode, create a new StorageClass and recreate the PVC.

Cache capacity

dataCacheSize is the cache capacity allocated on the local disk for a single volume, and it also acts as the switch. A non-zero value enables the cache, and omitting the parameter or setting it to 0 mounts the volume as a regular cloud disk. The value is a Kubernetes quantity, such as 20Gi, and must be smaller than the capacity available under /var/alibaba-cloud-csi/data-cache/ on the node. All cache-enabled volumes on a node share that capacity, so an oversized value can prevent other volumes from creating a cache.

StorageClass parameter validation fails if dataCacheMode is set without a non-zero dataCacheSize.

Cache mode

dataCacheMode determines when writes reach the cloud disk, which also affects how recoverable the data is if a node fails:

  • writethrough (default): Each write is acknowledged only after it lands on both the local disk cache and the cloud disk, so the cache accelerates reads only. The cloud disk always holds a complete copy, so it can be detached from a failed node and attached to another one, where the pod continues to run (fault migration). Use this mode for most production workloads, especially read-intensive ones.

  • writeback: A write is acknowledged as soon as it lands in the local disk cache and is flushed to the cloud disk asynchronously, so both reads and writes are accelerated. However, the cache holds unflushed data, and this mode does not provide the fault migration of writethrough. Use it only where data loss is acceptable, such as for rebuildable cache data or temporary computation results.

Important

In writeback mode, csi-plugin writes all unflushed data back to the cloud disk during a normal unmount, and a node restart does not discard the cache. The data is lost only if the node's local disk itself fails. Write-back time depends on the amount of pending data: a large amount noticeably slows cloud disk detachment and therefore the workload's migration to another node. Keep the default writethrough for volumes that carry persistent data.

Configure disk data cache

Step 1 is performed on the node and only once per node. The remaining steps are performed in the cluster with kubectl.

Step 1: Mount a local disk on the node

Cache files are always created in /var/alibaba-cloud-csi/data-cache/, so the local disk must be mounted to that directory on every node that uses disk data cache.

  1. Log on to the node, format the local disk, and mount it to /var/alibaba-cloud-csi/data-cache/. The following example uses the ext4 file system. Replace /dev/nvme0n1 with the actual local disk device name.

    mkfs.ext4 /dev/nvme0n1
    mkdir -p /var/alibaba-cloud-csi/data-cache
    mount /dev/nvme0n1 /var/alibaba-cloud-csi/data-cache
    Important

    Formatting erases all existing data on the local disk. Confirm that the disk can be used for caching before you run these commands. To remount the disk automatically after the node restarts, add the mount entry to /etc/fstab.

  2. As a best practice, create a dedicated node pool for nodes that have a local disk, apply the preceding initialization consistently across the pool, and then use node affinity to keep pods that use disk data cache on that pool. Otherwise, a pod can land on a node that does not meet the cache conditions and lose the acceleration. If the node operating system does not support dm-cache, replace the node image with one that does, and then recreate the nodes.

Step 2: Create a StorageClass that enables disk data cache

Disk data cache is enabled through StorageClass parameters, so create a dedicated StorageClass for the PVC to reference.

  1. Create disk-datacache-sc.yaml.

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: alicloud-disk-datacache
    # Driver type. This value is fixed when you use the Alibaba Cloud disk CSI plugin.
    provisioner: diskplugin.csi.alibabacloud.com
    parameters:
      # Cloud disk type.
      type: cloud_essd
      # File system type.
      fstype: ext4
      # Cache capacity. Configuring this parameter enables disk data cache.
      dataCacheSize: "20Gi"
      # Cache mode. writethrough is the default value.
      dataCacheMode: writethrough
    # Binding mode. Delayed binding is recommended so that the cloud disk is created in the zone where the pod is actually scheduled.
    volumeBindingMode: WaitForFirstConsumer
    # Reclaim policy.
    reclaimPolicy: Delete
    # Allow volume expansion.
    allowVolumeExpansion: true

    The following table describes the disk data cache parameters. For the meaning and values of the other parameters, see Use a dynamic cloud disk volume.

    Parameter

    Type

    Required

    Default

    Description

    dataCacheSize

    string

    Required to enable disk data cache

    None

    The cache capacity allocated on the local disk for the volume. A non-zero value enables disk data cache. The value is a Kubernetes quantity, such as 20Gi. For capacity planning, see Cache capacity.

    dataCacheMode

    string

    No

    writethrough

    The cache write policy. Valid values: writethrough and writeback. For the difference in write performance and fault migration, and for how to choose, see Cache mode.

  2. Create the StorageClass.

    kubectl create -f disk-datacache-sc.yaml
  3. Check whether the StorageClass is created.

    kubectl get sc alicloud-disk-datacache

    The expected output is similar to the following, where PROVISIONER is the cloud disk CSI plugin:

    NAME                      PROVISIONER                       RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
    alicloud-disk-datacache   diskplugin.csi.alibabacloud.com   Delete          WaitForFirstConsumer   true                   10s

    Alternatively, choose Storage > StorageClasses in the left-side navigation pane of the cluster management page and confirm the new StorageClass in the list.

Step 3: Create a PVC and mount it in an application

Disk data cache is transparent to the application: the PVC and workload definitions are the same as for a regular cloud disk volume. Set storageClassName to the StorageClass created in the previous step.

  1. Create disk-datacache-pvc.yaml.

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: disk-datacache-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      volumeMode: Filesystem
      resources:
        requests:
          # Requested storage capacity, which is the cloud disk size.
          storage: 100Gi
      # Reference the StorageClass that enables disk data cache.
      storageClassName: alicloud-disk-datacache
  2. Create the PVC.

    kubectl create -f disk-datacache-pvc.yaml
  3. Create datacache-app.yaml to mount the PVC in the application.

    A cloud disk is non-shared storage: without multi-attach, it can be attached to only one pod at a time, so set the replica count to 1. For multiple replicas, use a StatefulSet with volumeClaimTemplates to request a separate volume per replica.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: datacache-app
    spec:
      # A cloud disk is non-shared storage. Set replicas to 1.
      replicas: 1
      selector:
        matchLabels:
          app: datacache-app
      template:
        metadata:
          labels:
            app: datacache-app
        spec:
          containers:
          - name: nginx
            image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            volumeMounts:
            # Mount the volume to the /data directory in the container.
            - name: datacache-volume
              mountPath: /data
          volumes:
          - name: datacache-volume
            persistentVolumeClaim:
              claimName: disk-datacache-pvc
  4. Deploy the application and confirm that the pod is in the Running state.

    kubectl create -f datacache-app.yaml
    kubectl get pod -l app=datacache-app

Step 4: Verify that disk data cache takes effect

A running pod only shows that the cloud disk is mounted. Confirm that csi-plugin has also created the cache device; otherwise, the volume may have fallen back to a regular cloud disk.

  1. Query the ID of the cloud disk behind the volume. The cache device and cache file names are both derived from this ID.

    # Query the PV bound to the PVC.
    kubectl get pvc disk-datacache-pvc -o jsonpath='{.spec.volumeName}'
    # Query the ID of the cloud disk behind the PV. Replace <pv-name> with the output of the preceding command.
    kubectl get pv <pv-name> -o jsonpath='{.spec.csi.volumeHandle}'

    The output is a cloud disk ID in the form d-bp1xxxxxxxxxxxxxxxx, which is the <volumeID> used in the following steps.

  2. Log on to the node where the pod runs, confirm that the cache device exists, and check its status. Replace <volumeID> with the cloud disk ID obtained in the previous step.

    ls -l /dev/mapper/csi-datacache-<volumeID>
    dmsetup status csi-datacache-<volumeID>

    The cache is in effect if the device exists and dmsetup status returns output, which includes cache block usage, hit and miss counts, dirty block count, I/O mode, and the current cache policy (mq during normal operation). If the device does not exist, the volume either has not enabled disk data cache or has fallen back to a regular cloud disk.

  3. View the cache files on the local disk of the node.

    ls -lh /var/alibaba-cloud-csi/data-cache/

    Normally, the directory contains <volumeID>.data and <volumeID>.meta, and the size of .data matches dataCacheSize.

  4. Check whether a cache fallback event exists.

    kubectl get events --field-selector reason=DataCacheFallback

    No output means that no fallback occurred. A DataCacheFallback event means that the node does not meet the cache conditions and csi-plugin mounted the cloud disk directly. Check the node against each item in Prerequisites.

Expand a cloud disk volume

Because the StorageClass sets allowVolumeExpansion: true, modify the capacity requested by the PVC to expand the underlying cloud disk, exactly as for a regular cloud disk volume. csi-plugin expands the cloud disk, extends the dm-cache device to match the new capacity, and then expands the file system, with no manual intervention required. The cache capacity on the local disk stays at dataCacheSize and does not grow with the cloud disk.

Limits

  • Cloud disk limits still apply. A cloud disk without multi-attach enabled can be attached to only one pod at a time, and cross-zone attachment is not supported.

  • Disk data cache takes effect only on nodes that meet all conditions in Prerequisites. On other nodes, the volume falls back to a regular cloud disk.

  • Disk data cache accelerates the read path. Writes are accelerated only in writeback mode. Workloads that are not latency-sensitive, or that write data once and rarely read it again, gain little from the cache.

  • The cache is local to the node. After a pod and its volume move to another node, the cache files on the original node are cleaned up during unmount, and the new node must build and warm up the cache again, during which read performance matches a regular cloud disk.

  • dataCacheSize and dataCacheMode cannot be modified after the StorageClass is created. Determine both values in advance, as described in Plan the cache capacity and cache mode.

Billing

Disk data cache does not change how the cloud disk is used. The cloud disk that backs the volume is still billed on a pay-as-you-go basis. For cloud disk prices, see Elastic Block Storage (EBS) pricing. The local disk comes with the node's ECS instance type and is not billed separately.

FAQ

The pod is running, but the cache device cannot be found. What do I do?

The volume either has not enabled the cache or has already fallen back. Run kubectl get events --field-selector reason=DataCacheFallback to check for a fallback event, and then check, in order: dataCacheSize in the StorageClass, the StorageClass referenced by the PVC, the csi-plugin version on the node (v1.37.2 or later), the local disk mounted at /var/alibaba-cloud-csi/data-cache/, and the free space in that directory (at least dataCacheSize).

The node does not have a local disk mounted. Does this affect the workload?

No. When csi-plugin cannot create the cache, it mounts the cloud disk directly and records a DataCacheFallback event. The workload reads and writes normally but gets no local disk acceleration. To enable acceleration, complete the mount described in Step 1: Mount a local disk on the node, and use node affinity to keep the pod on those nodes.

The node operating system does not support dm-cache. What do I do?

Disk data cache depends on the dm-cache module of the node kernel. Without it, the volume falls back to a regular cloud disk. Replace the node image with one that supports device-mapper and dm-cache, recreate the node, and then reschedule the pods that use the volume.

Can I change dataCacheMode to writeback to improve write performance?

Yes, but dataCacheMode cannot be modified after the StorageClass is created, so create a new StorageClass that sets writeback and recreate the PVC. In this mode, the cache may hold unflushed data, which is lost if the node's local disk fails. See Cache mode for the trade-off, and keep the default writethrough for volumes that carry persistent data.

References