StarDomain

Redis and Memcached Setup

Redis and Memcached Setup

Redis and Memcached are in-memory caching systems that dramatically speed up web applications by storing frequently accessed data in RAM instead of querying the database repeatedly. This guide explains how to enable and configure these caching solutions on your hosting account.

Redis vs Memcached

FeatureRedisMemcached
Data structuresStrings, lists, sets, hashes, sorted setsStrings only
PersistenceOptional disk persistenceNo persistence
ReplicationBuilt-in replication supportNo built-in replication
Memory efficiencyGoodSlightly better for simple key-value
Use caseWordPress, Laravel, complex cachingSimple session/object caching
Recommended forMost applicationsLegacy applications

Tip: For most modern PHP applications and WordPress sites, Redis is the recommended choice due to its richer feature set and better ecosystem support.

Enabling Redis on Shared Hosting

Via cPanel

  1. Log in to cPanel
  2. Navigate to Software > Select PHP Version (or MultiPHP Manager)
  3. Click on Extensions tab
  4. Find and enable the redis extension
  5. Click Save

Via DirectAdmin

  1. Log in to DirectAdmin
  2. Navigate to Account Manager > PHP Settings
  3. Enable the Redis PHP extension for your PHP version
  4. Save changes

Tip: On shared hosting, Redis runs as a shared service. Each account typically gets a dedicated Redis database number or prefix to isolate data. Check with support for your specific configuration.

Enabling Memcached on Shared Hosting

Via cPanel

  1. Log in to cPanel
  2. Navigate to Software > Select PHP Version
  3. Click on Extensions tab
  4. Enable the memcached extension (note: not memcache — they are different extensions)
  5. Click Save

Setting Up Redis on VPS

If you have a VPS with root access:

Install Redis

# AlmaLinux/Rocky
sudo dnf install redis -y
sudo systemctl enable redis
sudo systemctl start redis

# Ubuntu/Debian
sudo apt install redis-server -y
sudo systemctl enable redis-server
sudo systemctl start redis-server

Verify Installation

redis-cli ping

Expected response: PONG

Basic Configuration

Edit /etc/redis/redis.conf (or /etc/redis.conf):

# Bind to localhost only (security)
bind 127.0.0.1

# Set a password
requirepass your-strong-password

# Set maximum memory
maxmemory 256mb

# Eviction policy (remove least recently used keys when full)
maxmemory-policy allkeys-lru

Restart Redis after changes:

sudo systemctl restart redis

Configuring WordPress with Redis

Redis object caching can reduce WordPress database queries by 50-80%.

Step 1: Install a Redis Plugin

  1. In WordPress admin, go to Plugins > Add New
  2. Search for Redis Object Cache by Till Kruss
  3. Install and activate the plugin

Step 2: Add Redis Configuration to wp-config.php

Add these lines above the "That's all" comment:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'your-redis-password');
define('WP_REDIS_DATABASE', 0);
define('WP_REDIS_PREFIX', 'wp_mysite_');

Step 3: Enable Object Cache

  1. Go to Settings > Redis in WordPress admin
  2. Click Enable Object Cache
  3. Verify the status shows "Connected"

Tip: Use a unique WP_REDIS_PREFIX for each WordPress site to prevent cache collisions when multiple sites share the same Redis instance.

Configuring Laravel with Redis

Laravel has built-in Redis support:

Step 1: Install the PHP Redis Extension

Ensure the phpredis extension is installed, or use the predis/predis package:

composer require predis/predis

Step 2: Configure .env

CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=your-redis-password
REDIS_PORT=6379

Monitoring Cache Performance

Redis

Check Redis statistics:

redis-cli info stats

Key metrics to monitor:

  • keyspace_hits / keyspace_misses: Calculate hit rate (aim for >90%)
  • used_memory: Current memory usage
  • connected_clients: Number of active connections

Memcached

echo "stats" | nc localhost 11211

Watch for get_hits vs get_misses to evaluate cache effectiveness.

Troubleshooting

Cannot Connect to Redis

  • Verify Redis is running: systemctl status redis
  • Check if the port is correct (default: 6379)
  • Ensure the password matches in both Redis config and your application
  • On shared hosting, confirm the Redis extension is enabled in PHP

High Memory Usage

  • Set maxmemory to a reasonable limit (128-512 MB for most sites)
  • Use allkeys-lru eviction policy to automatically remove old keys
  • Run redis-cli info memory to check current usage

Cache Not Working (WordPress)

  • Check if wp-content/object-cache.php exists — the plugin creates this file
  • Verify Redis connection in Settings > Redis
  • Ensure no conflicting caching plugins are active

Need help setting up caching? Contact our support team at {{SUPPORT_EMAIL}} or open a ticket at {{SUPPORT_URL}}.