Why Redis Caching on Ubuntu

Redis is an in-memory key-value store that speeds up applications by serving hot data from RAM instead of a relational database. On an Ubuntu 22.04 or 24.04 VPS it installs in minutes, consumes a modest memory footprint, and integrates cleanly with every major web framework. This guide walks through installing Redis, securing it for single-server use, and wiring it into common application stacks.

Install Redis from the Ubuntu Repositories

Ubuntu LTS ships a recent Redis build in its default archive. Update the package index and install the server package:

sudo apt update
sudo apt install redis-server -y

Check the running version and confirm the service is active:

redis-server --version
sudo systemctl status redis-server

By default Redis binds to 127.0.0.1:6379 and starts automatically at boot. Verify basic connectivity with the built-in CLI:

redis-cli ping
# Expected response: PONG

Configure Redis for Security

The Redis configuration file lives at /etc/redis/redis.conf. Open it and apply three important settings before exposing the service to any application.

Bind address: keep Redis on localhost unless you need remote access. If a remote app server connects, bind only to the private network interface.

bind 127.0.0.1 ::1
protected-mode yes

Password authentication: set a strong requirepass value so clients must authenticate before issuing commands.

requirepass YourLongRandomPasswordHere

Supervised mode: align Redis with systemd so restarts and status checks work correctly on Ubuntu.

supervised systemd

Apply the changes:

sudo systemctl restart redis-server

Set Memory Limits and Eviction Policy

Without a memory cap, Redis can consume all available RAM and trigger the Linux OOM killer. Define a cap that matches your VPS size and choose an eviction policy for cache workloads.

maxmemory 512mb
maxmemory-policy allkeys-lru

The allkeys-lru policy evicts the least recently used keys when the cap is reached, which is ideal for pure caching. If Redis also stores session data you cannot lose, use volatile-lru so only keys with a TTL are evicted.

Persistence: RDB vs AOF

Redis offers two persistence mechanisms. RDB snapshots the dataset at intervals; AOF logs every write. For most caches, RDB is sufficient because the data can be regenerated from the primary database. For session stores or small datasets where losing a few seconds of writes is unacceptable, enable AOF as well.

save 900 1
save 300 10
appendonly yes
appendfsync everysec

Restart Redis after changing persistence settings. Keep an eye on disk I/O if you enable AOF on a small VPS — pair it with regular offsite backups of the /var/lib/redis/ directory.

Connect Redis to Your Application

Most frameworks expose a Redis driver that needs a URL and password. A few quick examples:

PHP (Laravel) .env:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=YourLongRandomPasswordHere
REDIS_PORT=6379

Node.js with ioredis:

const Redis = require('ioredis');
const redis = new Redis({ host: '127.0.0.1', port: 6379, password: 'YourLongRandomPasswordHere' });

Django with django-redis:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://:YourLongRandomPasswordHere@127.0.0.1:6379/1",
    }
}

Monitor and Test the Cache

Redis exposes rich monitoring through the CLI. Watch live commands with redis-cli monitor, inspect memory with redis-cli info memory, and check hit rates with redis-cli info stats. A healthy cache typically shows a keyspace hit ratio above 90 percent.

If you run multiple application servers, move Redis to its own node on the private network and firewall port 6379 so only your app servers can reach it. For a refresher on locking down the host, see our guide on Ubuntu VPS security hardening and our Ubuntu VPS setup guide.

Running a busy cache in production? MassiveGRID's Cloud VPS offers NVMe storage, root access, and private networking across four data centers — ideal for Redis-backed applications. Contact our team to scope the right plan for your workload.

Published by MassiveGRID, your trusted Linux VPS hosting partner. Explore our Cloud VPS plans for root-access Ubuntu hosting.