12 main components of memcache

Divesh
9 min readMar 9, 2023

--

Memcached is a computer program that helps make websites and apps run faster by storing data in a place that can be accessed more quickly.

below is the short history of memcached!

Memcached was originally created by Brad Fitzpatrick in 2003 while he was working at LiveJournal, a blogging platform that experienced rapid growth and scalability challenges. Fitzpatrick developed memcached as a way to improve the performance of LiveJournal by caching frequently accessed data in memory and reducing the load on the backend database.

Memcached quickly gained popularity and was released as open-source software in 2004. It became widely adopted by web developers and companies as a high-performance distributed caching system for improving the performance and scalability of web applications.

Since its initial release, memcached has been maintained and developed by a community of contributors, with new features and improvements added over time. Today, memcached is used by many large-scale web applications, including Facebook, Twitter, Wikipedia, and YouTube, to improve the performance and scalability of their services.

In addition to memcached, there are now several other caching systems available, including Redis, Apache Ignite, and Hazelcast, that offer similar features and capabilities. However, memcached remains a popular and widely used caching system due to its simplicity, speed, and ease of integration with existing web architectures.

Memcache

Let’s say you have a bunch of toys, and your friends come over to play with them all the time. But every time they come over, you have to go to your toy box and find the toys they want to play with. It takes a lot of time and makes your friends wait.

Memcached is like having a special shelf in your room where you put the toys your friends play with the most. When your friends come over, you can quickly grab the toys from the special shelf and give them to your friends to play with. It saves time and makes your friends happy because they don’t have to wait as long to play with their favorite toys.

In the same way, when you use a website or app, there are certain things you do a lot, like logging in, searching for something, or looking at your profile. Memcached helps store this information in a special place called the “cache”. When you do those things again, the website or app can quickly retrieve the information from the cache instead of having to search for it all over again. This makes the website or app run faster and smoother.

So, in summary, Memcached helps websites and apps run faster by storing data in a special place called the “cache”, which can be accessed more quickly than searching for the data all over again.

Here are the main components that I am going to explain:

  1. Client:

This is the component that is responsible for communicating with the Memcached server to store and retrieve data.

Lets understand client in detail:

a client is an application or process that communicates with the memcached server to store, retrieve or delete data from the cache.

The client typically sends requests to the server using a simple key-value interface, where the key is a unique identifier for the data and the value is the data itself. The client can send requests to the server to store data in the cache, retrieve data from the cache, or delete data from the cache.

Clients can be written in various programming languages and use various client libraries or drivers to interact with the memcached server. These libraries abstract the details of the communication protocol and provide a simple API for the application to use.

The client can also configure the behavior of the memcached server, such as the size of the cache, the expiration time of the data, and the distribution of the data across multiple servers.

2. Server:

This is the component that stores and retrieves data in memory, and responds to client requests.

Lets understand what is server is memcache!

A server is a software component that provides a cache service to clients. The server stores and manages data in a cache, which can be accessed by clients for fast data retrieval.

Memcached servers typically run on dedicated machines or as part of a cluster of servers. Clients connect to the server over a network and send requests to store, retrieve, or delete data from the cache.

Each server can store a subset of the data in the cache and has a unique identifier in the cluster. When a client requests data from the cache, the server checks if the data is available locally, and if not, it retrieves the data from another server in the cluster that stores the data.

Servers can be configured with various settings, such as the size of the cache, the maximum amount of memory allocated to the cache, and the behavior when the cache is full. Memcached servers also support advanced features such as replication, sharding, and consistent hashing, which enable the distribution of data across multiple servers for scalability and fault tolerance.

3. Memory Cache:

This is the main component of Memcached, which stores the data in memory.

lets understand what is memory cache in memcache.?

Memory cache in memcached refers to a high-performance, distributed caching system that stores frequently accessed data in memory to reduce the time required to retrieve the data from slower data sources such as disk or database.

In memcached, the memory cache is implemented as a distributed hash table, where the data is partitioned across multiple servers in the cluster. Each server stores a subset of the data in its memory cache, and clients can access the data by sending requests to any of the servers in the cluster.

When a client requests data from the cache, the server checks if the data is available locally, and if not, it retrieves the data from another server in the cluster that stores the data. This enables memcached to scale horizontally by adding more servers to the cluster as the demand for the cache grows.

The data stored in the memory cache is typically transient and can be expired after a configurable period or evicted if the cache is full. Memcached supports a simple key-value interface, where the application can store, retrieve, and delete data using a unique key identifier.

Memory cache in memcached is commonly used to improve the performance of web applications, especially for read-heavy workloads, where the same data is frequently accessed by multiple clients. By caching frequently accessed data in memory, memcached reduces the load on the backend data sources and improves the overall response time of the application.

4. Cache Key:

This is a unique identifier that is used to access a particular piece of data in the cache.

5. Cache Value:

This is the actual data that is stored in the cache.

6. Cache Expiration:

This is a time limit set for how long data should be stored in the cache before it is considered expired and discarded.

Lets understand what is Cache expiration.!

Cache expiration in memcached refers to the process of automatically removing data from the cache after a specified period of time. This ensures that the cached data is fresh and up-to-date and reduces the risk of serving stale data to clients.

In memcached, cache expiration is controlled by setting a time-to-live (TTL) value for each key-value pair stored in the cache. The TTL value specifies the amount of time that the data can remain in the cache before it is automatically expired and evicted from the memory.

When a client requests data from the cache, the server checks if the data is still valid by comparing the current time with the time when the data was stored in the cache. If the data has expired, the server removes the data from the cache and returns a cache miss to the client.

The TTL value can be set for each key-value pair at the time of storing the data in the cache or updated later using a separate operation. If the TTL value is set to 0, the data is stored in the cache indefinitely until it is explicitly deleted by the client or evicted due to memory constraints.

Cache expiration is an important feature in memcached as it helps to ensure that the cached data is fresh and relevant. It also helps to control the size of the cache and prevent the cache from consuming too much memory by storing outdated data.

7. Hash Function:

This is an algorithm that converts the cache key into a memory address where the data is stored in the cache.

Lets understand the hash function of memcache which is important to know.!

In memcached, a hash function is used to map a given key to a specific server in a cluster. This enables data to be distributed across multiple servers in the cluster, allowing for scalability and fault tolerance.

The hash function takes a key as input and returns a hash value, which is used to determine the server in the cluster that will store the data associated with the key. The hash function should be deterministic, meaning that the same key should always be mapped to the same server in the cluster.

Memcached uses a consistent hashing algorithm to distribute data across servers in the cluster. The consistent hashing algorithm uses a ring of hash values, where each server is assigned a range of hash values based on its unique identifier in the cluster. When a key is hashed, it is mapped to the server with the closest matching hash value on the ring.

Consistent hashing ensures that data is distributed evenly across servers in the cluster, and also enables the cluster to scale up or down dynamically without requiring all data to be redistributed.

The choice of hash function can affect the performance and scalability of a memcached cluster. A good hash function should distribute data evenly across servers and minimize collisions, where multiple keys are mapped to the same hash value. Collisions can lead to uneven data distribution and hotspots, where some servers in the cluster become overloaded with traffic.

8. Connection Pool:

This is a pool of connections between the client and server, which allows multiple clients to access the cache simultaneously.

9. Cache Miss:

This occurs when the requested data is not found in the cache, and the server has to fetch it from the database or another source.

10. Cache Hit:

This occurs when the requested data is found in the cache and can be returned immediately.

11. Slab Allocator:

This is a memory management component that allocates memory in predefined sizes called “slabs”.

12.LRU (Least Recently Used) Algorithm:

This is an algorithm used by Memcached to discard the least recently used data from the cache when the memory limit is reached.

Lets now understand the LRU in memcache.?

In memcached, LRU eviction policy is used when the cache is full, and a new key-value pair needs to be inserted into the cache. The LRU policy evicts the least recently accessed key-value pair from the cache to make space for the new data.

The LRU eviction policy works by maintaining a list of all the keys in the cache in the order of their access time, with the most recently accessed key at the front of the list and the least recently accessed key at the back of the list. When the cache is full, the key at the back of the list is evicted from the cache.

This eviction policy assumes that the data that was accessed recently is more likely to be accessed again in the near future than the data that was accessed a long time ago. This makes LRU a good choice for caching systems where there is temporal locality, meaning that recently accessed data is more likely to be accessed again soon.

Memcached supports other cache eviction policies, such as random eviction, where a random key-value pair is evicted when the cache is full, or LRU with expiration, where the least recently accessed key-value pair that has expired is evicted. The eviction policy can be configured using the “eviction” parameter in the memcached configuration file.

Let us know if anything is to be added modified in this post we will be happy to do that..

--

--

Divesh
Divesh

Written by Divesh

An Architect, A DevOps Engineer, An Automation master, A Kubernetes Security Specialist and always willing to help because helping others is my favourite task.

No responses yet