What is memcached?
Memcached is the free and open source caching system which is used to cache database data, API calls, page rendering in chunks in RAM.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
How does memcached work?
Unlike databases that store data in dicks or SSDs, memcached keeps it's data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds. More over memcached is multithreaded so it uses multiple cores on a given node. Which makes it simple to scale up compute capacity. Highly scalable distributed caching solutions designed to provide fast and consistent performance can be built with memcached.
How Distributed caching implemented in memcached?
- Memcached server is unaware of each other
- Client is configured with server pool
- Client do the distribution through consistent hashing
- It is up to client to shuffle the key when adding / removing servers
How does memory management done in memcached?
- Memory allocation is random
- Items freed/allocated causes gaps
- Results in memory fragmentation
( Figure of memory fragmentation ) - New items can no longer fit
- Memory is allocated in pages ( 1 MB configurable )
- Pages are organized by slab classes
- Pages consists of chunks of fixed size
- Items are stored in chunks
- Each slab class has fixed chunk size
Memcached vs Redis
Redis and Memcached are the two most popular in-memory key-value data stores. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases.
Now let's understand the difference between them in brief:
Memcached | Redis | |
Sub-millisecond latency | Yes | Yes |
Developer ease of use | Yes | Yes |
Data partitioning | Yes | Yes |
Support for a broad set of programming languages | Yes | Yes |
Advanced data structures | - | Yes |
Multithreaded architecture | Yes | - |
Snapshots | - | Yes |
Replication | - | Yes |
Transactions | - | Yes |
Pub/Sub | - | Yes |
Lua scripting | - | Yes |
Geospatial support | - | Yes |