Redis Cache

Fastline

New Member
Hello Xon,

Hope you're doing well. So, recently, i had upgraded the server and running latest XF and while i upgraded the PHP to 8.3, i remembered about caching. A couple of years back, i talked to you regarding the caching options and i was using Memcache since then. However, this time, i have installed the Redis Cache and how the site works now, it is wonders!

Although due to lack of documentation and new to the caching thingy, i've a few question:

- Should i use compression with Redis? I'm aware or zlib, brotli, etc.

Here's my .htaccess and the site is working fine. But i'm not sure if i should make any kind of changes in the htaccess after installing the Redis Cache:
Code:
#    Mod_security can interfere with uploading of content such as attachments. If you
#    cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#    SecFilterEngine Off
#    SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>
    RewriteEngine On

    #    If you are having problems with the rewrite rules, remove the "#" from the
    #    line that begins "RewriteBase" below. You will also have to change the path
    #    of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    #    This line may be needed to workaround HTTP Basic auth issues when using PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>

<IfModule mod_deflate.c>
    # Compress HTML, CSS, JavaScript, Text, XML and fonts
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-otf
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
</IfModule>

<ifModule mod_expires.c>
    ExpiresActive On
    ############################################
    ## Add default Expires header
    ## http://developer.yahoo.com/performance/rules.html#expires
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        ExpiresDefault "access plus 1 year"
    </FilesMatch>
</ifModule>

  • Should i configure and use Guest Page caching?
  • Should i configure and use CSS caching?
  • Should i configure and use Session caching?

On the forums, i saw several configurations for the Redis and i'm interested to know about a few of the parameters listed below:
'timeout' => 2.5,
'persistent' => null,
'force_standalone' => false,
'connect_retries' => 1,
'read_timeout' => null,
'compress_data' => 1,
'lifetimelimit' => 2592000,
'compress_threshold' => 20480,
'compression_lib' => null, // dynamically select first of; snappy,lzf,l4z,gzip IF EMPTY/null


Regarding the compress_data parameter, some people have configured it as 6 (mostly) and 2. What these value mean exactly?

As always, Thank You for your excellent addons!
 
The defaults should give reasonable performance, and by default it will use compression with a smaller compression threshold. I'ld recommend the defaults (ie without much customization of the library options) unless you need changes.

Session caching is generally an easy win, and setting up a cache will automatically enable CSS caching.

Guest page caching isn't required for most sites.
 
The defaults should give reasonable performance, and by default it will use compression with a smaller compression threshold. I'ld recommend the defaults (ie without much customization of the library options) unless you need changes.
Okay, i see. Yes, the site is faster now for sure. But i'm not sure if it was due to the XF upgrade to the latest version or the Redis implementation.

Session caching is generally an easy win, and setting up a cache will automatically enable CSS caching.
So, this can be achieved with the default Redis cache configuration or will i have to set it up? Currently, here is how the Redis cache configuration looks on the server:
Code:
$config['cache']['enabled'] = true;
$config['cache']['sessions'] = true;
$config['cache']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
    'serializer' => 'igbinary', // to disable set to 'php'
    'use_lua' => true,
    'compress_data' => 6,
    'lifetimelimit' => 2592000,
    'compress_threshold' => 20480,
];

Is it good, as recommended or do i need any sort of modification? Also, do i need to add $config['css']['enabled'] = true; parameter in the config?

Guest page caching isn't required for most sites.
Oh, i see. So, can you tell me for what kind of sites it is required? Just curious

Next to this, do i need to make any sort of changes in my .htaccess file i posted above? If you could check it, i would be very thankful to you :)

And if you could explain what these parameters mean and whether i should add them up, it would be really good:

'persistent' => null, and 'compress_data' => 1,

Regarding the compress_data parameter, some people have configured it as 6 (mostly) and 2. What these value mean exactly?

Lastly, is there anyway to check if Redis is actually caching? From the XF cache manual, it says: When a page is served from the cache, a X-XF-Cache-Status: HIT header will be present in the response.

How can i check this status?
 
I'ld recommend this:
PHP:
$config['cache']['enabled'] = true;
$config['cache']['sessions'] = true;
$config['cache']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
];
  • igbinary / persistent/compress_data/use_lua are being set to the default values, so you don't need those
    • igbinary defaults to true if igbinary is installed
    • use_lua is not longer disablable.
  • compress_threshold the default is better than 20k as it will more aggressively compress where possible which gives an IOPs savings
  • lifetimelimit this option doesn't do anything.
Is it good, as recommended or do i need any sort of modification? Also, do i need to add $config['css']['enabled'] = true; parameter in the config?
This doesn't do anything in XF2+


And if you could explain what these parameters mean and whether i should add them up, it would be really good:

'persistent' => null, and 'compress_data' => 1,

Regarding the compress_data parameter, some people have configured it as 6 (mostly) and 2. What these value mean exactly?
persistent is used for "persistent connections" which I strongly do not recommend.

As for compress_data it's value depends on the compressor backend (really only gzip uses it) but generally it is 0 or 1 which matters. Again, I recommend the defaults.

Lastly, is there anyway to check if Redis is actually caching? From the XF cache manual, it says: When a page is served from the cache, a X-XF-Cache-Status: HIT header will be present in the response.
You can look in the admincp dashboard, the add-on will display various information about redis's state.
 
I'ld recommend this:
Thank you, will put that in.

igbinary / persistent/compress_data/use_lua are being set to the default values, so you don't need those
So, if a parameter is not defined, it will automatically use the default values, yeah?

compress_threshold the default is better than 20k as it will more aggressively compress where possible which gives an IOPs savings
Gotcha!

lifetimelimit this option doesn't do anything.
I got that from your addon in the config examples :)

This doesn't do anything in XF2+
Gotcha!

persistent is used for "persistent connections" which I strongly do not recommend.
Oh, okay okay. I think when i had it enabled, i was having lfd reporting on an hourly basis stating excessive usage on the server.

As for compress_data it's value depends on the compressor backend (really only gzip uses it) but generally it is 0 or 1 which matters. Again, I recommend the defaults.
Okay cool. So, will i have to define it with a value of 1 or you recommend me removing the compress_data line?

You can look in the admincp dashboard, the add-on will display various information about redis's state.
Oh, those are available in the admin dashboard and here is how it looks:
Screen Shot 2024-10-03 at 12.43.10 PM.png


The forum is currently closed so i guess that's why the connected clients are being reported to 1.

Regarding lua, its installed but not the PHP extension as whenever i try to install it, i get a weird error.

Code:
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to
 update
downloading lua-2.0.7.tgz ...
Starting to download lua-2.0.7.tgz (15,107 bytes)
.....done: 15,107 bytes
6 source files, building
running: phpize
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
building in /root/tmp/pear/pear-build-rootjGuXbh/lua-2.0.7
running: /root/tmp/pear/lua/configure --with-php-config=/opt/cpanel/ea-php83/root/usr/bin/php-config

checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for pkg-config... /bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /opt/cpanel/ea-php83/root/usr
checking for PHP includes... -I/opt/cpanel/ea-php83/root/usr/include/php -I/opt/cpanel/ea-php83/root
/usr/include/php/main -I/opt/cpanel/ea-php83/root/usr/include/php/TSRM -I/opt/cpanel/ea-php83/root/u
sr/include/php/Zend -I/opt/cpanel/ea-php83/root/usr/include/php/ext -I/opt/cpanel/ea-php83/root/usr/
include/php/ext/date/lib
checking for PHP extension directory... /opt/cpanel/ea-php83/root/usr/lib64/php/modules
checking for PHP installed headers prefix... /opt/cpanel/ea-php83/root/usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... gawk
checking for lua support... yes, shared
checking to specify a custom lua version... yes, shared
checking to specify a custom lua version... yes, shared
checking for lua in default path... not found
configure: error: Please reinstall the lua distribution - lua.h should be in /include/
ERROR: `/root/tmp/pear/lua/configure --with-php-config=/opt/cpanel/ea-php83/root/usr/bin/php-config'
 failed

Also, is there anyway to flush the cache for Redis so that it can rebuild the cache from scratch?

Also, i tried to check the HTTP header in the browser and i don't see X-XF-Cache-Status: HIT there. So, is it that Redis is not caching properly? Maybe the problem is coming from the .htaccess file? To ensure there's nothing wrong, could you please verify it for me?

Thank you for all your help!
 
Last edited:
Regarding lua, its installed but not the PHP extension as whenever i try to install it, i get a weird error.
You don't need to install lua, it is bundled into redis.

As for .htaccess, I don't use apache so I can't help you much there.

Also, is there anyway to flush the cache for Redis so that it can rebuild the cache from scratch?
You can run the CLI command:
Code:
redis-cli flushall

Also, i tried to check the HTTP header in the browser and i don't see X-XF-Cache-Status: HIT there. So, is it that Redis is not caching properly? Maybe the problem is coming from the .htaccess file? To ensure there's nothing wrong, could you please verify it for me?
That is only for when the page cache is being used.

If you've configured a caching backend and redis isn't working the entire site would error.
 
You don't need to install lua, it is bundled into redis.
OMG!

But i don't see Use lua = Yes in Redis information in the AdminCP.

As for .htaccess, I don't use apache so I can't help you much there.
Ah ok. I understand!

You can run the CLI command:
Cool. I read that command somewhere but was not sure if it was the right one. Thanks for confirming that!

BTW, will Redis automatically flush the old content/cache and build new ones whenever required? I'm asking this cause when there is changes in the page or the content itself, it should reflect immediately.

That is only for when the page cache is being used.
Oh, i see. So, the code you provided me:
Code:
$config['cache']['enabled'] = true;
$config['cache']['sessions'] = true;
$config['cache']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
];
Seems like it doesn't include page cache? Should one use it? If yes, how can i configure it in Redis?

Also, when using the page caching, will it cache the static content or dynamic as well?

If you've configured a caching backend and redis isn't working the entire site would error.
Interesting. Makes sense.


EDIT:

So, i used the following for page cache but still i don't see X-XF-Cache-Status: HIT

Code:
$config['pageCache']['enabled'] = true;
$config['pageCache']['lifetime'] = 900;
$config['pageCache']['recordSessionActivity'] = true;
$config['cache']['context']['page']['namespace'] = 'xfaredispagecache';
$config['cache']['context']['page']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['context']['page']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
    'serializer' => 'igbinary', // to disable set to 'php'
    'use_lua' => true,
    'compress_data' => 1,
];

EDIT:

So, @Xon , I logged into the Admin CP and here's something new which i saw after making the above changes:
Screen Shot 2024-10-04 at 7.38.30 PM.png

Does this mean the Redis Page cache is enabled and configured properly and working as expected? If so, i'm not sure whether i should use it or not. Please help!

Also, despite the Redis Page information in the Admin CP, i still don't see X-XF-Cache-Status: HIT. Any idea what could be wrong?

EDIT:

BTW, i tried to compare both the information in the Admin CP and here's what i see. The information seems to be identical. Do you think i should still use the page cache?
Screen Shot 2024-10-04 at 9.12.39 PM.png


EDIT:
So, i was reading about caching with XF and i came to know about KeyDB which is a replacement for Redis. Should i use that? Would it provide better caching than Redis?

Secondly, i came to know about PHP OPcache. Do you think it would be better to install and configure that as well?

EDIT:
So, i installed ZendOpache and configured it and now the JIT is on and website speed is so much crazy!
 
Last edited:
BTW, will Redis automatically flush the old content/cache and build new ones whenever required? I'm asking this cause when there is changes in the page or the content itself, it should reflect immediately.
In general yes, there isn't really any need to manually flush it.

Also, despite the Redis Page information in the Admin CP, i still don't see X-XF-Cache-Status: HIT. Any idea what could be wrong?
With the default config the page cache only applies for guests.

Secondly, i came to know about PHP OPcache. Do you think it would be better to install and configure that as well?

EDIT:
So, i installed ZendOpache and configured it and now the JIT is on and website speed is so much crazy!
You absolutely want the opcache installed. This will be a massive performance difference.

I have no idea why the opcache isn't shipped with the default install setup for php. It is an insane footgun to not have it running.
 
In general yes, there isn't really any need to manually flush it.
Great!

With the default config the page cache only applies for guests.
Okay, so it means, only guests users will see the X-XF-Cache-Status: HIT status, yeah?

You absolutely want the opcache installed. This will be a massive performance difference.
Actually, Opcache was already installed but not well tuned. When i tuned it up, its way faster than before. I mean it got fast after 2.3 upgrade, then with Redis and your addon, the site was more faster and now with Opcache tuned+JIT, its butter! I'm totally loving the speed!

I have no idea why the opcache isn't shipped with the default install setup for php. It is an insane footgun to not have it running.
Yeah, it really brings massive performance and i can witness it ;)

Coming to the main question,

So, the code you provided me:
Code:
$config['cache']['enabled'] = true;
$config['cache']['sessions'] = true;
$config['cache']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
];

As you stated above, it includes page cache, but for guest users only, yeah? Is that sufficient or i should i implement the code below for page cache?

Code:
$config['pageCache']['enabled'] = true;
$config['pageCache']['lifetime'] = 900;
$config['pageCache']['recordSessionActivity'] = true;
$config['cache']['context']['page']['namespace'] = 'xfaredispagecache';
$config['cache']['context']['page']['provider'] = 'SV\RedisCache\Redis';
$config['cache']['context']['page']['config'] = [
    'server' => '127.0.0.1',
    'port' => 6379,
    'serializer' => 'igbinary', // to disable set to 'php'
    'use_lua' => true,
    'compress_data' => 1,
];

When i used this code for page cache, then only i was able to see the Redis Information - page in Admin CP. Can you please check and let me know if its good or if any change is required and whether i should even use it or not?

Also, when using the page caching, will it cache the static content or dynamic as well?

BTW, i tried to compare both the information in the Admin CP and here's what i see. The information seems to be identical. Do you think i should still use the page cache?
Screen Shot 2024-10-04 at 9.12.39 PM.png


Also, the second Redis information (Redis Information - page), will it be only shown when the page caching is enabled? Or the page caching is already enabled by default but is not displayed in the Redis in Admin CP?

Next to this, i was reading about caching with XF and i came to know about KeyDB which is a replacement for Redis. Should i use that? Would it provide better caching than Redis?

Lastly, i have read about Registration issues with caching enabled. Is that the case?

Thank you so much again for helping me with the issues and questions!
 
Last edited:
@Xon

I've got a couple of questions. If you can answer them, it would be very helpful for me.

1. Should i use Gzip or Brotli on the server side for the HTTP compression? I guess only these two are the widely adopted methods, yeah?
2. Should i use Gzip or Brotli or zstd as a compression_lib for Redis Cache?
3. I should not use Zlib compression on the server side when Brotli as it requires Gzip header, right?

Also, i've an interesting question regarding caching. Although the Redis is setup and working fine, do i need to configure cache for the static files on the server side? Or Redis will do it on its own?
 
The page cache questions are really something to ask on xenforo.com's support forums, and it outside the support I offer for my redis caching add-on.

XenForo doesn't support Brotli for server-side HTTP compression, nor is it supported as a compression type for redis cache.

Also, the second Redis information (Redis Information - page), will it be only shown when the page caching is enabled? Or the page caching is already enabled by default but is not displayed in the Redis in Admin CP?
It will only display the statistics information if that section is enabled/active.

Next to this, i was reading about caching with XF and i came to know about KeyDB which is a replacement for Redis. Should i use that? Would it provide better caching than Redis?
This is something you would need to evaluate for yourself. I haven't used KeyDB, or any of the other drop-in redis replacements.
 

Similar threads

Users who are viewing this thread

Back
Top