Skip to content
Redis vs Memcached: Caching Solutions Compared

Redis vs Memcached: Caching Solutions Compared

DodaTech 5 min read

Redis offers rich data structures and persistence beyond caching, while Memcached focuses on simplicity and raw throughput for key-value caching — two in-memory data stores with different feature sets.

At a Glance

FeatureRedisMemcached
Data TypesStrings, lists, sets, hashes, sorted sets, streams, bitmapsStrings only
PersistenceRDB snapshots + AOF append-only logNone (ephemeral)
ReplicationBuilt-in master-replicaClient-side (no built-in)
ClusteringRedis Cluster (automatic sharding)No (add nodes manually)
Pub/SubBuilt-in messagingNot supported
Lua ScriptingServer-side Lua scriptsNot supported
TTL HandlingPer-key TTL with eviction policiesPer-key TTL with LRU eviction
Memory Eviction8 policies (allkeys-lru, volatile-ttl, etc.)LRU only (when slab limit reached)

Key Differences

  • Data structures: Redis supports strings, lists, sets, sorted sets, hashes, bitmaps, HyperLogLog, streams, and geospatial indexes. Memcached only supports string key-value pairs. If you need atomic increments, list operations, or sorted sets, Redis is required. Memcached is purely a key-value cache — what you store is what you get.
  • Persistence: Redis can persist data to disk via RDB snapshots (point-in-time) and AOF (append-only file) for full durability. Memcached is entirely ephemeral — data exists only in memory and is lost on restart. Use Redis when cache data must survive restarts (session stores, leaderboards). Use Memcached when cache data is disposable.
  • Replication and clustering: Redis has built-in master-replica replication and Redis Cluster for automatic sharding across nodes. Memcached has no replication — you must handle redundancy at the application level or use a proxy like Mcrouter. Redis Cluster provides automatic failover and resharding.
  • Memory management: Memcached uses slab allocation — memory is divided into slabs of different chunk sizes. This avoids fragmentation but wastes memory if keys vary in size. Redis uses a simple allocator (jemalloc) with object sharing for small integers and strings. Memcached is slightly more memory-efficient for uniform-sized values.
  • Feature scope: Redis is often described as a data structure server — it goes beyond caching to serve as a message broker (Pub/Sub), rate limiter, leaderboard, queue (lists + BLPOP), and real-time analytics store. Memcached does one thing — cache key-value data — and does it well.

When to Choose Redis

Choose Redis when you need more than simple caching. Redis’s data structures power leaderboards (sorted sets), rate limiters (strings + TTL), job queues (lists), session stores (hashes with TTL), pub/sub messaging, real-time analytics (HyperLogLog), and full-text search (RediSearch). Redis’s persistence means your cache can double as a primary database for certain workloads. Redis Cluster provides automatic sharding for horizontal scaling. At DodaTech, Redis powers session storage and real-time analytics across our product suite.

Use Redis for: session stores, rate limiting, leaderboards, job queues, pub/sub messaging, real-time analytics, geospatial queries, and applications where cache data must survive restarts or be shared across application servers.

When to Choose Memcached

Memcached is the right choice for pure key-value caching where simplicity and maximum throughput are the priorities. Its multi-threaded architecture (Memcached uses multiple threads, Redis is single-threaded) can outperform Redis for simple get/set operations on large values. If you already use Redis for data structures and just need a separate cache layer, Memcached complements Redis well. Memcached’s memory overhead per key is lower than Redis’s for simple string values.

Use Memcached for: simple key-value caching (database query results, API responses, rendered page fragments), high-throughput caching of large objects (images, serialized data), and environments where data loss on restart is acceptable.

Side by Side Code Example: Cache Database Query Results

Redis (Node.js with ioredis)

import Redis from "ioredis";

const redis = new Redis();
const cacheKey = "user:profile:42";

// Get from cache or database
async function getUserProfile(userId) {
  const cached = await redis.get(cacheKey);
  if (cached) return JSON.parse(cached);

  // Simulate DB query
  const data = { id: userId, name: "Alice", email: "alice@example.com" };

  // Cache with TTL of 5 minutes
  await redis.setex(cacheKey, 300, JSON.stringify(data));
  return data;
}

const profile = await getUserProfile(42);
console.log(profile);
// Output: { id: 42, name: "Alice", email: "alice@example.com" }

Memcached (Node.js with memcached)

import Memcached from "memcached";

const memcached = new Memcached("localhost:11211");
const cacheKey = "user_profile_42";

// Get from cache or database
function getUserProfile(userId) {
  memcached.get(cacheKey, (err, data) => {
    if (data) return JSON.parse(data);

    // Simulate DB query
    const data = { id: userId, name: "Alice", email: "alice@example.com" };

    // Cache with TTL of 5 minutes
    memcached.set(cacheKey, JSON.stringify(data), 300, () => {});
    return data;
  });
}

getUserProfile(42);
// Same output: { id: 42, name: "Alice", email: "alice@example.com" }

Both examples cache a database query result for 5 minutes. Redis’s setex combines set + expire in one atomic command. Memcached requires separate set with TTL parameter. Redis offers richer data operations (increment, append, set operations) that Memcached cannot match.

Expected Output

# First call:   Database query → cache → return
# Second call:  Cache hit → return (no database query)
# Both return identical data:
# { id: 42, name: "Alice", email: "alice@example.com" }

FAQ

Which is faster, Redis or Memcached?
For simple get/set operations on string values, Memcached is slightly faster due to its multi-threaded architecture. Redis is single-threaded but makes up for it with richer commands and data structures. The performance difference is small — both handle hundreds of thousands of operations per second.
Can I use Redis as a primary database?
Yes, Redis can serve as a primary database for certain workloads (session stores, leaderboards, real-time counters). However, Redis’s in-memory nature means data size is limited by RAM. For applications that need more data than fits in memory, combine Redis caching with a disk-based database like PostgreSQL.
Does Memcached support replication?
No — Memcached has no built-in replication. If a Memcached node fails, all cache data on that node is lost. Redis supports master-replica replication with automatic failover. For production caching, use multiple Memcached nodes with consistent hashing at the client level, or switch to Redis for replication support.
Which is better for session storage?
Redis is the standard for session storage because it supports persistence (sessions survive restarts), TTL-based expiration (automatic cleanup), and atomic operations. Memcached can store sessions but loses them on restart and has no data structure support for session metadata or atomic updates.

Related Comparisons

SQL vs NoSQL — MongoDB vs PostgreSQL — MySQL vs PostgreSQL — REST vs gRPC

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro