Many of the caches that already been discussed before have more to do with caching precompiled code for faster execution as well as optimization. Memcached in MySQL is a simple, highly scalable key-based cache that stores data, file and objects wherever dedicated or spare RAM is available for quick access, without needing to parsing or disk I/O. Memcached allows you to take extra, unused memory and make it available to your application that could use extra memory for caching. It also allows you to use memory efficiently that it conglomerates disparate Memcached servers into one memory pool.
In traditional caching systems, memory would be used on each server separately, which was unefficient and wasteful because the capacity of this cache type was only a fraction of what it could be when you sum total of memory across your server farm. Also that way would be complex to keep the data stored in the cache consistent.
How Does Memcached in MySQL Work
Memcached in MySQL at its core utilize a slab allocator, providing a dictionary, or hash lookup table where items are cached in memory, for extremely fast performance. The important thing to realize is the data that stored in memcached is not durable (it’s not really stored in a traditional on-disk sense, and gone when the server goes away by being shutdown or restarted).
Memcached has no failover or built-in replication, so it’s up to the application (with memcached) to implement how data is managed and keep updated. Thanks to Alan Kasindorf, Dustin Sallings, Trond Norbye, and Turo Maesaka, Memcached project is constantly being improved.
Memcached is a “Least Recently Used” cache, which means that the older data always be replaced by newer data when memcached’s memory capacity is reached. Also, Memcached allows you to set an expiration time for object being stored. Memcached can run in any type of configuration (one or more servers), even with multiple instances on the same server. The memcached server itself is simple. The one great thing about memcached is you can use it with cheap commodity hardware to solve your caching requirements. Also it’s not CPU intensive. It simply uses memory.
The mechanism that used to threat multiple memcached servers as one is called consistent hashing. It’s implemented in the client. It determines what object will be stored to or retrieved from. Consistent Hashing essentially computes hashed value of connected server and compares it to hashes value which memcached server using. Once client knows which server handle the given item, it sends the request in parallel to the appropriate servers. Then, client aggregates the result together for the application to use.