System Design CourseSystem Design Course1

Redis and Memcached Overview



Introduction to Redis and Memcached

Caching is a powerful strategy to speed up applications, reduce latency, and lessen the burden on backend systems like databases. Two of the most popular in-memory caching solutions used in system design are Redis and Memcached.

What Is Caching?

Caching stores frequently accessed data in memory (RAM) so that future requests for the same data can be served faster. Instead of going to a slower data store like a database, the application can quickly get the data from the cache.

Why Use Redis or Memcached?

Both Redis and Memcached offer lightning-fast data retrieval because they keep data in memory. This is crucial in high-scale systems like social media platforms, e-commerce sites, and search engines, where every millisecond matters.

Redis Overview

Redis (REmote DIctionary Server) is an open-source, in-memory data structure store. It can be used as a cache, message broker, and even a lightweight database.

Key Features of Redis

Example: Using Redis to Cache API Responses

Imagine you're building a weather API service that returns the current temperature for a city. This data comes from a third-party service that charges money per call and is also relatively slow (around 2 seconds per request).

To reduce cost and speed up responses, you can use Redis:


  // Pseudocode
  if (redis.has("weather:delhi")) {
      return redis.get("weather:delhi")
  } else {
      data = callWeatherAPI("delhi")
      redis.set("weather:delhi", data, expiry=600)  // Cache for 10 minutes
      return data
  }
  

Question

Why is an expiry time set for the cached value?

Answer

Weather data changes frequently. Setting an expiry (TTL - Time to Live) ensures the cached value is not stale and is periodically refreshed from the source.

Memcached Overview

Memcached is another open-source, in-memory key-value store, optimized for simplicity and speed. It is widely used to cache small chunks of arbitrary data (like database query results, session tokens, etc.).

Key Features of Memcached

Example: Using Memcached to Cache Database Query Results

Suppose you're running an e-commerce site, and every time a user visits the product page, you query your database for product details. As traffic increases, the database load becomes a bottleneck.

With Memcached, you can cache query results:


  // Pseudocode
  if (memcached.has("product:123")) {
      return memcached.get("product:123")
  } else {
      product = db.query("SELECT * FROM products WHERE id = 123")
      memcached.set("product:123", product, expiry=300)  // Cache for 5 minutes
      return product
  }
  

Question

Why use Memcached instead of Redis in this case?

Answer

Memcached is lightweight and faster for simple key-value data. If you don’t need advanced data structures or persistence, Memcached is more memory-efficient and faster for this use case.

Redis vs Memcached: A Comparison

Feature Redis Memcached
Data Structures Supports complex structures Key-Value only
Persistence Yes No
Use Case Advanced caching, message queues Simple cache, session storage
Memory Efficiency Less efficient for large simple values Highly efficient
Multi-threading Single-threaded Multi-threaded

When to Use Redis

When to Use Memcached

Example: Session Storage Comparison

Let's say you're storing user sessions (logged-in user data) temporarily in memory:

Question

Can Redis and Memcached be used together?

Answer

Yes. Some systems use Memcached for fast, lightweight caching, and Redis for more persistent or complex needs like leaderboards or real-time chat systems.

Key Takeaways



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M