Memcached is often used to improve the per­for­mance of dynamic ap­pli­ca­tions with database con­nec­tions. This in-memory datastore ensures, for example, that access to the hard drive does not need to retrieve data from the memory. Not only does this relieve the load on back-end systems, but it also sig­nif­i­cant­ly reduces latency. Popular websites such as YouTube, Facebook, Twitter, and Wikipedia have been reaping the benefits of this open-source solution for a long time now, allowing internet users to enjoy an improved browsing ex­pe­ri­ence. So, how does Memcached work, and how do you get started using it? Here, you will learn every­thing you need to know about this popular caching system.

What is Memcached?

Memcached is a high-per­for­mance caching system that was developed by Danga In­ter­ac­tive nearly twenty years ago for the internet portal Live­Jour­nal. The cache server was developed with the intent of getting around time-consuming database accesses when using demanding web ap­pli­ca­tions. The solution was to use an in-memory datastore that could provide website users with cached elements quickly. Memcached is very user-friendly and easy to install, set up and use. It is also under the BSD license, so it may be freely used, modified, and copied.

What does Memcached’s ar­chi­tec­ture look like?

The general structure of Memcached’s ar­chi­tec­ture is rel­a­tive­ly simple. It is similar to a dis­trib­uted database system and consists of the ap­pli­ca­tion, a client library, and a pool of Memcached instances. Any number of these instances can be installed on a server’s main memory. We recommend ac­ti­vat­ing one instance on each server that has memory to spare. These instances work together to acquire the free space that is available for the cache. The client library is the interface between the re­spec­tive ap­pli­ca­tion and Memcached. It takes the data to be stored and places it on an existing server. Through its multi-thread ar­chi­tec­ture, Memcached can also use several process kernels si­mul­ta­ne­ous­ly.

Note

In addition to Memcached, Redis is also a popular in-memory datastore. While Memcached stands out for its sim­plic­i­ty, Redis brings a wide range of ap­pli­ca­tions and functions to the table. If you want to figure out which software best meets your needs, you can read our article directly comparing Memcached vs. Redis.

How does Memcached work?

Con­ven­tion­al databases store data on a hard drive or prefer­ably a solid-state drive (SSD). With Memcached, on the other hand, data is stored in memory so that it can be retrieved in mi­crosec­onds, elim­i­nat­ing the delay caused by seek times when re­triev­ing data. While it is possible to store data for a prolonged period, most data are au­to­mat­i­cal­ly deleted after a certain amount of time. This is because Memcached is just a cache and not a database in the tra­di­tion­al sense. The least requested data is thus deleted as soon as there is no more space for new data. So, how exactly does the storage process work?

Memcached is also described as an in-memory key-value store. Using the TCP and IP protocols, a con­nec­tion is first es­tab­lished with the server. If a user wants to retrieve specific data, Memcached will check whether it is available in the cache. If it is not, the required data will be retrieved from the main memory. The client then provides a key value for the data in question which is generated by the software library. Using a hashing algorithm, the client then de­ter­mines which Memcached server the data, which is in the form of character strings, is to be stored on. Below are the five most important char­ac­ter­is­tics of Memcached:

  • Data is only sent to one server.
  • Data is stored as key-value pairs.
  • The various servers do not share data with each other.
  • The server only stores data in the memory.
  • If there is not enough space, the server will delete the oldest data.

What are the ad­van­tages and dis­ad­van­tages of Memcached?

De­ter­min­ing whether Memcached is the best caching solution for your needs depends on the re­quire­ments and the com­plex­i­ty of the ap­pli­ca­tion in question. Its temporary memory system can be useful, for example, when it comes to high-traffic web ap­pli­ca­tions and websites accessing huge databases. Naturally, there are also a few dis­ad­van­tages that come along with its many ad­van­tages. The following is a brief overview:

Ad­van­tages Dis­ad­van­tages
Extremely fast response times thanks to the in-memory key-value store Only stores data tem­porar­i­ly and loses that data if a Memcached instance fails
Multi-thread ar­chi­tec­ture enables vertical scaling of computing capacity Data cannot be viewed which makes debugging difficult
So­phis­ti­cat­ed open-source solution with a publicly available datastore Key length for values is limited to 250 char­ac­ters (1 MB)
Easy to use and flexible in terms of ap­pli­ca­tion de­vel­op­ment Lack of security features ne­ces­si­tates ad­di­tion­al firewalls
Supports open data formats and most common clients and pro­gram­ming languages Not redundant (i.e., no failover using mirroring or data backups)

What is Memcached used for?

Memcached’s uses include caching and storing session data. Caching makes cached data such as files, metadata, and images available at lightning speed. This allows you to scale in a cost-effective manner when your workload increases and improves the per­for­mance of your ap­pli­ca­tion. De­vel­op­ers also use in-memory stores to manage session data such as user profiles or online session states. However, this requires that data per­sis­tence not be essential. Generally, Memcached is a good solution for ap­pli­ca­tions that require a high-per­for­mance large-scale cache.

Getting started with Memcached

Memcached’s greatest strengths are un­doubt­ed­ly its speed, scal­a­bil­i­ty, and support of all common APIs and pro­gram­ming languages, including Ruby, Java, JavaScript, Python, Go, PHP, C, C++, C# and Node.js. It is also simple to install on a Windows or Unix operating system. The Memcached daemon is provided by Danga In­ter­ac­tive as a free download. In addition to the relevant developer package, you also need a libevent library that provides asyn­chro­nous event no­ti­fi­ca­tions. After in­stal­la­tion, there will be different con­fig­u­ra­tion options available for the client and server depending on the pro­gram­ming language used.

Since Memcached is an open-source project, it has been used and co-developed by a large community for many years. This is an advantage in that detailed in­for­ma­tion on using, managing, and trou­bleshoot­ing various APIs and operating systems is available to you online.

Go to Main Menu