Skip to content

Commit 9c788bf

Browse files
committed
fix: add documentation about the new KeyValueCache
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent cfeaa88 commit 9c788bf

3 files changed

Lines changed: 206 additions & 10 deletions

File tree

admin_manual/configuration_files/files_locking_transactional.rst

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,38 @@ the database load and improves performance. Admins of Nextcloud servers with
2828
heavy workloads should install a memcache. (See
2929
:doc:`../configuration_server/caching_configuration`.)
3030

31-
To use a memcache with Transactional File Locking, you must install the Redis
32-
server and corresponding PHP module. After installing Redis you must enter a
33-
configuration in your ``config.php`` file like this example::
31+
To use a memcache with Transactional File Locking, you must install a key-value store
32+
server like Valkey or Redis. Two cache backends are available for it:
33+
34+
* The Nextcloud ``keyvalue`` cache, which works with Valkey and Redis and does not
35+
require the installation of a PHP module (see :ref:`keyvalue_cache_label`)
36+
* The ``Redis`` cache, which requires the phpredis PHP module
37+
38+
Using the Nextcloud ``KeyValueCache`` cache
39+
-------------------------------------------
40+
41+
The ``KeyValueCache`` cache is built on a library bundled with Nextcloud, so no PHP module
42+
needs to be installed — you only need a running Valkey or Redis compatible server.
43+
Enter a configuration in your ``config.php`` file like this example::
44+
45+
'memcache.locking' => '\OC\Memcache\KeyValueCache',
46+
'memcache.kvstore' => [
47+
'server' => [
48+
'host' => 'localhost',
49+
'port' => 6379,
50+
],
51+
'password' => '', // Optional, if not defined no password will be used.
52+
],
53+
54+
See :ref:`keyvalue_cache_label` for all available options, including Unix sockets, TLS,
55+
Sentinel and cluster setups.
56+
57+
Using the Redis cache with the phpredis PHP module
58+
--------------------------------------------------
59+
60+
To use the ``Redis`` cache instead, you must install the Redis server and corresponding
61+
PHP module. After installing Redis you must enter a configuration in your ``config.php``
62+
file like this example::
3463

3564
'memcache.locking' => '\OC\Memcache\Redis',
3665
'redis' => array(
@@ -40,8 +69,9 @@ configuration in your ``config.php`` file like this example::
4069
'password' => '', // Optional, if not defined no password will be used.
4170
),
4271

43-
.. note:: For enhanced security it is recommended to configure Redis to require
44-
a password. See http://redis.io/topics/security for more information.
72+
.. note:: For enhanced security it is recommended to configure your key-value store
73+
server to require a password. See http://redis.io/topics/security or
74+
https://valkey.io/topics/security/ for more information.
4575

4676
If you want to configure Redis to listen on an Unix socket (which is
4777
recommended if Redis is running on the same system as Nextcloud) use this example

admin_manual/configuration_server/caching_configuration.rst

Lines changed: 169 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ needs. The supported caching backends are:
2929
* `Redis <https://redis.io/open-source/>`__ (4.0.0 and up required);
3030
`Valkey <https://valkey.io/>`__ and `KeyDB <https://docs.keydb.dev/>`__ are expected to work as Redis-compatible backends.
3131

32-
.. note:: Automated/formal testing currently only occurs against Redis Open Source.
32+
.. note::
33+
Automated/formal testing currently only occurs against Redis Open Source using the `redis` PHP extension
34+
and against Valkey using the Nextcloud `keyvalue` cache provider.
35+
KeyDB is expected to work as compatible backends as well, but are not formally tested.
3336

3437
For local and distributed caching, as well as transactional file locking.
3538

@@ -47,7 +50,7 @@ Recommendations based on type of deployment
4750
-------------------------------------------
4851

4952
You may use both a local and a distributed cache. Recommended caches are APCu
50-
and Redis. After installing and enabling your chosen memcache (data cache),
53+
and Redis or Valkey. After installing and enabling your chosen memcache (data cache),
5154
verify that it is active by running :ref:`label-phpinfo`.
5255

5356
.. note:: See specific cache configuration options under the appropriate section further down.
@@ -142,8 +145,170 @@ increase the size. `APCu provides a script
142145
otherwise the `serverinfo app <https://github.com/nextcloud/serverinfo>`_ in
143146
Nextcloud can also show the APCu cache status.
144147

145-
Redis
146-
-----
148+
.. _keyvalue_cache_label:
149+
150+
Redis / Valkey using the Nextcloud ``KeyValueCache``
151+
----------------------------------------------------
152+
153+
.. note::
154+
The KeyValueCache was added with Nextcloud 34.0.2.
155+
156+
The ``KeyValueCache`` cache is a brand-independent cache backend for key-value stores like
157+
`Valkey <https://valkey.io/>`__ or `Redis <https://redis.io/open-source/>`__. These are excellent
158+
modern memcaches to use for distributed caching, and as a key-value store for
159+
:doc:`Transactional File Locking <../configuration_files/files_locking_transactional>` because they
160+
guarantee that cached objects are available for as long as they are needed.
161+
162+
Unlike the ``phpredis`` backend described in the next section, the ``KeyValueCache`` does not require
163+
a PHP extension: it is built on the `predis <https://github.com/predis/predis>`__ library which is
164+
bundled with Nextcloud. You only need to install and run a Valkey or Redis compatible server and
165+
configure it in ``config.php``.
166+
167+
.. note::
168+
When the cache server is not located on the same machine the performance
169+
may be impacted due to network overhead compared to the ``php-redis`` based backend.
170+
In those cases it may be preferable to use the ``php-redis`` backend instead.
171+
172+
On Debian/Ubuntu/Mint, install ``valkey-server`` or ``redis-server``. On CentOS and Fedora,
173+
install ``valkey`` or ``redis``. Depending on your distribution the server might not start
174+
automatically, so you may need to use your service manager to start the server, and to launch it
175+
at boot as a daemon.
176+
177+
You can verify that the daemon is running with ``ps ax``::
178+
179+
ps ax | grep valkey
180+
22203 ? Ssl 0:00 /usr/bin/valkey-server 127.0.0.1:6379
181+
182+
Add the appropriate entries to your ``config.php``, and refresh your Nextcloud admin page.
183+
184+
.. _keyvalue_cache_configuration:
185+
186+
KeyValueCache configuration in Nextcloud (config.php)
187+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188+
189+
For best performance, use the ``KeyValueCache`` for file locking by adding this::
190+
191+
'memcache.locking' => '\OC\Memcache\KeyValueCache',
192+
193+
Additionally, you should use it for the distributed server cache::
194+
195+
'memcache.distributed' => '\OC\Memcache\KeyValueCache',
196+
197+
Furthermore, you could use it for the local cache like so, but it's not recommended (see
198+
warning below)::
199+
200+
'memcache.local' => '\OC\Memcache\KeyValueCache',
201+
202+
.. warning:: Using a key-value store server for local cache on a multi-server setup can cause
203+
issues. Also, even on a single-server setup, APCu (see section above) should be faster.
204+
205+
When using the ``keyvalue`` cache for any of the above cache settings, you also need to specify
206+
the ``memcache.kvstore`` configuration in ``config.php``. Three topologies are supported: a single
207+
server (``server``), a Sentinel managed replication set (``sentinel``), and a server cluster
208+
(``seeds``). Configure exactly one of them.
209+
210+
The following options are available when using a single server (all but ``server`` are
211+
optional)::
212+
213+
'memcache.locking' => '\OC\Memcache\KeyValueCache',
214+
'memcache.distributed' => '\OC\Memcache\KeyValueCache',
215+
'memcache_customprefix' => 'mynextcloudprefix',
216+
'memcache.kvstore' => [
217+
'server' => [
218+
'host' => 'cache-host.example.com',
219+
'port' => 6379,
220+
// Protocol used to connect. One of 'tcp', 'tls' or 'unix'.
221+
// When omitted it is derived from the host (a leading '/' means 'unix').
222+
'protocol' => 'tcp',
223+
],
224+
'user' => 'nextcloud', // only sent when the server uses ACLs
225+
'password' => 'password',
226+
'dbindex' => 0,
227+
'timeout' => 1.5,
228+
'read_timeout' => 1.5,
229+
'persistent' => false, // keep the connection open across requests
230+
],
231+
232+
The same optional parameters are available for the Sentinel and cluster topologies below and are
233+
applied to every node.
234+
235+
For a Sentinel managed replication setup, provide the name of the monitored service and one seed
236+
entry per Sentinel node (each seed uses the same format as the ``server`` entry above)::
237+
238+
'memcache.kvstore' => [
239+
'sentinel' => [
240+
'service' => 'mymaster',
241+
'seeds' => [
242+
['host' => 'sentinel1.example.com', 'port' => 26379],
243+
['host' => 'sentinel2.example.com', 'port' => 26379],
244+
['host' => 'sentinel3.example.com', 'port' => 26379],
245+
],
246+
],
247+
],
248+
249+
For a cluster, provide some or all of the cluster nodes to bootstrap discovery::
250+
251+
'memcache.kvstore' => [
252+
'seeds' => [
253+
['host' => 'cache-cluster.example.com', 'port' => 7000],
254+
['host' => 'cache-cluster.example.com', 'port' => 7001],
255+
],
256+
],
257+
258+
.. note:: Selecting a numbered database with ``dbindex`` is only supported for single servers and
259+
Sentinel setups; clusters always use database 0, the support for numbered databases added with Valkey v9 is pending `upstream <https://github.com/predis/predis/issues/1696>`_.
260+
261+
Connecting over TLS
262+
^^^^^^^^^^^^^^^^^^^
263+
264+
To connect via TCP over TLS, set the ``protocol`` of the server (or of each seed) to ``tls`` and
265+
provide the SSL context (`SSL context options
266+
<https://www.php.net/manual/en/context.ssl.php>`_)::
267+
268+
'memcache.kvstore' => [
269+
'server' => [
270+
'host' => 'cache-host.example.com',
271+
'port' => 6379,
272+
'protocol' => 'tls',
273+
],
274+
'ssl_context' => [
275+
'local_cert' => '/certs/cache.crt',
276+
'local_pk' => '/certs/cache.key',
277+
'cafile' => '/certs/ca.crt',
278+
],
279+
],
280+
281+
Connecting over UNIX socket
282+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
283+
284+
If you want to connect to a server configured to listen on a Unix socket (which is recommended if
285+
the server is running on the same system as Nextcloud), pass the socket path as the host. A
286+
leading slash in the host is automatically detected as a Unix socket, so no ``protocol`` or
287+
``port`` is needed::
288+
289+
'memcache.kvstore' => [
290+
'server' => [
291+
'host' => '/run/valkey/valkey.sock',
292+
],
293+
],
294+
295+
Update the server configuration (for example ``/etc/valkey/valkey.conf``) accordingly: uncomment
296+
the Unix socket options and ensure the socket path matches your Nextcloud configuration.
297+
298+
Be sure to set the right permissions on the socket so that your web server can read and write to
299+
it. For this you typically have to add the web server user to the ``valkey`` (or ``redis``)
300+
group::
301+
302+
usermod -a -G valkey www-data
303+
304+
And modify the ``unixsocketperm`` setting of the server configuration accordingly::
305+
306+
unixsocketperm 770
307+
308+
You might need to restart your web server and the cache server for the changes to take effect.
309+
310+
Redis using the phpredis PHP extension
311+
--------------------------------------
147312

148313
Redis is an excellent modern memcache to use for distributed caching, and
149314
as a key-value store for :doc:`Transactional File Locking

admin_manual/installation/php_configuration.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ performance and reliability. Choose and install your preferred combination of me
111111

112112
.. note::
113113
Memory caching is highly recommended for optimal performance. In most cases, a combination of `APCu` and
114-
`redis` are the best choice for new installations.
114+
`redis` are the best choice for new installations. You can also use the ``KeyValueCache`` cache option
115+
which provides Redis-like functionality without requiring installing a PHP module, see :ref:`keyvalue_cache_configuration`.
115116

116117
See :doc:`../configuration_server/caching_configuration` for configuration details.
117118

0 commit comments

Comments
 (0)