Redis

From MoodleDocs
Revision as of 07:22, 16 July 2023 by Aadi Bhamidipati (talk | contribs) (Created the page for Redis, describing how to install/configure it for Moodle.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Redis is "The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.", to quote the website and the people who make it. Essentially to call a spade a spade, in my experience it is one of the easiest things you can do to almost immediately boost Moodle Performance, and this is amplified depending on the hardware you're running, especially if it is older and mechanical drives etc. The main reason for the performance increase is due to in-memory caching, especially of Database Queries, meaning these queries do not need to be re-run every time a user requests it (especially useful for things like the home page) and can then be served from cache. If you haven't experimented with in-memory caching via either Redis or Memcached (though I'd personally recommend redis as it's actively developed and has more security features from memory), then I would certainly consider it depending on your site and hardware/spare memory (it doesn't use a lot though, like maybe 10MB).

Plus, there's also a variant for Linux (the original), and a current port for Windows (as of the time of writing, July 2023)!

Note: One thing I'd like to make very clear (hence why I'm putting it here especially), is while Redis does have some basic security features, ideally for security reasons it is preferable that you don't run this exposed to the big wide world, and ideally only have it locally accessible. The ideal would be to run it on the same machine you're running your Moodle Webserver on, though of course this isn't always possible. This is because if a malicious user does happen to gain access to your redis instance, they can very much wreak havoc on the data in there.

Installing Redis

Installing redis is largely like installing any other application on Linux. Sudo apt install redis-server on most Linux Distro's, as per the documentation. On Windows, it's a matter of copying the files and registering it as a service (though there is a batch script to do that included, but like any script, open it up and see what's in it to confirm it'll do what it's saying it will), but then from there in both cases you're almost immediately good to go. The documentation tells you how to use and test the app as well as change things, but DigitalOcean also has an excellent write up on installing Redis and configuring basic security features as well, designed for the shared hosting environment which is probably the documentation I would have a look at, especially if you're not using it in a non-standard configuration.

Once it's running, you're basically almost good to go, now to configure it in Moodle!

Configuring Redis in the Moodle Cache Store

Now head over to your Moodle site, and go to Site Administration -> Plugins -> Caching -> Configuration, and you'll find the Cache Administration page. Depending on your Redis config, especially if you haven't added a password, you may actually find that there's already a green tick under "Ready" to indicate the plugin is ready to be used. If not, or you did configure a password or needed to modify the Redis connection Parameters, this can be done once you've created a Store Instance and is stored as part of the settings of the store (Meta-Store?). If you wanted to use Test Server settings, these can be found under Site Administration -> Plugins -> Caching -> Cache Stores -> Redis, but this isn't applicable for most users.

The next step is to create an instance for Redis - Under Plugins, click on the Add Instance button next to Redis. This will ask you to give it a name (eg. Redis1, so that you don't get confused between 4 different things on the page all called Redis, or "Redis Store" etc, whatever you prefer. This is where you can put in the password, and other things such as key prefix if you have more than one Moodle Instance. The server in most cases will be localhost on the default port of 6379 (127.0.0.1:6379). Save your changes and this will take you back to the Caching Administration page.

You'll now notice there's a Redis Store configured under "Configured Store Instances". If this has a green tick, then it's ready to be used - yay! If not, something has gone wrong. Unfortunately to list things to check would be much too wide, so start from the basics (is Redis running?) and work your way up from there, and recheck configs etc. Once it's got a green tick, you're ready to make things use it.

Making Redis the Default for the Application & Session Caches

So you'll notice there are many mappings for where different caches are stored by default, and mapping these would be time consuming and also not helpful, as any future stores would default to the wrong place. The simplest way to use Redis as a default is to scroll to the bottom of the page, where you'll find "Stores used when no mapping is present". Click on "Edit Mappings", and change the Application and Session Caches to the name of your Redis store. You'll notice the Request Store list doesn't have Redis listed, as it isn't suitable to be a store for the request cache, though this isn't really a problem/bad thing anyway. Save your changes, and you should be good to go! You'll now see under "Store Mappings" next to your Redis store how many mappings are stored to it - for us, this was 76 at the time of writing. The key is that you'll notice that they've all moved away from the file cache and session cache to Redis, which is what we wanted to achieve. If you're worried about data loss etc, the data in these caches is designed to be lost, and that's even explicitly stated on the Moodle Caching page, as well as in the forums, so that isn't an issue. If you're also wondering what the difference between the Application, Session and Request Caches are, this is again covered in the Moodle Caching page, where it talks about the design of the Moodle Universal Cache (MUC) amongst other things.

If you did want to test the performance of the cache and configure it further, this can be done on the server side of Redis, and it's all contained in the docs. But otherwise, you've hopefully given yourself a decent performance boost, and saved the day! Enjoy your slightly faster server :)

See Also

Redis.io zkteco-home/redis-windows How To Install and Secure Redis on Ubuntu 22.04