Varnish Cache Plus

MSE 3.0

Varnish 6.0

Massive Storage Engine 3.0 (Varnish 6.0)

The Massive Storage Engine (MSE) is an advanced stevedore for Varnish Cache Plus. The stevedore is the component that handles storing the cached objects and their metadata, and keeping track of which objects in the cache are most relevant, and which to purge if needed to make room for new content. MSE adds several advanced features compared to standard stevedores that ship with Varnish:

  • Compact memory object structure. MSE has a more compact object storage structure giving less storage overhead. This is most noticable for small objects.

  • Fair LRU eviction strategy. When evicting content to make room for fresh content in the cache, the fetch task that does the eviction will be given priority to the space that it made available. This ensures that fetches does not fail due to other simultaneous fetch tasks stealing the space from under it.

  • Large caches using disks to cache objects. MSE can use disks as backing for object data, enabling cache sizes that are much larger than the available system memory. Content that is frequently used will be kept in memory, while less frequently used content will be read from disk instead of fetching from the backend.

  • Persisted caches. MSE will persist the disk stored objects, keeping the content in the cache between planned and unplanned restarts of the Varnish daemon.

Quick Start

Create the following MSE 3.0 conf file /var/lib/mse/mse.conf:

env: {
      id = "mse";
      memcache_size = "24G";

      books = ( {
              id = "book";
              directory = "/var/lib/mse/book";
              database_size = "2G";

              stores = ( {
                      id = "store";
                      filename = "/var/lib/mse/store.dat";
                      size = "100G";
              } );
      } );
};

In the above example, MSE will use 24GB of memory (memcache_size) and 100GB of disk (size). Change these values accordingly.

Next, initialize the MSE configuration:

mkfs.mse -c /var/lib/mse/mse.conf

You should now see a book directory and a store file in /var/lib/mse. This is called a “bookstore”.

Finally, add the MSE configuration to Varnish with the following startup parameter:

-s mse,/var/lib/mse/mse.conf

If there is an existing -s malloc,[size], replace it with the above MSE storage entry. Restart Varnish and it should now be using MSE 3.0.

For very large MSE stores, increase the startup_timeout to allow for a longer startup time.

Configuration and usage

MSE uses a structured configuration file to describe the layout of the devices to use for object storage. The syntax of the configuration file is shown in the examples below.

The configuration structure is hierarchical. At the top level there is one and exactly one environment, which configures the global sizes and rules to use for memory held object fragments.

An environment by itself configures a non-persistent cache. This makes MSE behave like a regular Varnish instance with a memory only cache, much like when using the default malloc stevedore, while giving the benefits of the compact object memory structure and fair LRU eviction.

To configure a persisted cache using disk for object storage, one or more books with associated stores needs to be configured in the environment. The books contain metadata internal to MSE, while the stores contain the object data used by Varnish.

Once the configuration file has been created, MSE can be enabled using the -s mse,<path-to-config-file> option to the Varnish daemon.

Persisted caching

When books and stores are configured, the cached objects are also persisted, keeping the content between restarts of the Varnish daemon.

The book is an embedded database that contains the necessary metadata about the objects stored in the cache. This includes e.g. the hash values for the objects, their associated TTLs and Vary matching information. The book also contains the maps for where in the store the payload data for the object resides, and the lists mapping free store space. Lastly the book has one journal file to persist bans on the system, and one journal file for each configured store to speed up metadata updates. All the data files that make up the book are kept in a directory, and the path to this directory is given in the configuration file.

Each book needs to have at least one store associated with it. The store holds the object payload data, consisting of its attributes (object headers, ESI instructions etc) and the object body. Each store is a single large file in the filesystem, that can contain any number of objects within it. New objects are assigned to a store on a round robin basis.

Keeping books and stores configured and stored separately is useful when the disks to use may not have the same IO capacity. It would be advisable to e.g. keep the book on a fast SSD type of drive, while using the larger but slower rotating disk for the store.

The data files, both for books and stores, needs to be initialized before starting the Varnish daemon for the first time. This is done using the bundled mkfs.mse utility, passing the configuration file as an option. See the mkfs.mse(1) manpage for details.

All the data files are marked with a version marker identifying the on disk data format. If this version marker does not match that of the Varnish daemon, Varnish will refuse to start, and the files will have to be recreated using the mkfs.mse utility with the -f force option, clearing the cache in the process. If a new release of Varnish Cache Plus comes with a new on disk format, the changelog entry will clearly say so.

Using MSE with SELinux

If SELinux is enabled, the MSE books and stores needs to be located on a path that the SELinux policy allows the Varnish daemon to access. The policy shipped with the packages enables the /var/lib/mse/* path for this purpose. If your stores are on separate drives, you will need to mount those drives below that path.

Example memory only configuration

The following is an example configuration for a memory only cache using 100 Gb of memory to hold cached objects.

env: {
	id = "myenv";
	memcache_size = "100G";
};

Example with 2 books each holding 2 stores

The following example demonstrates how to configure multiple books each holding multiple stores. In this example the server will use 100Gb of memory to hold frequently accessed object data. There will be 2 books, each configured for 1Gb metadata space. Each book has 2 stores, each holding 1Tb of object data.

env: {
	id = "myenv";
	memcache_size = "100G";

	books = ( {
		id = "book1";
		directory = "/var/lib/mse/book1";
		database_size = "1G";

		stores = ( {
			id = "store-1-1";
			filename = "/var/lib/mse/stores/disk1/store-1-1.dat";
			size = "1T";
		}, {
			id = "store-1-2";
			filename = "/var/lib/mse/stores/disk2/store-1-2.dat";
			size = "1T";
		} );
	}, {
		id = "book2";
		directory = "/var/lib/mse/book2";
		database_size = "1G";

		stores = ( {
			id = "store-2-1";
			filename = "/var/lib/mse/stores/disk3/store-2-1.dat";
			size = "1T";
		}, {
			id = "store-2-2";
			filename = "/var/lib/mse/stores/disk4/store-2-2.dat";
			size = "1T";
		} );
	} );
};

Example of inhomogenious setup with a default storage selection

This example is similar to the previous, but the stores are of different sizes. In addition, a default set of stores is selected through the default_stores parameter.

The two books in the configuration are of the same size, while their stores’ sizes differ by an order of magnitude. Although spinning disks is often a bad idea, it can be a good option if you have many huge, uncommonly requested files that you want to cache.

In the example, imagine that the second book contains two stores on spinning disks, while the first book use faster SSD/nVMe drives. By default, through the default_stores parameter, only the first book’s stores will be selected during object insertion::

env: {
	id = "myenv";
	memcache_size = "100G";

	books = ( {
		id = "book1";
		directory = "/var/lib/mse/book1";
		database_size = "1G";

		stores = ( {
			id = "store-1-1";
			filename = "/var/lib/mse/stores/disk1/store-1-1.dat";
			size = "1T";
		}, {
			id = "store-1-2";
			filename = "/var/lib/mse/stores/disk2/store-1-2.dat";
			size = "1T";
		} );
	}, {
		id = "book2";
		directory = "/var/lib/mse/book2";
		database_size = "1G";

		stores = ( {
			id = "store-2-1";
			filename = "/var/lib/mse/stores/disk3/store-2-1.dat";
			size = "10T";
		}, {
			id = "store-2-2";
			filename = "/var/lib/mse/stores/disk4/store-2-2.dat";
			size = "10T";
		} );
	} );
	default_stores = "book1";
};

It is possible to use vmod_mse to override the store selection on each individual backend request, and this will be the only way to get objects into book2 with the above configuration.

Tags and an example of how the are used

It is possible to attach tags to books and individual stores, and use these to select which stores. Consider the following, somewhat silly, example::

env: {
	id = "myenv";
	memcache_size = "100G";

	books = ( {
		id = "book1";
		directory = "/var/lib/mse/book1";
		database_size = "1G";
		tags = "red";

		stores = ( {
			id = "store-1-1";
			filename = "/var/lib/mse/stores/disk1/store-1-1.dat";
			size = "1T";
		}, {
			tags = ( "orange", "store-2-1" );
			id = "store-1-2";
			filename = "/var/lib/mse/stores/disk2/store-1-2.dat";
			size = "1T";
		} );
	}, {
		id = "book2";
		directory = "/var/lib/mse/book2";
		database_size = "1G";
		tags = ( "pink", "red" );

		stores = ( {
			id = "store-2-1";
			filename = "/var/lib/mse/stores/disk3/store-2-1.dat";
			size = "1T";
			tags = "green";
		}, {
			id = "store-2-2";
			filename = "/var/lib/mse/stores/disk4/store-2-2.dat";
			size = "1T";
			tags = ( "blue", "book1", "red" );
		} );
	} );
	default_stores = "none";
};

The example above is equal to the previous example, but tags have been added on both the books and the stores, and default_stores is set to the special value "none" (indicating that objects should be memory only by default).

Tags can be specified either as a single string, or as a list of strings, and they can be applied to books and stores.

When a set of stores is selected, either by using default_stores or vmod_mse, the string will be matched against book names, store names, and tags. In the example above, mse.set_stores("red"); will select all stores, since both books have been tagged "red". Even though store-2-2 is tagged "red" twice (in the book and the store itself), it will not be chosen twice as often as the other "red" stores.

There is no discrimination of names and tags, so mse.set_stores("book1"); will select all the stores in book1, and store-2-2, since this store has the "book1" tag. Similarly, mse.set_stores("store-2-1"); will select two stores, one because of a matching name, and the other because a tag matches.

Read more about store selection in the MSE VMOD documentation, where it also explained how stores are weighted after selection, and how to change it.

Further reading

In addition to the information above, the manual page varnish-mse(7) includes information on all configuration flags and parameters.