Home Knowledge base Skyline Cloud How to Use Redis Caching for Faster Websites KNOWLEDGE BASE

How to Use Redis Caching for Faster Websites

A practical, step-by-step guide to installing, securing, and configuring Redis as an object cache to dramatically reduce database load and speed up your website — with WordPress, PHP, and benchmark verification examples.

Why Redis Speeds Up Websites

Most dynamic websites — WordPress, Laravel, Magento, custom PHP — rebuild the same data on every request: menus, options, user sessions, query results. Each rebuild hits your database. Under traffic, the database becomes the bottleneck and pages slow down.

Redis is an in-memory data store. Instead of asking the database the same question repeatedly, your application asks Redis, which answers from RAM in well under a millisecond. The result is fewer database queries, lower CPU usage, and faster page loads — especially on busy sites.

This tutorial covers installing Redis, securing it, tuning it for caching, and wiring it into a WordPress or PHP application, then verifying the improvement. The steps apply to any Linux server, including a Skyline Cloud VPS or cloud server hosted in-Kingdom.

Prerequisites

  • A Linux server (Ubuntu 22.04/24.04 or similar) with root or sudo access.
  • A web application that supports an object/cache backend (WordPress, Laravel, etc.).
  • The PHP redis extension if your app is PHP-based.

Step 1 — Install Redis

On Debian/Ubuntu:

sudo apt update
sudo apt install redis-server -y

Confirm it is running:

sudo systemctl enable --now redis-server
redis-cli ping

You should see PONG. Check the version (use Redis 6 or newer so you get ACLs and TLS):

redis-server --version

Step 2 — Secure Redis

By default Redis listens only on localhost, which is correct for a single-server setup where the web app and Redis run on the same machine. Verify this in /etc/redis/redis.conf:

bind 127.0.0.1 -::1
protected-mode yes

Always set a password, even on localhost, as defence in depth. Generate a strong one and add it to the config:

requirepass YOUR_STRONG_PASSWORD_HERE

If — and only if — Redis must be reached from another server, do not simply open the bind address. Instead enable TLS and a firewall allow-list, or keep Redis private and connect over the internal network. Exposing an unauthenticated Redis to the public internet is one of the most commonly exploited misconfigurations.

Restart to apply:

sudo systemctl restart redis-server

Test authentication:

redis-cli -a 'YOUR_STRONG_PASSWORD_HERE' ping

Step 3 — Tune Redis for Caching

A cache should never run your server out of memory. Set a memory ceiling and an eviction policy so Redis discards old entries instead of crashing. Edit /etc/redis/redis.conf:

maxmemory 256mb
maxmemory-policy allkeys-lru
  • maxmemory caps how much RAM Redis may use. A common guideline is 60–75% of the RAM you can spare for caching; start with 256mb on a small site and raise it as needed.
  • allkeys-lru evicts the least-recently-used keys when the limit is reached. This is the right policy for a pure cache where every key is disposable. (For session stores where you only want keys with a TTL evicted, use volatile-lru instead.)

Restart Redis again after editing:

sudo systemctl restart redis-server

Step 4 — Connect WordPress to Redis

WordPress is the most common case. First install the PHP extension:

sudo apt install php-redis -y
sudo systemctl restart php8.3-fpm   # match your PHP version

Add your connection settings to wp-config.php, above the /* That's all, stop editing! */ line:

define( 'WP_REDIS_HOST', '127.0.0.1' );
define( 'WP_REDIS_PORT', 6379 );
define( 'WP_REDIS_PASSWORD', 'YOUR_STRONG_PASSWORD_HERE' );
define( 'WP_REDIS_PREFIX', 'site1:' );
define( 'WP_REDIS_DATABASE', 0 );

The WP_REDIS_PREFIX matters when several sites share one Redis instance — a unique prefix per site prevents cache collisions.

Install and enable the Redis Object Cache plugin. From the dashboard, go to Plugins → Add New, install Redis Object Cache, activate it, then open Settings → Redis and click Enable Object Cache. Or, with WP-CLI:

wp plugin install redis-cache --activate
wp redis enable

Confirm the status:

wp redis status

It should report Status: Connected and Drop-in: Valid.

Step 5 — Connect a Plain PHP App

If you are not using WordPress, the phpredis extension gives you a direct client. A minimal cache-aside pattern:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('YOUR_STRONG_PASSWORD_HERE');

$key = 'products:featured';
$cached = $redis->get($key);

if ($cached === false) {
    $data = expensive_db_query();          // your DB call
    $redis->setex($key, 300, serialize($data)); // cache for 300s
} else {
    $data = unserialize($cached);
}

The pattern is always the same: check the cache, return it on a hit, and on a miss compute the value, store it with a sensible TTL, then return it. Frameworks like Laravel make this even simpler — set CACHE_STORE=redis (and SESSION_DRIVER=redis) in .env.

Step 6 — Verify the Improvement

Watch live cache activity:

redis-cli -a 'YOUR_STRONG_PASSWORD_HERE' monitor

Check the hit ratio — a healthy cache shows far more hits than misses:

redis-cli -a 'YOUR_STRONG_PASSWORD_HERE' info stats | grep keyspace

Benchmark a page before and after enabling Redis with ab (Apache Bench) or curl:

ab -n 200 -c 10 https://your-site.example/

Compare the mean response time across the two runs. On a database-heavy WordPress homepage you will typically see a meaningful drop in time-to-first-byte and lower MySQL load.

Setting Recommended value Purpose
bind 127.0.0.1 -::1 Local-only access
requirepass strong secret Authentication
maxmemory 256mb (then tune) Prevent OOM
maxmemory-policy allkeys-lru Evict old cache keys
WP_REDIS_PREFIX unique per site Avoid collisions

Common Pitfalls

  • No memory limit. Without maxmemory, Redis can consume all RAM and crash the server. Always cap it.
  • Cache not actually active. Installing the plugin is not enough; you must enable the drop-in (wp redis enable) and confirm Connected.
  • Stale content. Aggressive caching can serve outdated pages. Use sensible TTLs and flush on deploy (wp redis flush or FLUSHDB).
  • Exposed Redis. Never bind Redis to a public IP without TLS and authentication.

Run It on In-Kingdom Cloud

Redis pairs perfectly with a properly sized server. On Skyline Cloud you can run Redis on a VPS or cloud server hosted inside Saudi Arabia, keeping your data and caching layer compliant with PDPL and NCA requirements, with local Arabic support. Pair it with managed business email for a complete stack, and explore more performance guides in our Saudi web hosting hub.

Ready to deploy a faster site? Create your Skyline Cloud account and spin up an in-Kingdom server in minutes.

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Skyline Cloud for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.