Redis as a local cache and queue service

★ ★ ★ ★ ★ Not rated

Design and boundaries

Redis is appropriate for cache entries, sessions, queues and short-lived application state. It is not a replacement for MariaDB, backups, or permanent user data.

Never expose Redis to the public internet. Do not open TCP 6379 in UFW, do not bind it to a public address, and do not use an unauthenticated instance across a network.

The intended topology is: the PHP application and Redis run on the same VPS, and the application connects through 127.0.0.1:6379 or the local Unix socket.

Install and start Redis

apt update
apt -y install redis-server
systemctl enable --now redis-server
systemctl --no-pager status redis-server
redis-server --version

Debian manages the service as redis-server.service. Do not run an additional manually downloaded Redis daemon on the same port.

Keep the listener local

Check the active listener before connecting an application:

ss -lntp | grep 6379
grep -E '^(bind|protected-mode|port|unixsocket)' /etc/redis/redis.conf | grep -v '^#' || true

The expected TCP binding is 127.0.0.1:6379 and, where enabled, [::1]:6379. Keep protected-mode yes. If a local Unix socket is preferred, create a dedicated group and grant only the web application’s service account access to it.

Authentication is not a network boundary. Use ACLs or a password for defence in depth, but local-only binding and firewall policy remain mandatory. Store credentials in the application secret store, not in source code or this guide.

Set memory policy and persistence deliberately

Redis uses RAM. Before setting a limit, reserve memory for Apache/PHP-FPM, MariaDB, Elasticsearch and the operating system. A cache-only deployment usually uses an eviction policy; a queue/session deployment often needs persistence as well.

redis-cli INFO memory
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
redis-cli CONFIG GET appendonly
redis-cli CONFIG GET save

Example cache policy — adapt the size to the VPS after observing actual memory use:

cat >/etc/redis/conf.d/99-application-cache.conf <<'EOF'
maxmemory 256mb
maxmemory-policy allkeys-lru
EOF
systemctl restart redis-server
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
Persistence choice. If Redis contains jobs, sessions that cannot be lost, or rate-limit data with business impact, review RDB/AOF persistence and backup implications before relying on it. Do not assume a cache eviction policy preserves data.

Connect PHP applications

Install the matching PHP Redis extension for every PHP-FPM version used by an application:

apt -y install php8.1-redis php8.4-redis php8.5-redis
systemctl restart php8.1-fpm php8.4-fpm php8.5-fpm
php8.4 -m | grep -i redis

Application configuration should use host 127.0.0.1, port 6379, a prefix unique to the application/environment, and a short connection timeout. A prefix prevents unrelated sites from overwriting each other’s cache keys.

Verify from the VPS

redis-cli ping
redis-cli SET redis-guide:test "$(date -Is)" EX 60
redis-cli GET redis-guide:test
redis-cli INFO keyspace
redis-cli ACL LIST

The first command must return PONG. The temporary test key expires after 60 seconds. Remove it manually if required with redis-cli DEL redis-guide:test.

Operations and incident checks

systemctl is-active redis-server
redis-cli INFO memory
redis-cli INFO persistence
redis-cli SLOWLOG GET 10
journalctl -u redis-server -n 100 --no-pager
  • Monitor memory use, evicted keys, rejected connections and persistence failures.
  • Restart Redis only after understanding the impact on cache, sessions and active queues.
  • Back up Redis data only when the selected persistence mode makes that meaningful.
  • Apply Debian security updates and test application behaviour after Redis upgrades.

Result: a private, monitored Redis instance available only to applications on this VPS.

0.0
★ ★ ★ ★ ★
0 reviews
5 ★
0 %
4 ★
0 %
3 ★
0 %
2 ★
0 %
1 ★
0 %

Add a comment

There's nothing here.

No comments have been posted here yet.