Memcached Interview Questions and Answers

This section covers frequently asked Memcached interview questions, focusing on its architecture, usage, and common commands.

What is Memcached?

Memcached is a free and open-source, high-performance, distributed memory object caching system. It stores data in RAM to speed up database-driven websites and applications by reducing database load. It's commonly used to cache frequently accessed data.

Memcached's Creator and Programming Language

Memcached was created by Brad Fitzpatrick in 2003 (initially for LiveJournal) and is written in C.

How Memcached Works

  1. A client requests data.
  2. Memcached checks if the data is in its cache (using a unique key).
  3. If found, the data is returned.
  4. If not found, the application fetches the data from the database and stores it in Memcached for future use.

Memcached supports basic operations like get, set, add, replace, append, prepend, and delete.

Memcached's Launch Date

The first version of Memcached was released on May 22, 2003.

Uses of Memcached

Memcached is used to cache frequently accessed data to improve the performance of web applications. Common use cases include:

  • Caching user profiles on social networking sites.
  • Caching website pages (HTML).
  • Caching session data in e-commerce applications.
  • Caching database query results.
  • Caching game state data.
  • Caching data for ad targeting.

Advantages of Memcached

  • Easy installation.
  • API support for many languages (Java, PHP, C/C++, etc.).
  • Performance improvement through caching.
  • Reduced database load.
  • Simple data manipulation (set, get, delete).

Limitations of Memcached

  • Data is not persistently stored (volatile cache).
  • Not a database; it's a caching layer.
  • Cannot efficiently cache very large objects.
  • Not inherently fault-tolerant or highly available.

Conditions for Cache Expiration

  • Memory exhaustion (cache is full).
  • Explicit deletion of a cached item.
  • Expiration of a cached item's time-to-live (TTL).

Memcache vs. Memcached

Memcache Memcached
Description Client library providing an interface to Memcached Distributed memory object caching system
Interface Procedural and object-oriented Uses the libmemcached library
Session Handling Provides Memcache session handler Provides Memcached session handler

Sharing a Memcached Instance

Yes, multiple applications can share a single Memcached instance (or a cluster of Memcached servers). Data needs to be partitioned appropriately to avoid conflicts.

SAP HANA vs. Memcached

SAP HANA is an in-memory database optimized for SAP applications. Memcached is a key-value store used primarily for caching.

Connecting to Memcached with telnet

Use the telnet command to test the connection to a Memcached server and execute commands directly:

Syntax

telnet hostname portNumber

Memcached Commands

Key Memcached commands (used via telnet or client libraries):

  • get key: Retrieves the value associated with the specified key.
  • set key flags expiry bytes value: Stores data with a key, flags, expiry time (seconds), and data size.
  • add key flags expiry bytes value: Adds a key if it doesn't already exist.
  • replace key flags expiry bytes value: Replaces the value associated with an existing key.
  • append key flags expiry bytes value: Appends data to an existing key's value.
  • prepend key flags expiry bytes value: Prepends data to an existing key's value.
  • delete key: Removes a key from the cache.
  • stats: Shows server statistics.
  • version: Displays the Memcached server version.
  • quit: Closes the connection.

Updating Memcached When Data Changes

Two approaches to updating Memcached when data in your application changes:

  • Proactive cache invalidation: Delete outdated cache entries when data is updated or inserted in your main data source.
  • Cache invalidation on demand: The application checks if a cached value exists. If it does not, it fetches the current data from the data source.

Updating Memcached: Cache Resetting

An alternative to clearing the entire cache is to reset only the changed values. This method updates the cache entries after an insert or update without deleting and refetching all entries. This approach is usually more efficient than completely clearing the cache.

The Dogpile Effect

The dogpile effect occurs when multiple clients simultaneously request a recently expired cache entry. This causes a surge in requests to the database when the cache is being regenerated.

Preventing the Dogpile Effect

The dogpile effect can be mitigated by using a locking mechanism (like a mutex or semaphore). When a cache entry expires, the first client to request it acquires the lock, fetches the data, updates the cache, and releases the lock. Other clients wait until the lock is released before fetching the data.

Data Loss on Server Shutdown

Memcached data is volatile (stored in RAM); it's lost when the server shuts down or restarts. Memcached is not a persistent storage solution; it's designed for caching.

Handling Memcached Server Failure

If a Memcached server fails, clients won't be able to access data stored on that server. Proper configuration with multiple Memcached servers and a failover mechanism is essential for high availability.

Minimizing Memcached Outages

  • Minimize Cache Stampedes: Write your application code to handle cache misses efficiently, reducing the load on your database during cache regeneration.
  • Failover Mechanisms: Configure your Memcached clients to automatically switch to other available servers if one fails.
  • Redundancy: Use multiple Memcached servers to prevent a single point of failure.
  • Client-Side Timeouts: Configure your Memcached client to have timeouts when connecting to Memcached servers to allow your application to continue functioning even if a server is temporarily unavailable.