Redis, Memcached and cache services
Caches, queues and sessions are much faster in memory than in a database. SrvYard installs the two most common in-memory stores, Redis and Memcached, as ordinary services of a server, and manages them from the Services tab: you can see whether they run, restart them, read their logs and edit their configuration.
This article explains which one to choose, how to install it, how to set a password on Redis, how to point a Laravel site at it and what to do when something goes wrong.
Before you begin
- A server in the Ready state and the permission to manage services.
- Redis is often already there. Servers created as Cache servers, and servers where you ticked a cache service in the server's advanced options during creation, come with it installed. Look at the Services tab before you install anything.
- Which one? Redis stores more than plain values (lists, sets, queues) and can persist them. Laravel can use it for cache, sessions and queues,. Memcached is a simpler key-value cache: it only caches, and everything in it is lost when it restarts. When in doubt, choose Redis.
1. See what runs on the server
Open the server and choose Services.
- Install service opens the list of things SrvYard can install. The button shows once the server has at least one service.
- Each row is a service: its name and unit (here Redis and
redis-server), the Type (Cache), the Version, the Status (Running) and whether it starts At boot. The menu at the end of the row holds the actions of step 4.
2. Install Redis or Memcached
Press Install service.
- The service cards. Pick Redis or Memcached. A service that is already installed is greyed out with the note is already installed on this server. The same list offers Nginx, MySQL, MariaDB, PostgreSQL, Meilisearch and Supervisor.
- Install Memcached (the button names your choice). SrvYard installs the package from the Ubuntu archive, shows the output live and adds the service to the list when it is done. The Version list is Latest from Ubuntu for both.
Both services listen on localhost only. That is the safe default: only programs on the same server can talk to them, and nobody on the internet can.
If you create a new server and tick Redis or Memcached in its advanced options, you do not need this step at all.
3. Protect Redis with a password
Redis on localhost is safe from the internet, but anything running on the server can use it. A password adds a second lock, and it becomes important the moment other servers can reach Redis. Open the server's Settings tab and find the Redis card.
- Password. Shows Set when a password is active, and the sentence Clients must authenticate. The password is stored encrypted. Without one it offers to add a password.
- Change password (or Add password) opens the dialog below.
- Reveal shows the stored password. Viewing it is recorded in the audit log.
- Remove switches the password off again.
- Password. 8 to 128 characters from letters, numbers and the symbols
. _ ~ + = : @ ! % ^ * -. The eye button shows what you typed. - Generate fills the field with a strong random password. Use it unless you have a reason not to.
- Add password or Change password saves it.
Redis restarts for a moment and from then on every client needs the password. Sites keep failing to connect until their REDIS_PASSWORD is updated in the .env. Do it straight after saving, on every site that uses this Redis, and in a quiet moment.
4. Manage the service
Back on the Services tab, the menu at the end of a row offers:
- Start, Stop, Restart and Reload, where the service supports them. Restarting Memcached empties the cache.
- Start at boot or Do not start at boot, to decide whether the service comes back after the server reboots.
- Edit configuration opens
redis.conf(ormemcached.conf). Neither has a configuration checker, so SrvYard restarts the service after saving and puts the previous file back if it does not come up. - View logs shows the latest lines of the service log, with a choice of how many.
- Uninstall asks you to type the service name, and then removes the package and its configuration. Anything that relies on it stops working.
Services are synchronised with the server regularly, so a service you stopped by hand over SSH shows as stopped here too. A cache lives in memory, so keep an eye on the server's memory graph on the Monitoring tab after you install one: a cache that grows without limits will eventually push other programs out of memory.
A Latest activity panel appears at the top of the page while an operation runs, and it shows the output live.
5. Use it from Laravel
For a Laravel site on the same server, set these values on the site's Environment tab and press Save and deploy:
CACHE_STORE=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=the-password-from-step-3
Use REDIS_PASSWORD=null when you have not set a password. For Memcached, set CACHE_STORE=memcached and MEMCACHED_HOST=127.0.0.1. The application also needs the matching PHP extension: Redis and Memcached are both in the extension list of the Runtimes tab (see PHP extensions and php.ini settings).
Use separate Redis databases or key prefixes if several sites share one Redis, so that php artisan cache:clear on one site does not flush another site's data.
6. Reach it from another server
Redis and Memcached listen on localhost, so a site on a different server cannot use them as they are. SrvYard does not open the port for you, because an exposed cache is a serious risk. If you really need a shared cache server, do all three of these on purpose: put both servers on the same provider and region and grant access under Settings > Server network (this creates the firewall rule for one peer only), change bind in redis.conf through Edit configuration, and set a Redis password. Never open 6379 or 11211 to the world.
Troubleshooting
- "Connection refused" from the application. The service is stopped, or the site is on another server. Check the Status column on the Services tab and press Start.
- "NOAUTH Authentication required". Redis has a password and the
.envhas none, or the wrong one. Press Reveal on the Redis card and updateREDIS_PASSWORD. - "Class Redis not found" or "could not find driver". The PHP extension is missing for the site's PHP version. Install it on the Runtimes tab.
- Saving the configuration reports that the service did not come back. The new file was invalid, so SrvYard restored the previous one. Read the log, fix the setting and save again.
- The cache is empty after a restart. That is expected for Memcached, and for Redis when persistence is not configured. Do not store anything in a cache that you cannot rebuild.
- I cannot install the service. The server is not Ready, a package manager is busy, or your role cannot manage services.
Related
- Meilisearch and other search services: another service you can install the same way.
- PHP extensions and php.ini settings: the Redis and Memcached extensions.
- Connect a site to its database: how the
.envpage works. - Create a database and a user: the relational side of the data.