Memcached and Redis are both popular in-memory data stores used for caching, but they offer distinct features and performance characteristics that make one a better fit than the other depending on your application’s needs. The most surprising truth is that Redis isn’t just a faster Memcached; it’s a fundamentally different tool with a richer feature set that often makes it the default choice for modern applications, even if Memcached might technically be "faster" for simple key-value lookups.

Let’s see them in action. Imagine you have a web application that needs to serve frequently accessed user profiles. Without a cache, each request would hit your primary database, which can become a bottleneck.

Scenario: Caching User Profiles

Memcached Example:

# Add a user profile to Memcached
echo "set user:101 0 3600 150" | nc localhost 11211
echo '{"name": "Alice", "email": "alice@example.com", "level": 5}' | nc localhost 11211

# Retrieve the user profile from Memcached
echo "get user:101" | nc localhost 11211

Memcached’s interface is simple and text-based. You send commands like set and get, specifying a key, expiration time, and the value.

Redis Example:

# Add a user profile to Redis using HSET (Hash Set)
redis-cli HSET user:101 name "Alice" email "alice@example.com" level 5

# Set an expiration time for the user profile
redis-cli EXPIRE user:101 3600

# Retrieve the user profile from Redis
redis-cli HGETALL user:101

Redis offers a more structured approach with commands like HSET for storing complex data types and HGETALL for retrieving them. It also has built-in support for expirations.

The Mental Model: What Problem Do They Solve?

Both Memcached and Redis solve the problem of slow data retrieval from persistent storage (like databases or disk). By keeping frequently accessed data in RAM, they drastically reduce latency and the load on your primary data sources.

Memcached:

  • Core Idea: A simple, high-performance, distributed memory object caching system. It’s designed for one thing: storing and retrieving arbitrary data blobs (strings) quickly.
  • How it Works: Memcached runs as a single process. When you scale it, you typically run multiple independent Memcached instances on different servers. Clients are responsible for sharding (distributing keys across these instances). It uses a slab allocator to manage memory efficiently, reducing fragmentation.
  • Levers:
    • max_memory: The total amount of RAM allocated to Memcached.
    • Expiration Time (TTL): How long an item stays in the cache before being automatically evicted.
    • Key Naming Conventions: Crucial for managing and invalidating cached items.

Redis:

  • Core Idea: An advanced in-memory data structure store, used as a database, cache, and message broker. It supports a richer set of data types beyond simple strings.
  • How it Works: Redis can run as a single instance or be configured for high availability with replication (master-slave) and sharding (Redis Cluster). It stores data in memory but also offers persistence options (RDB snapshots and AOF logs) to recover data after restarts. Its data structures (lists, sets, sorted sets, hashes, etc.) allow for more sophisticated caching patterns.
  • Levers:
    • Memory Management (maxmemory and maxmemory-policy): You can set a memory limit and define eviction policies (e.g., LRU, LFU, random) when the limit is reached.
    • Data Structure Choice: Using hashes for objects, lists for queues, sets for unique items, etc.
    • Persistence Settings (RDB, AOF): Controls how often data is saved to disk.
    • Replication and Clustering: For high availability and scalability.
    • Lua Scripting: For atomic execution of complex operations on the server.

When to Choose Which: A Deeper Dive

Memcached is ideal for:

  • Pure, simple object caching: When you just need to store and retrieve large amounts of unstructured data (like HTML fragments, API responses, or serialized objects) as quickly as possible.
  • Massive scale with simple distribution: If you’re comfortable with client-side sharding and need to distribute a huge cache across many machines, Memcached’s simplicity can be an advantage.
  • Lowest possible latency for GETs: For straightforward key-value lookups, Memcached can be marginally faster due to its simpler design and lack of complex data structures or persistence overhead.

Redis is ideal for:

  • Complex data structures: Caching lists of items, sets of unique IDs, leaderboards (sorted sets), or session data that benefits from more than just key-value access.
  • Atomic operations: Performing operations like incrementing a counter or adding an element to a list without race conditions.
  • Real-time features: Using Redis as a message broker for pub/sub or building real-time features like chat applications.
  • Data persistence: When you need the cache to survive restarts and don’t want to lose all your cached data.
  • Simplified scaling and HA: Redis Cluster and Sentinel provide more integrated solutions for sharding and high availability compared to Memcached’s client-side approach.

The subtle but crucial difference in how Redis handles data structures means you can perform operations on the cached data directly on the server. For example, instead of fetching a whole user object, modifying a field in your application, and then writing the whole object back, you can use Redis’s HSET to update just a single field atomically: redis-cli HSET user:101 level 6. This reduces network round trips and ensures data consistency.

The next concept you’ll likely explore is implementing robust cache invalidation strategies, which can be surprisingly complex regardless of whether you choose Memcached or Redis.

Want structured learning?

Take the full Memcached course →