What is Memcached and How does it Work

In this article, we will learn about memcached and working of the same.


What is Memcached

A general-purpose networked caching system that is free to use is Memcached. mostly used to lighten the load on databases in order to speed up dynamic online applications. For short data chunks, database calls, API calls, or page rendering, Memcached also functions as an in-memory key value store. Linux, macOS, and Windows are all operating systems that are similar to Unix, and the software is open source.

Advantages of Memcached

  1. It is an open source technology.
  2. Simply add more boxes with enough of memory to scale it up.
  3. Rapid reaction times made possible by the in-memory key-value storage.
  4. Computing power can possibly be scaled vertically thanks to multi-thread design.
  5. Advanced open-source programme with a datastore that is accessible to the whole population.
  6. Adaptable in terms of application development and simple to utilise.
  7. Supports the most popular clients, computer languages, and open data formats.
  8. It lessens the burden on the database.
  9. For websites with significant database loads, it is particularly effective.
  10. The cache nodes lack knowledge about other participating nodes since they are extremely dumb. This makes managing and configuring such a system incredibly simple.
  11. Running independently, Memcached is a service. The cached data will therefore continue to exist in memory as long as the service is running even if your application is shut down.
  12. A very basic and generic memory caching system is Memcached. Because of this, it is incredibly versatile in terms of application development and enables developers to grow their apps without having to do intricate database programming.

Features of Memcached

  • Memcached is a high-performance, in-memory data store that is simple to use.
  • It can be used as a cache or session store since it provides an established, scalable, open-source solution for delivering sub-millisecond response speeds.
  • Memcached keeps all of its data in the server’s main memory.
  • Memcached retains its data in memory as opposed to databases, which keep their data on disc or SSDs.
  • Memcached can easily scale out by adding new nodes because it is distributed.
  • Memcached is multithreaded, therefore expanding the computing power is simple.
  • It is a high-performance memory caching technology that is free and open-source.
  • In order to improve application performance, it is frequently used to cache database information, API requests, or page rendering chunks in RAM.
  • It can hold information as small as a number or as large as a completed HTML page.
  • The older data is replaced as space runs out. The fact that your data might not still be there means that it must be viewed as a temporary (not-persisted) cache.

Disadvantages of Memcached

  • It is not a tool that is fault-tolerant.
  • It is extremely slow when compared to in-memory caching, primarily due to serialisation or deserialization and network latency.
  • The cache nodes are extremely illiterate; for instance, it is impossible to iterate through all of the cached entries.
  • A persistent datastore is not what it is. So it only stores data temporarily and loses the entire data if a Memcached instance fails or crashes.
  • Because data cannot be displayed, debugging is challenging.
  • Value keys can only have a maximum length of 250 characters (1 MB).
  • Additional firewalls are required due to a lack of security measures.
  • Memcached isn’t primarily intended to be a security programme. In general, it makes your applications run faster. To keep your web application secure if you’re using a shared system, you’d need extra security measures like third-party firewalls.

How does Memcached Work?

Data is often stored on a hard disc or, preferably, a solid-state drive (SSD) in conventional databases. On the other hand, Memcached eliminates the delay brought on by seek times while obtaining data by storing it in memory where it may be recovered in microseconds. Although data can be kept for a long time, most data are destroyed by default after a specific period of time. This is so because Memcached isn’t really a database in the conventional sense; it’s just a cache. Therefore, as soon as there is no longer room for new data, the least requested material is erased.

In-memory key-value storing is another term used to describe Memcached. The first step is to establish a connection with the server using the TCP and IP protocols. Memcached will determine whether certain data is present in the cache if a user requests a specific piece of information. The necessary information will be fetched from the main memory if it is not. The client then supplies a key value for the relevant data, which the software library has created. The client then decides which Memcached server the data, which takes the form of character strings, should be saved on using a hashing method.

Memcached, which can store and retrieve data, has below primary components, as was already explained. A key, an expiration date, and raw data make up each item. High-level functions of Memcached are as follows:

  1. When a client requests data, Memcached first determines whether it is already in cache.
    1. If the data is cached, simply return it from Memcached (the database need not be checked).
    2. The information is not kept in cache; rather, it is requested from the database, obtained, and then kept in Memcached.
  2. Memcached updates its cache whenever data is modified or after an item’s expiry value has passed, ensuring that clients always receive up-to-date information.

This arrangement has numerous clients and different Memcached servers. Clients choose the appropriate memcahed storage server using a hashing method. Thus, the load is more evenly distributed.

The server then calculates a second hash of the key to identify where in its internal hash table to store the associated value. The following are some critical aspects of Memcached architecture:

  1. Only one server receives data.
  2. Data is not shared among the servers.
  3. Servers store the values in RAM, and the oldest value is destroyed when RAM is exhausted.

Leave a Comment