Memory Caching

Introduction

You can significantly improve ownCloud server performance by using memory caching. This is the process of storing frequently-requested objects in-memory for faster retrieval later. There are two types of memory caching available:

A PHP opcode Cache (OPcache): An opcode cache stores compiled PHP scripts so they don’t need to be re-compiled every time they are called. These compiled PHP scripts are stored in-memory, on the server on which they’re compiled.

A Data Cache: A data cache stores copies of data, templates, and other types of information-based files. Depending on the cache implementation, it can be either local, or specific, to one server, or distributed across multiple servers. This cache type is ideal when you have a scale-out installation.

Supported Caching Backends

The caching backends supported by ownCloud are:

  • APCu: APCu 4.0.6 and up is required. Alternatively you can use the Zend OPCache. However, it is not a data cache, only an opcode cache.

  • Redis: This is a distributed cache for multi-server ownCloud installations. Version 2.2.6 or higher of the PHP Redis extension is required.

  • Memcached: This is a distributed cache for multi-server ownCloud installations.

You may use both a local and a distributed cache. The recommended ownCloud caches are APCu and Redis. If you do not install and enable a local memory cache you will see a warning on your ownCloud admin page. If you enable only a distributed cache in your config.php (memcache.distributed) and not a local cache (memcache.local) you will still see the cache warning.

Cache Directory Location

The cache directory defaults to data/$user/cache where $user is the current user. You may use the 'cache_path' directive in config.php (See config_sample_php_parameters) to select a different location.

Quick Configuration Example

Install APCu and Redis:

apt install php-apcu php-redis redis-server

Enable them by adding this to your config.php:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
    'host' => 'localhost',
    'port' => 6379,
],

Restart your web server and you are ready to go.

Cache Types

APCu

We recommend that you use APCu, because it is both a data cache and available in most Linux distributions.

Installing APCu

# On Ubuntu/Debian/Mint systems
apt install php-apcu

With that done, assuming that you don’t encounter any errors, restart Apache and the extension is ready to use.

Redis

Redis is an excellent modern memory cache to use for both distributed caching and as a local cache for transactional file locking, because it guarantees that cached objects are available for as long as they are needed.

The Redis PHP module must be at least version 2.2.6 or higher. If you are running a Linux distribution that does not package the supported versions of this module — or does not package Redis at all — see Installing Redis on other distributions.

Installing Redis

If you have Ubuntu 16.04 or higher:

apt install redis-server php-redis

The installer will automatically launch Redis and configure it to launch at startup.

After that, assuming that you don’t encounter any errors, restart Apache and the extension is ready to use.

Additional notes for Redis vs. APCu on Memory Caching

APCu is faster at local caching than Redis. If you have enough memory, use APCu for memory caching and Redis for file locking. If you are low on memory, use Redis for both.

Clearing the Redis Cache

The Redis cache can be flushed from the command-line using the redis-cli tool, as in the following example:

sudo redis-cli
SELECT <dbIndex>
FLUSHDB

<dbIndex> is the number of Redis database where the cache is stored. It is zero by default at ownCloud. To check what yours is currently set to, check the dbindex value in config/config.php. Here’s an example of what to look for:

'redis' => [
    'host' => 'localhost',       // For a Unix domain socket, use '/var/run/redis/redis.sock'
    'port' => 6379,  // Set to 0 when using a Unix socket
    'timeout' => 0,
    'password' => '',            // Optional, if not defined no password will be used.
    'dbindex' => 0               // Optional, if undefined SELECT will not run and will
                                 // use Redis Server's default DB Index.
],

Memcached

Memcached is a reliable old-timer for shared caching on distributed servers. It performs well with ownCloud with one exception: it is not suitable to use with Transactional File Locking. This is because it does not store locks, and data can disappear from the cache at any time. Given that, Redis is the best memory cache to use.

Be sure to install the memcached PHP module, and not memcache, as in the following examples. ownCloud supports only the memcached PHP module.

Installing Memcached

On Ubuntu/Debian//Mint

On Ubuntu/Debian/Mint run the following command:

apt-get install memcached php5-memcached
The installer will automatically start memcached and configure it to launch at startup.

Configuration File Paths

PHP Version Filename

7.1

/etc/opt/rh/rh-php71/php.d/25-memcached.ini

After that, assuming that you don’t encounter any errors:

  1. Restart your Web server

  2. Add the appropriate entries to config.php (which you can find an example of below)

  3. Refresh your ownCloud admin page

Clearing the Memcached Cache

The Memcached cache can be flushed from the command-line using a range of common Linux/Unix tools, including netcat and telnet. The following example uses telnet to login, run the flush_all command, and logout:

telnet localhost 11211
flush_all
quit

For more information see:

Configuring Memory Caching

Memory caches must be explicitly configured in ownCloud by:

  1. Installing and enabling your desired cache (whether that be the PHP extension and/or the caching server).

  2. Adding the appropriate entry to ownCloud’s config.php.

See config_sample_php_parameters for an overview of all possible config parameters. After installing and enabling your chosen memory cache, verify that it is active by viewing the PHP configuration details.

APCu Configuration

To use APCu, add this line to config.php:

'memcache.local' => '\OC\Memcache\APCu',

With that done, refresh your ownCloud admin page, and the cache warning should disappear.

Redis Configuration

Regardless of whether you have setup Redis to use TCP or a Unix socket, we recommend adding the following for best performance:

'memcache.locking' => '\OC\Memcache\Redis',

Redis Configuration Using TCP

The following example config.php configuration connects to a Redis cache via TCP:

'memcache.local' => '\OC\Memcache\Redis',
'redis' => [
    'host' => 'localhost',
    'port' => 6379,
],

Redis Configuration Using Unix Sockets

If Redis is running on the same server as ownCloud, it is recommended to configure it to use Unix sockets. Then, configure ownCloud to communicate with Redis, as in the following example.

# Change the host value, based on the socket's location in your distribution
'memcache.local' => '\OC\Memcache\Redis',
'redis' => [
     'host' => '/var/run/redis/redis.sock',
     'port' => 0,
],

If setting up Redis to be accessed via a Unix socket from a webserver user, then consider the following:

  1. Make the webserver user www-data member of the group redis in /etc/group, e.g., redis:x:110:www-data

  2. In your Redis configuration (/etc/redis/redis.conf) set unixsocketperm to 770

To see a benchmark comparison, run:

sudo redis-benchmark -q -n 100000
sudo redis-benchmark -s /var/run/redis/redis-server.sock -q -n 100000

Redis is very configurable; consult the Redis documentation to learn more.

Memcached Configuration

Redis is very configurable; This example uses APCu for the local cache, Memcached as the distributed memory cache, and lists all the servers in the shared cache pool with their port numbers:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Memcached',
'memcached_servers' => [
     ['localhost', 11211],
     ['server1.example.com', 11211],
     ['server2.example.com', 11211],
 ],

Configuration Recommendations Based on Type of Deployment

Small/Private Home Server

// Only use APCu
'memcache.local' => '\OC\Memcache\APCu',

Small Organization, Single-server Setup

Use APCu for local caching, Redis for file locking

'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
    'host' => 'localhost',
    'port' => 6379,
],

Large Organization, Clustered Setup

Use Redis for everything except a local memory cache. Use the server’s IP address or hostname so that it is accessible to other hosts:

'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'memcache.local' => '\OC\Memcache\APCu',
'redis' => [
    'host' => 'server1',      // hostname example
    'host' => '12.34.56.78',  // IP address example
    'port' => 6379,
],

Configuring Transactional File Locking

Transactional File Locking prevents simultaneous file saving. It is enabled by default and uses the database to store the locking data. This places a significant load on your database. It is recommended to use a cache backend instead. You have to configure it in config.php as in the following example, which uses Redis as the cache backend:

'filelocking.enabled' => true,
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
     'host' => 'localhost',
     'port' => 6379,
     'timeout' => 0.0,
     'password' => '', // Optional, if not defined no password will be used.
 ],
For enhanced security it is recommended to configure Redis to require a password. See http://redis.io/topics/security for more information.

Caching Exceptions

If ownCloud is configured to use either Memcached or Redis as a memory cache, please be aware that you may encounter issues with functionality. When these occur, it is usually a result of PHP being incorrectly configured, or the relevant PHP extension not being available.

In the table below, you can see all of the known reasons for reduced or broken functionality related to caching.

Setup/Configuration Result

If file locking is enabled, but the locking cache class is missing, then an exception will appear in the web UI

The application will not be usable

If file locking is enabled and the locking cache is configured, but the PHP module missing.

There will be a white page/exception in web UI. It will be a full page issue, and the application will not be usable

All enabled, but the Redis server is not running

The application will be usable. But any file operation will return a "500 Redis went away" exception

If Memcache is configured for local and distributed, but the class is missing

There will be a white page and an exception written to the logs, This is because autoloading needs the missing class. So there is no way to show a page