Console Login

Memcached or Redis for Caching and Sessions: A 2014 Reference

The problem

A dynamic PHP site (Magento, Drupal or a custom application) that reads everything from MySQL tends to fail the same way under a traffic peak. CPU rises, I/O wait climbs, queries queue behind each other, and visitors get slow or blank pages. An in-memory cache in front of the database is the standard remedy. The choice between Memcached and Redis 2.8 then brings two further failure modes:

  • Sessions vanish when the cache restarts. If PHP sessions live in Memcached, restarting the daemon (for a kernel update, for example) logs out every active user at once.
  • A persistent Redis stalls on slow disks. With the append-only file enabled, a slow or contended disk can delay write commands, so a supposedly fast cache shows latency spikes.

A third, quieter problem is configuration copied between roles. A setup tuned for a disposable cache is wrong for sessions or job queues, and the reverse is also true.

Why it happens

Memcached forgets by design

Memcached keeps key/value items in RAM only. It has no persistence and no replication, and it evicts the least recently used items when its memory limit is reached. That is the right behaviour for a cache and the wrong one for data that must survive a restart. Memcached is multi-threaded, with worker threads that each handle many connections, so one instance can use several cores.

Redis executes commands on one thread

Redis is a data-structure server (strings, hashes, lists, sets, sorted sets). It can persist to disk with RDB snapshots, an append-only file (AOF), or both. It processes commands on a single thread, so one instance uses roughly one core for command execution. Two consequences follow:

  • Anything that blocks that thread delays every client. Examples are fork() for background saves, large commands, swapping, and a write(2) that waits on a busy disk.
  • To use several cores, run several instances.

The AOF fsync detail

With appendfsync everysec, the fsync runs on a separate background thread, so the main thread does not wait for it directly. If a previous fsync is still running, Redis postpones the next write(2) for up to two seconds. If the fsync takes even longer, Redis performs the write anyway and blocks until the disk catches up. Slow, shared or saturated storage therefore shows up as periodic latency spikes. The effect is limited to persistent setups and is worse around AOF rewrites.

Smaller causes

  • The Memcached worker-thread default is 4. Raising it far beyond the number of cores does not help.
  • A persistent Redis whose eviction policy is allkeys-lru may silently drop session or queue data once memory is full.

Source of truth

  • Redis persistence confirms that Redis offers RDB snapshots, an AOF of every write, and the option to combine both in one instance.
  • Diagnosing latency issues confirms the everysec behaviour described above: fsync on a different thread, writes delayed up to two seconds, and a possible stall if the fsync takes too long. It also names fork for background saves as a latency source.
  • Redis configuration confirms that maxmemory with maxmemory-policy allkeys-lru makes Redis behave like Memcached. With that policy, keys do not need a TTL to be evicted.
  • The Memcached wiki, ConfiguringServer confirms the worker-thread model and the default of 4 threads. It advises against raising the value unless the server is under extreme load.

These pages are the current versions. The features used below (RDB, AOF, maxmemory-policy, unix sockets) already exist in Redis 2.6 and 2.8. Check the documentation matching the installed version with redis-server --version.

Fix options

OptionStrengthsWeaknesses
Memcached for everythingSimple, multi-threaded, predictable memory use, little to tuneSessions are lost on every restart; no persistence, replication or data types
One Redis instance for everythingOne service to run; persistence and rich data typesA single policy for both disposable and durable data; eviction can hit sessions; one core
Memcached for cache plus Redis for sessions and queuesEach tool does what it is best atTwo different services to operate and monitor
Two Redis instances, one per roleOne technology, one client library; each role has its own persistence and eviction policy; uses two coresTwo configuration files and two memory budgets

For sessions there is a fifth approach: sticky load balancing with local session files. It avoids a shared store but breaks as soon as the balancer misroutes a user or a node is taken out of rotation.

Why this fix

The chosen layout is two Redis instances, split by the role of the data:

  • Cache instance: no persistence, a fixed maxmemory, and allkeys-lru. It behaves like Memcached and can be lost without consequence.
  • Session/queue instance: AOF with everysec, plus RDB snapshots, and an eviction policy that never removes keys without a TTL (volatile-lru, or noeviction for queues).

The reasons:

  1. Restarts no longer log users out, because sessions are reloaded from disk.
  2. The risky part, AOF I/O, is confined to a small instance that is not in the hot read path of the page cache.
  3. Because Redis runs commands on one thread, the split also spreads the load across two cores.
  4. A single client library (phpredis) covers every use.

Memcached remains the better choice if the need is only a volatile cache of small objects on a machine with many cores and no durability requirement.

Step by step

The commands below are instructions for a CentOS 6 host. No command output is reproduced here; the verification commands at the end show what to check.

1. Install and prepare the kernel

yum install redis
redis-server --version
sysctl -w vm.overcommit_memory=1
sysctl -w vm.swappiness=1
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
echo 'vm.swappiness = 1' >> /etc/sysctl.conf
mkdir -p /var/run/redis /var/lib/redis-cache /var/lib/redis-sessions
chown redis:redis /var/run/redis /var/lib/redis-cache /var/lib/redis-sessions

Overcommit lets the fork() used by background saves succeed on a busy host. A low swappiness value keeps Redis memory out of swap, which would otherwise turn memory-speed operations into disk-speed ones. If the package from the enabled repositories is old, install a newer 2.6 or 2.8 build.

2. Cache instance: /etc/redis/cache.conf

daemonize yes
pidfile /var/run/redis/cache.pid
bind 127.0.0.1
port 0
unixsocket /var/run/redis/cache.sock
unixsocketperm 770
dir /var/lib/redis-cache
save ""
appendonly no
maxmemory 1gb
maxmemory-policy allkeys-lru

The unix socket is disabled by default, so it must be enabled explicitly. port 0 turns off TCP for this instance. The web user must be able to access the socket, for example by adding it to the redis group.

3. Session instance: /etc/redis/sessions.conf

daemonize yes
pidfile /var/run/redis/sessions.pid
bind 127.0.0.1
port 6380
dir /var/lib/redis-sessions
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
maxmemory 512mb
maxmemory-policy volatile-lru

With no-appendfsync-on-rewrite yes, Redis stops calling fsync on the main AOF while a rewrite or snapshot is running. This reduces stalls, but a crash during that window can lose a few tens of seconds of writes. For sessions that is acceptable. Use appendfsync always only if every write must be durable and the disk can take it. Redis has no authentication unless requirepass is set, so keep it bound to the loopback address or firewall it.

4. Start both

redis-server /etc/redis/cache.conf
redis-server /etc/redis/sessions.conf

5. PHP: sessions in Redis

Install the phpredis extension from PECL (pecl install redis), enable it, then set in php.ini:

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6380"

6. PHP: cache-aside with an atomic TTL

The original pattern called set and then expire, which is two round-trips. If the process dies in between, the key never expires. setex does both in one command. The check against false also avoids treating an empty cached value as a miss.

<?php
$redis = new Redis();
$redis->connect('/var/run/redis/cache.sock');

$key  = 'user_profile_1234';
$data = $redis->get($key);

if ($data === false) {
    $stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->execute(array(1234));
    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    $redis->setex($key, 600, serialize($data));
} else {
    $data = unserialize($data);
}

The pure-PHP Predis client is easier to deploy. phpredis, a C extension, has lower per-call overhead, which matters when a page makes many cache calls.

7. Verify

redis-cli -s /var/run/redis/cache.sock ping
redis-cli -p 6380 ping
redis-cli -p 6380 config get appendfsync
redis-cli -p 6380 info persistence
redis-cli -s /var/run/redis/cache.sock info stats
redis-cli -p 6380 --latency

In the info persistence output, aof_enabled:1 shows that the AOF is active. A growing aof_delayed_fsync counter means the disk is not keeping up. In the cache instance's info stats, compare keyspace_hits with keyspace_misses and watch evicted_keys.

To test restart behaviour, log in on the site, run redis-cli -p 6380 shutdown, start the instance again and reload the page. The session should still be valid. A restart of the cache instance should simply produce misses that refill over time.

Review

What the approach gives

  • Disposable and durable data are separated, so each gets its own persistence and eviction policy.
  • Session loss on a restart is avoided, and the AOF cost is confined to a small instance.
  • Memory limits are explicit, so neither role can starve the other.

Limits

  • Redis persistence is not a database transaction log. With everysec, roughly the last second of writes can be lost in a crash, and more if no-appendfsync-on-rewrite is on.
  • Each instance runs commands on one thread. A single very hot cache on a machine with many cores can be limited by that thread, where Memcached would scale across its worker threads. Sharding keys across several Redis instances in the client is the usual answer.
  • Disk latency still matters for the persistent instance. Slow or contended storage produces the stalls described earlier, so watch aof_delayed_fsync and consider RDB-only persistence or appendfsync no if it keeps rising.
  • Memory used by background saves can grow because of copy-on-write. Leave headroom beyond maxmemory.
  • Cached data and sessions may contain personal data. Keep the services on the loopback interface or a private network, and apply the same retention and access rules as for the database.

When to choose another method

  • Choose Memcached when the data is a purely volatile cache of small objects, the host has many cores, and operational simplicity matters most.
  • Choose a single Redis instance for a small site where one policy is enough and sessions are low value.
  • Add replication (Redis supports it; Memcached does not) when losing the session node would be unacceptable and a short warm-standby failover is acceptable.