What Is Memcached? Why Web Developers Love It?


Want to develop a high-performance large-scale web application? You may probably get benefitted with Memcached distributed caching solution.

Memcached is a popular distribute caching system. It is developed using PHP but available to use in all popular languages including Java, Python, and Ruby.

It was developed by Brad Fitzpatrick in 2003 and has been heavily used in PHP applications after that.

Memcached is very simple to use. It can store a key value pair in memory for fast retrieval. The Memcached API offers methods to manipulate and fetch data easily. Serious Memcache users may also want to check out these books on Memcached.

This article provides a very high-level overview of Memcached system and its benefits.


How Does Memcached Work?

Memcached has a distributed caching architecture that relies on sharding of keys. Each key is stored in a dedicated shard that may be backed by one or more machines.


This approach helps us scale better for caching really large amount of data. A single machine can cache only up to its RAM limit. Whereas in the case of Memcached you can add many machines to your system and cache really large amount of data.


The design of Memcached abstracts the storage from API users, so the usage of the system is very simple. The application user need not worry about the complex logic it uses to divide keys between different machines. It guarantees that a provided key will be stored and retrieved without user knowledge about where it is stored.

Can It Be Used Only For Web Applications?

Memcached is usage in a web application is very common. However, its design does not limit it to be used in only web applications. You can use it in any application software where distributed caching can help. The key idea is to avoid IO and server data direct from RAM.

Why Is Memcached Popular?

Memcached is very popular among web applications. Below are some key benefits of using a distribute caching solution like Memcached.

  • Much faster application since IO is reduced and most data is served from RAM.
  • Better use of RAM - A lot of RAM may be left unused on multiple servers. You can easily add these machines as nodes to Memcached system and start making use of it.
  • The application can scale out instead of scale up. 

Practical Scenarios Where Memcached Can Be Used

Below are some practical scenarios where people have used Memcached successfully. 

  • Serving relatively static data that changes infrequently but gets read a lot. E.g. Blogs, Websites with dynamic pages that are generated, User profile pages etc. 
  • Preventing weak passwords attack. I will describe this in a separate blog post sometime this month.
  • Page elements of websites can be cached that do not really change in the application lifecycle. 
  • There may be large tables in your relational database that are accessed often with a primary key. This access can be minimized by caching most common query results in Memcached. 

Who Is Using Memcached?

Memcached is a popular library and used by thousands of apps. Below are some popular names that use Memcached.
  • Apple 
  • Craigslist 
  • Wikipedia 
  • Flickr 
  • Wordpress

Areas Of Concern About Memcached

Memcached is a highly scalable solution however, it has some areas of concern that you need to be aware.

  • It stores all data in RAM. This makes it fast, however, it also makes it easy to loose. This data is never persisted to a storage system. If there is a power loss or server crash, you will lose all your data in Memcached. 
  • Since it is always in RAM, you need to initialize the cache after every restart. This may require extra programming awareness towards serving data from cache or storage. 
  • Since it's not persisted in any storage, the application developer must take the onus of persisting and updating data for all scenarios. All CRUD (Create, Read, Update, Delete) scenarios must be considered while designing your system with Memcached.
  • Memcached does not support transactions, this may be a big consideration if you are trying to cache transactional data. 
  • It can be CPU intensive mainly due to generating a lot of garbage in memory.

What Are Memcached Alternatives?

Below are some alternatives to Memcached that you can consider using for highly scalable applications.

Redis

Redis is an open source in-memory caching solution with a lot many more features as compared to Memcached. It has support for data structures such as strings, list, and sets.

Couchbase

Couchbase is a leading noSQL database that is built on top of Memcached. It has enhanced features for persistent storage and low latency.

There are other alternatives catching popularity in recent years. See below google trends.

Memcached trend in google compared with redis and others


Summary

Memcached is a powerful system and many applications are able to get faster response time with it. Are you using it? Please share your feedback about it in comments. 

Related

Performance 2432012979938348134

Post a Comment Default Comments

...

item