Making ownCloud Faster Through Caching

ownCloud 8.1 image roundSome operations an ownCloud server executes take a lot of time to complete, such as expensive calculations or communication with a remote storage server. Some other operations don’t take particularly long – but are needed many, many times per second. To improve performance and reduce the load on the system caused by expensive or frequently needed work, ownCloud can cache the result of these operations. In ownCloud 8.1, caching has become an explicit setting. Read on to learn more about caching in ownCloud and how to configure it so you get the best possible ownCloud experience.

Caching in PHP

Caching is used in PHP to store compiled versions of the scripts so they don’t need to be recompiled on every request. This is called opcaching and has been included and enabled by default in PHP since the 5.5 release. On PHP 5.4, the oldest currently supported PHP release which works with ownCloud, it is highly recommended to install an opcache. The most used implementation is called APC (see the Caching section in our Performance Tuning documentation). It is a webserver setting, meaning it won’t require you to configure anything in ownCloud.

Memory caching on the other hand is used directly by web applications like ownCloud. It helps ownCloud avoid slow database queries or file system checks by retrieving a result from a memory cache, either on the local machine or distributed on a cluster of servers. The result is a trade-off of memory usage for improved performance. As the memory usage of these caches is typically small, it is generally worth the effort to set them up.

In PHP, a handful of memory caches, or ‘memcaches,’ exist. A typical one available on nearly all distributions is APCu, or the Alternative PHP User Cache. It runs locally in the PHP process and can save information across requests. It is only accessible by the local machine however, so for a cluster a distributed memcache is needed.

Memcached is a popular distributed memcache which runs as a separate service and communicates with PHP via TCP port 11211. Redis is also a distributed memcache, very similar to Memcached and is available on TCP port 6379. Redis is much more advanced than other memcaches, with support for atomic operations, which makes it more suitable for ownCloud and is the recommended option.

The principle of caching - animated
The principle of caching – animated

For a distributed memcache, the communication port can be exposed to the network, allowing multiple servers access, further improving performance as cached results are available to all servers in the cluster.

How does memcaching work?

A memcache is a key-value store. An application can store a value under a key and can then retrieve that value by searching the memcache with the key. Values may also have a Time To Live (TTL) set so that values eventually expire from the memcache and need to be re-inserted by the application. This is useful as a sanity check – if we get something wrong with cache expunging, the TTL acts as a backup to recalculate the values at some point.

ownCloud Memcaches

Memcaches can be configured in config.php, under the ‘memcache.local’ and ‘memcache.distributed’ parameters. ‘memcache.distributed’ will default to the value of ‘memcache.local’ if unset, so a single-server installation can just set a local memcache which will be used for everything. Setting an explicit distributed memcache is only useful for multiple servers in a cluster where the data that can be shared between servers is stored in a cache that is available to all the servers.

  • ownCloud supports the following memcaches:
  • APC (for PHP 5.4 and under)
  • APCu (for PHP 5.5 and above)
  • Redis (for distributed systems)
  • Memcached (not recommended with locking)
  • XCache (local like APCu; not recommended)

Highly recommended are: APCu for all installations and Redis for multi-server installations in the distributed memcache configuration field. See the memchache configuration options in the sample config.php documentation to understand the parameters needed as well as for additional configuration requirements for certain memcache backends. memcached is not recommended as it does not guarantee the availability of stored values which doesn’t play well with the ownCloud locking mechanism. If you use the locking mechanism you can configure a separate cache for the locking (“memcache.locking”). Outside of the locking issue, memcached should work without problems.

For home users, the typical setup would be to add this line to config.php:

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

But make sure that APCu is installed. Usually this package is named ‘php-apcu’ or ‘php5-apcu’. If you get a white screen upon visiting your ownCloud that means your cache configuration is broken! Make sure the memcache tool is installed and enabled correctly.

There is currently a caveat with APCu and the command line interface. You need to set the php.ini option apc.enable_cli = 1. See this issue for more details and the error you might get. We’re looking into this and will most likely provide a solution for 8.1.1, (coming out in about 3 weeks) which will make this no longer needed.

Set up your cache and enjoy a faster ownCloud!

Thanks to Robin ‘Xenopathic’ McCorkell for much of the write-up

27 Responses to “Making ownCloud Faster Through Caching”

  1. Paul Newbery

    OK, so what setup options do we use for php 5.5 which comes with opcache and not ACP?

  2. Skywalker

    I’m using Debian 8 with php 5.6.9. Since PHP 5.5.0 it has its own cache build in (http://php.net/manual/de/opcache.installation.php). What do I have to set as ‘memcache.local’ to use that?

  3. andreas G

    Raspbian, OC 8.1
    Do we have to change the Admin username and Admin password in the APC.php files in the /user/share/doc/php5-apc folder??? And what admin would that be root, local pi admin account, or www-data?
    Also will it work without copying it to the www root or owncloud root.? It keeps telling me “caching not enabled.
    Thanks a bunch.
    Andreas

  4. OKA

    Hello!

    i get this problem when i re scan data on owncloud

    i have owncloud lastversion on FREEBSD and PHP 5.4.43

    root@owncloud_1:/usr/local/www/owncloud # sudo -u www php occ files:scan
    An unhandled exception has been thrown:
    OC\HintException: [0]: Missing memcache class \OC\Memcache\APCu for local cache (Is the matching PHP module installed and enabled ?)
    root@owncloud_1:/usr/local/www/owncloud #

    i can’t find php-apcu’ or ‘php5-apcu apc.ini or apcu.ini

    • Chris

      Check all your php configs or ask at a community dedicated to your distro where this can be configured.

    • Jos

      I would also suggest to use the forums to get help, Chris is being awesome answering many questions here but it isn’t the best place 😉

      • andreas G

        I have been checking forums. Tougher using RaspberryPi.I have tried it all, but OC still says cache not enabled. But thanks Chris. It runs well so I’ll leave it. Have ‘memcache.local’ => ‘\OC\Memcache\APC’ in OC config.php. Also added required lines to APC.ini but no difference. phpinfo() says it’s enabled also. Anyways thanks and I’ll stop posting here.

  5. Chris

    When running on Ubuntu 14.04 also be aware that APCu and Redis can’t be used there because this release is shipping APCu/php5-apcu in version 4.0.2 (4.0.6 or higher is needed) and Redis/php5-redis in 2.2.4 (2.2.5 or higher is needed).

    The only caches there are APC (not installed from the repository), XCache or Memcached

  6. Michael Singer

    Unfortunately there seems to be no way to speed up ownCloud when one has no access to the server, e.g. on shared hosts.

    I’m successfully running ownCloud on a shared server (world4you.com), the only problem i’m experiencing is the lack of performance. OwnCloud is really slow on shared hosts. A lot of javascript files have to be called and loaded one by one (why not compressed as one file?), which takes several seconds. Also, loading of pictures and icons takes quite some time.

    Any improvement (for instance, a caching system as it exists in most CMS?) would be more than welcome!

    • Chris

      Check the asset pipeline (see config.sample.php)

      • Michael Singer

        Thanks so much Chris, i did not know that!

  7. Marcel

    Hi,

    unfortunately APCU is not available for my Raspberry Pi (Raspbian).

    • tony1

      use php-apc on the Rpi
      ~# apt-get install php-apc then edit apc.ini
      ~# nano /etc/php5/mods-available/apc.ini
      set ‘memcache.local’ => ‘\OC\Memcache\APC’, in config.php

      take care

      • Jos

        but that IS something else, isn’t it? APC vs APCu? I have Bananian, which comes with ancient PHP 5.4 – so even APC is ‘special’ technology… With it, setting up caching is a pain.

        • tony1

          I think of php-apc as being the same thing as opcache and apcu rolled into one package.
          the above commands should work on Bananian although I have not tried it but should work fine.
          all the settings should be in the apc.ini
          apc.enabled=1 and apc.enable_cli=1 being the most importaint and set ‘memcache.local’ => ‘\OC\Memcache\APC’, in config.php in config.php

      • Athena

        Hello Tony,

        I just upgraded my OC to 8.1.0 from 7.0.4 on my Raspberry. It seems to be working but I got memcache warning. I followed your suggestions and I’ve got that fixed too. Thank you. Here is my apc.ini file:

        extension=apc.so
        apc.enabled=1
        apc.enable_cli=1
        apc.shm_size=12M

        Are these sufficient to increase the speed in Raspberry pi? It is painfully slow. I am relatively new to Linux, and many suggestions are not directly applicable to Raspbian. For example I don’t know how to “move /usr/share/doc/php5-apcu/apc.php to your web root”? For example, apc.php is in the /user/share/doc directory. I do not have php5-apcu directory, but bunch of links in /usr/share/doc! I would appreciate step by step instructions and your suggestions to increase the speed. Thank you.

        • tony1

          On the Rpi the file I referenced is located at /usr/share/doc/php-apc/apc.php

          The terminal command ~ cp /usr/share/doc/php-apc/apc.php /var/www will get it to your webserver root on raspbian.

          The Rpi will be a little slow, it’s just a little guy after all!!

          I set my apc.shm_size=32M everyones case will be different but apc.php will show you allot of information and help you determine the correct value for your installation. 12M would not be enough in my case , 32M is borderline and 64M is excessive. it is a case by case scenario that’s for sure.

          There are other variables so look at the documentation. What is good for some people may not be good for you.

          Take care

          • tony1

            one more comment, I don’t think it is a very good idea to leave apc.php on the server.
            after testing either remove it or lock it down for security reasons.

            take care.

          • Athena

            Hello Tony,

            I changed apc_shm_size to 32M. APC shows that 15.8MB memory is free, 16.2MB is used with a hit rate of 98.4%. I hope these are OK. I deleted apc.php afterwards. Not much use, since I do not understand the stats! Thank you.

  8. ThFree

    Thanks. We verify the performance with Caching .) Good luck in the works.

  9. tony1

    If you are using php 5.4 in config.php you should use ‘memcache.local’ => ‘\OC\Memcache\APC’,
    apc.enable_cli = 1 and apc.enabled=1 can be set in apc.ini or apcu.ini as well as other settings.
    if you move /usr/share/doc/php5-apcu/apc.php to your web root you can see apc in action!!

    take care

    • Jos

      That’s a great tip/contribution, tony, thanks!!!

    • Rich**

      Tony, great tip re moving apc.php to see it in action!

Comments are closed.