Install Redis on Ubuntu 24.04, turn on a password and persistence, and use it as a cache or session store for your app.
- Topic
- Tutorials
- Read time
- 4 min read
- Covers
- redis, cache, database
- Last updated
- August 5, 2026
Redis is an in-memory data store used as a cache, session store, message broker, and rate limiter. It is blisteringly fast and pairs with almost any web app. This guide installs Redis on Ubuntu 24.04, secures it with a password, and configures sensible persistence.
Install Redis
Redis is in Ubuntu's default repositories:
sudo apt update
sudo apt install redis-server -yThe service starts automatically. Test it:
redis-cli pingA reply of PONG means Redis is up.
Set Redis to run under systemd
Open /etc/redis/redis.conf and ensure Redis is supervised by systemd, which makes systemctl status reporting accurate:
supervised systemdSet a password
By default Redis has no authentication. Anyone who can reach the port can read and flush your data — and exposed Redis instances are relentlessly attacked. In /etc/redis/redis.conf, set a strong password:
requirepass a-very-strong-redis-passwordRestart to apply:
sudo systemctl restart redis-serverNow commands require auth:
redis-cli
127.0.0.1:6379> AUTH a-very-strong-redis-password
OK
127.0.0.1:6379> SET greeting "hello from Nxeon"
127.0.0.1:6379> GET greetingKeep it bound to localhost
Confirm Redis is only listening locally — the default and the safe choice:
sudo ss -ltnp | grep 6379You should see 127.0.0.1:6379. The bind 127.0.0.1 ::1 line in redis.conf controls this. If you ever need remote access, require the password, restrict port 6379 tightly in UFW, and prefer an SSH tunnel. Redis also has "protected mode" on by default, which refuses external connections without a password — leave it enabled.
Configure persistence
Redis holds data in RAM but can persist to disk so a restart does not wipe everything. Two mechanisms:
- RDB snapshots: periodic point-in-time dumps. The default
saverules (e.g.save 900 1) already enable this. - AOF (append-only file): logs every write for stronger durability. Enable it in
redis.conf:
appendonly yes
appendfsync everysecappendfsync everysec is the sweet spot — at most one second of data loss on a crash, with negligible performance cost. Restart after changing it. If you use Redis purely as a disposable cache, you can leave persistence off for maximum speed.
Set a memory limit
To stop Redis consuming all RAM on a small VPS, cap it and pick an eviction policy in redis.conf:
maxmemory 256mb
maxmemory-policy allkeys-lruallkeys-lru evicts the least-recently-used keys when full — ideal for a cache. For a session store where you must not lose keys, use noeviction and size memory accordingly.
Connect from your app
Most frameworks take a URL like redis://:password@127.0.0.1:6379/0. In a Node app with ioredis or redis, point it there; in Django or Flask, use it for the cache and session backend. This pairs naturally with a Node app under PM2 or a Flask app behind Gunicorn.
Common Redis patterns
Redis is far more than a key-value cache. A few patterns you will use constantly, all from redis-cli:
Caching with expiry — store a value that self-deletes after a time, ideal for API responses or rendered pages:
SET user:42:profile "{...}" EX 3600Rate limiting — count actions per window using atomic increments:
INCR ratelimit:1.2.3.4
EXPIRE ratelimit:1.2.3.4 60Simple queues — push jobs onto a list and pop them from a worker:
LPUSH jobs "send-email:42"
RPOP jobsSessions — store a session hash keyed by token with an expiry so idle sessions clean themselves up.
Inspect and monitor
Two commands help day to day: redis-cli INFO reports memory, hit rate, and connections, and redis-cli --stat shows a live one-line dashboard. Watch the keyspace hit ratio — a low ratio means your cache is not helping and keys may be expiring too quickly.
FAQ
Do I really need a password if Redis is only on localhost?
It is strongly recommended. A password is defence in depth: if another process or a misconfiguration ever exposes the port, the password is the difference between a shrug and a breach. Setting it costs nothing.
RDB vs AOF — which persistence should I use?
For a cache, either or none. For data you cannot lose, enable AOF with appendfsync everysec; you can run both RDB and AOF together for snapshots plus a durable log. Pure caches often disable persistence entirely for speed.
How much memory does Redis need?
Only enough for the data you keep in it, plus overhead. Set maxmemory to a safe fraction of your VPS RAM and choose an eviction policy so Redis never pushes the system into swap.
Can I use Redis as my primary database?
Usually no — it is best as a cache, session store, or queue alongside a primary database like PostgreSQL. It shines at speed, not as a system of record for complex relational data.
Nxeon VPS plans give you fast NVMe and generous RAM to run Redis right beside your app for instant caching — with full root and free migration help if you are moving a stack across.
Put this guide to work on Cloud VPS hosting. From $8.88 for the first term, then $12.78 a month.
Nxeon editorial · Straight-talking guides from the team that runs the servers.
