Back to Blog

What Is Redis? Installation and Use Cases

What is Redis and what is it for? Redis for cache, sessions and queues, installing it on Ubuntu, secure settings, memory limits, persistence and WordPress.

Nodesty Team5 min readServers
The word Redis with Cache, Sessions and Queues on a dark Nodesty background

Redis is an extremely fast, open-source key-value data store that keeps data in memory (RAM). Because the data lives in memory rather than on disk, reads and writes take microseconds. That's why Redis is most often used as a cache, a session store and a job queue: it stores the results of frequently repeated database queries, holds user sessions and lines up jobs to run in the background.

This article covers what Redis is, when it's useful, installing it on an Ubuntu server, secure settings and using it with WordPress.

How does Redis work?

Redis stores every piece of data under a key. The value can be a simple string, or a data structure such as a list, set, sorted set or hash:

SET user:42:name "Alice"
GET user:42:name
EXPIRE user:42:name 3600

EXPIRE makes a key delete itself automatically after a set time; it's the foundation of caching. Redis runs on a single main thread and processes commands one after another, which makes it both very fast and predictable.

What is Redis used for?

  • Caching: Stores the results of expensive database queries or API responses for a set time. Later requests are answered from memory without going to the database.
  • Sessions: Keeps user sessions somewhere fast that multiple application servers can share.
  • Job queues: Moves jobs like sending email or processing images to the background. Libraries such as Laravel's queues and BullMQ for Node.js use Redis.
  • Rate limiting: Ideal for counting how many requests an IP address sends per minute.
  • Rankings and counters: Sorted sets make game leaderboards and live counters easy.
  • Publish/subscribe (pub/sub): Lets applications and servers send each other instant messages.

Redis doesn't replace a database like MariaDB; it sits in front of it and lightens its load. We cover setting up a database in our How to install MariaDB and MySQL guide.

Installing Redis on Ubuntu and Debian

After connecting to your server with SSH:

sudo apt update
sudo apt install redis-server
sudo systemctl enable --now redis-server

Test the installation:

redis-cli ping

If the response is PONG, Redis is running. If you use Docker, you can also use the official redis image; don't forget to bind its port only to 127.0.0.1. We explain why in our Docker installation guide.

Secure settings

Redis's settings live in the /etc/redis/redis.conf file. Check these lines:

bind 127.0.0.1 -::1
protected-mode yes
requirepass a-long-strong-password
maxmemory 256mb
maxmemory-policy allkeys-lru
  • bind: Makes Redis reachable only from the server itself. Never expose Redis to the internet; open Redis servers are found and taken over by automated scans within minutes.
  • protected-mode: Stops a Redis without a password from accepting outside connections.
  • requirepass: Requires connecting applications to send a password. Choose a long, random password.
  • maxmemory: The maximum memory Redis can use. Without this limit, Redis can eat up the memory of the other applications on the server.
  • maxmemory-policy: What to do when the limit is reached. allkeys-lru deletes the keys that have gone unused the longest and is ideal for caching. If you use Redis for queues or persistent data, choose noeviction so that data isn't silently deleted.

Restart the service after making changes:

sudo systemctl restart redis-server
redis-cli -a 'a-long-strong-password' ping

If an application on another server needs to reach Redis, connect the two servers with a private network like WireGuard instead of opening the port.

Persistence: RDB and AOF

Redis keeps data in memory, but it can also write it to disk so the data can be loaded back when the server restarts:

  • RDB (snapshot): A copy of all the data is written to disk at set intervals. It's fast and easy to back up, but changes made after the last snapshot can be lost.
  • AOF (append-only file): Every write is appended to a log. Far less is lost, but the file is larger.

If you only use Redis as a cache, you can turn persistence off; the cache fills up again on its own when Redis restarts. For sessions and queues, turning on AOF (appendonly yes) is safer.

Connecting to Redis from your application

Most languages have ready-made Redis libraries. For example, in Node.js:

import { createClient } from 'redis';

const redis = createClient({ url: 'redis://:a-long-strong-password@127.0.0.1:6379' });
await redis.connect();

await redis.set('hello', 'world', { EX: 60 });
console.log(await redis.get('hello'));

In Laravel, you just set CACHE_STORE, SESSION_DRIVER and QUEUE_CONNECTION to redis in the .env file and enter the Redis connection details.

Redis with WordPress

WordPress runs the same database queries over and over on every page load. An object cache keeps the results of these queries in Redis:

  1. Install Redis and the PHP Redis extension on the server (the php-redis package on Ubuntu).
  2. Install and activate the Redis Object Cache plugin in WordPress.
  3. If you set a password on Redis, add the line define( 'WP_REDIS_PASSWORD', 'a-long-strong-password' ); to the wp-config.php file.
  4. Turn on the object cache in the plugin's settings.

An object cache makes a difference especially on WooCommerce stores and sites with many logged-in users. We cover other ways to speed up a site in our How to speed up a website guide.

Monitoring Redis

redis-cli -a 'a-long-strong-password' info memory
redis-cli -a 'a-long-strong-password' info stats

info memory shows the memory in use, and info stats shows cache hits (keyspace_hits) and misses (keyspace_misses). If the hit rate is low, cached data may be kept for too short a time, or the wrong data may be getting cached.

Nodesty servers for Redis

Because Redis keeps all of its data in memory, it needs a server with plenty of RAM. Nodesty AMD EPYC 7763 VPS plans, with high RAM options, let you comfortably run Redis, your database and your application on the same server, while AMD Ryzen 9 9950X VPS plans get the best performance out of Redis's single-threaded design with high single-core speed. Both plans include NVMe storage, nShield DDoS protection and free daily backups kept for 7 days.

Frequently Asked Questions

Does Redis replace a database?

Usually not. Redis is a fast key-value store that keeps data in memory, and in most projects it's used alongside a database like MariaDB or PostgreSQL to cache frequently read data. For persistent and relational data, you need a real database.

Is the data in Redis lost if the server restarts?

It depends on the persistence settings. The default RDB snapshots are written to disk at set intervals; changes made after the last snapshot can be lost. With AOF turned on, every write is appended to a log, so far less is lost. If you only use Redis as a cache, turning persistence off is also an option.

How much RAM does Redis need?

Redis keeps all of its data in memory, so what you need depends on the data you store. For a website's object cache, 128–512 MB is usually enough. Limit the maximum memory Redis can use with the maxmemory setting; otherwise there may be no room left for the other applications on the server.

Should I open the Redis port to the internet?

No. Redis servers exposed to the internet are quickly found and taken over by automated scans. Redis should listen only on 127.0.0.1 and be protected with a password. If you need to reach it from another server, use an SSH tunnel or a private network like WireGuard.

What's the difference between Redis and Valkey?

Valkey is an open-source fork of Redis started under the Linux Foundation after Redis changed its license. It's compatible with Redis commands and the Redis protocol; most applications that expect Redis work with Valkey without any changes.

Does using Redis with WordPress speed up the site?

On busy sites with many logged-in users that use the database heavily, it speeds things up noticeably, because the results of repeated database queries come from memory. On simple sites with few visitors that use a page cache, the difference is small.

Nodesty

About the author

Nodesty Team · Infrastructure & Support

The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.

Related articles

Launch your server now.

Build, scale, and conquer without limits on Nodesty's premium infrastructure.

                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                             ············································                                                   
                                                ··································································································..........···························                                     
                                       ················..........................................................................................................................··············                             
                                  ·············.................::::::::::::::::::::::::::::::::::::::::::::::....................:::::::::::::::::::::::::::::::::::::::................···········                        
                             ············................:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::............·········                     
                         ············.................:::::::::::::::::::::::::::::::::::::::::::::::::::------------::::::::::::::::::::::::::::::----------------:::::::::::::::::::::..........········                  
                    ············......................::::::::::::::::::::::::::::::::::::------------------------------------------------------------------------------------:::::::::::::.........········                
                ··········.............................................::::::::::::::::::-------------==========================================-----------------------------------:::::::::::........·······               
             ········..................:.....................................::::::::::--------=======+++++++++++++++++++++++++++++++++++++=========================------------------::::::::::.......·······              
           ······.........::::::::::::::::::::................................:::::::-----=====++++xxxxxx**************************xxxxxxxxx++++++++++++++===================------------::::::::.......·······             
          ·····.......:::::::::::::::::::::::::::.............................:::::----===+++xxx****%%%%&&&&&&#############&&&&&&&%%%%%*******xxxxxxxx++++++++++++++==============----------:::::::......······             
         ····......::::::::-------------:::::::::::..........................::::---===+++xx***%%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@####&&&&&%%%%*******xxxxxxxxx+++++++++++++===========--------::::::......·····             
        ····.....:::::::--------------------:::::::::.......................::::---===++xxx**%%&&&###@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&%%%%%********xxxxxxxxxxx++++++++++++========------::::::.....······            
        ····.....:::::--------------------------::::::::....................::::---===++xx***%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&&%%%%%**********xxxxxxxxxxxxx+++++++++=======------:::::.....·····            
        ····....::::::-------=============---------:::::::::................:::::---===+++xx***%%%&&&#####@@@@@@@@@@@@@@@@@@@@@@@@@#####&&&&&%%%%%%*************xxxxxxxxxxxxxx+++++++++======-----:::::....·····            
         ····....:::::-------================---------:::::::::...............:::::----===++++xxx***%%%%&&&&&&#################&&&&&&%%%%%%%*******************xxxxxxxxxxxxxxxxx++++++++======----:::::....·····            
         ·····....:::::--------=================---------::::::::::..............:::::::-----====+++++xxxxxx**************************xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx++++++++=====-----::::....·····            
          ·····.....:::::--------=================----------:::::::::::.................:::::::::-------=============++++++++++++++++++++++++++++++++++++xxxxxxxxxxxxxxxxxxxxxxx++++++++======-----::::....·····            
            ·····.....:::::::----------===========-------------::::::::::::.........................::::::::::::::::::------------------=============+++++++++++++++++++++++++++++++++=======-----::::.....·····            
              ······......:::::::---------------------------------:::::::::::::........................................:::::::::::::::-----------============+++++++++++++++++++++========------:::::.....·····             
                 ······........:::::::::---------------------------:::::::::::::::::...........................................:::::::::::::------------=============================--------::::::.....······              
                    ·········.........:::::::::::::::-------------::::::::::::::::::::::::.......................................::::::::::::::::--------------------------------------::::::::......·······                
                          ··········............::::::::::::::::::::::::::::::::::::::::::::::::::................................::::::::::::::::::::::::::-------------:::::::::::::::.........········                   
                                  ············..................:::::::::::::::::::::::::::::::::::::::::::.........................:::::::::::::::::::::::::::::::::::::................···········                        
                                             ················.............................................................................................................·················                                 
                                                           ······················................................................................···························                                                
                                                                               ·········································································