Chapter 13. Enabling caching to improve logical volume performance

You can add caching to an LVM logical volume to improve performance. LVM then caches I/O operations to the logical volume using a fast device, such as an SSD.

The following procedures create a special LV from the fast device, and attach this special LV to the original LV to improve the performance.

13.1. Caching methods in LVM

LVM provides the following kinds of caching. Each one is suitable for different kinds of I/O patterns on the logical volume.

dm-cache

This method speeds up access to frequently used data by caching it on the faster volume. The method caches both read and write operations.

The dm-cache method creates logical volumes of the type cache.

dm-writecache

This method caches only write operations. The faster volume stores the write operations and then migrates them to the slower disk in the background. The faster volume is usually an SSD or a persistent memory (PMEM) disk.

The dm-writecache method creates logical volumes of the type writecache.

13.2. LVM caching components

When you enable caching for a logical volume, LVM renames and hides the original volumes, and presents a new logical volume that is composed of the original logical volumes. The composition of the new logical volume depends on the caching method and whether you are using the cachevol or cachepool option.

The cachevol and cachepool options expose different levels of control over the placement of the caching components:

  • With the cachevol option, the faster device stores both the cached copies of data blocks and the metadata for managing the cache.
  • With the cachepool option, separate devices can store the cached copies of data blocks and the metadata for managing the cache.

    The dm-writecache method is not compatible with cachepool.

In all configurations, LVM exposes a single resulting device, which groups together all the caching components. The resulting device has the same name as the original slow logical volume.

13.3. Enabling dm-cache caching for a logical volume

This procedure enables caching of commonly used data on a logical volume using the dm-cache method.

Prerequisites

  • A slow logical volume that you want to speed up using dm-cache exists on your system.
  • The volume group that contains the slow logical volume also contains an unused physical volume on a fast block device.

Procedure

  1. Create a cachevol volume on the fast device:

    # lvcreate --size cachevol-size --name fastvol vg /dev/fast-pv

    Replace the following values:

    cachevol-size
    The size of the cachevol volume, such as 5G
    fastvol
    A name for the cachevol volume
    vg
    The volume group name
    /dev/fast-pv
    The path to the fast block device, such as /dev/sdf1
  2. Attach the cachevol volume to the main logical volume to begin caching:

    # lvconvert --type cache --cachevol fastvol vg/main-lv

    Replace the following values:

    fastvol
    The name of the cachevol volume
    vg
    The volume group name
    main-lv
    The name of the slow logical volume

Verification steps

  • Examine the newly created devices:

    # lvs --all --options +devices vg
    
    LV              Pool           Type   Devices
    main-lv         [fastvol_cvol] cache  main-lv_corig(0)
    [fastvol_cvol]                 linear /dev/fast-pv
    [main-lv_corig]                linear /dev/slow-pv

Additional resources

  • You can also enable dm-cache caching in the cachepool configuration. This enables you to create the cache data and the cache metadata logical volumes individually and then combine the volumes into a single logical volume.

    For information on this procedure and other details, including administrative examples, see the lvmcache(7) man page.

13.4. Enabling dm-writecache caching for a logical volume

This procedure enables caching of write I/O operations to a logical volume using the dm-writecache method.

Prerequisites

  • A slow logical volume that you want to speed up using dm-writecache exists on your system.
  • The volume group that contains the slow logical volume also contains an unused physical volume on a fast block device.

Procedure

  1. If the slow logical volume is active, deactivate it:

    # lvchange --activate n vg/main-lv

    Replace the following values:

    vg
    The volume group name
    main-lv
    The name of the slow logical volume
  2. Create a deactivated cachevol volume on the fast device:

    # lvcreate --activate n --size cachevol-size --name fastvol vg /dev/fast-pv

    Replace the following values:

    cachevol-size
    The size of the cachevol volume, such as 5G
    fastvol
    A name for the cachevol volume
    vg
    The volume group name
    /dev/fast-pv
    The path to the fast block device, such as /dev/sdf1
  3. Attach the cachevol volume to the main logical volume to begin caching:

    # lvconvert --type writecache --cachevol fastvol vg/main-lv

    Replace the following values:

    fastvol
    The name of the cachevol volume
    vg
    The volume group name
    main-lv
    The name of the slow logical volume
  4. Activate the resulting logical volume:

    # lvchange --activate y vg/main-lv

    Replace the following values:

    vg
    The volume group name
    main-lv
    The name of the slow logical volume

Verification steps

  • Examine the newly created devices:

    # lvs --all --options +devices vg
    
    LV                VG Attr       LSize   Pool           Origin           Data%  Meta%  Move Log Cpy%Sync Convert Devices
     main-lv          vg Cwi-a-C--- 500.00m [fastvol_cvol] [main-lv_wcorig] 0.00                                    main-lv_wcorig(0)
     [fastvol_cvol]   vg Cwi-aoC--- 252.00m                                                                         /dev/sdc1(0)
     [main-lv_wcorig] vg owi-aoC--- 500.00m                                                                         /dev/sdb1(0)

Additional resources

  • For information, including administrative examples, see the lvmcache(7) man page.

13.5. Disabling caching for a logical volume

This procedure disables dm-cache or dm-writecache caching that is currently enabled on a logical volume.

Prerequisites

  • Caching is enabled on a logical volume.

Procedure

  1. Deactivate the logical volume:

    # lvchange --activate n vg/main-lv

    Replace the following values:

    vg
    The volume group name
    main-lv
    The name of the logical volume where caching is enabled
  2. Detach the cachevol or cachepool volume:

    # lvconvert --splitcache vg/main-lv

    Replace the following values:

    vg
    The volume group name
    main-lv
    The name of the logical volume where caching is enabled

Verification steps

  • Check that the logical volumes are no longer attached together:

    # lvs --all --options +devices [replaceable]_vg_
    
    LV      Attr       Type   Devices
    fastvol -wi------- linear /dev/fast-pv
    main-lv -wi------- linear /dev/slow-pv

Additional resources

  • The lvmcache(7) man page