Memcached is an open source distributed caching system. Used to speed up dynamic web applications by reducing database load. In other words, each database adds an additional load to the server when the request is made. Memcached reduces the load by storing data objects in dynamic memory (think of them as short-term memory for applications). Memcached stores data based on key/value pairs for small strings or objects:
- Results of database calls
- API calls
- Create a page
Memcached consists of four main components. These components allow the client and the server to work together to deliver cached data as efficiently as possible:
- Client software : A list of available Memcached servers is given
- Client-based hash algorithm : Selects a server based on "key"
- Server software : Saves values and keys to an internal hash table
- LRU : Determines when old data is discarded or when memory is reused
How does memcached work?
As mentioned above, Memcached consists of four main components and these components are elements that allow data storage and acquisition. Each element consists of key and expiration time and raw data. The top level of Memcached works as follows:
- The client requests a piece of data that Memcached checks to see if it is stored in the cache.
- Here are two possible results:
- If the data is stored in the cache: returns the data from Memcached (it does not need to check the database).
- If the data is not stored in the cache: queries the database retrieves the data and then stores it in Memcached.
- When information is changed or an item expires, Memcached updates its cache to ensure that new content is forwarded to the client.
A typical setup has several Memcached servers and many clients. Clients use a hash algorithm to determine which Memcached storage server to use, which helps deploy the load. The server then calculates a second key cartridge to determine where to store the corresponding value in an internal hash table. Here are a few important points about memcached architecture:
- Data is sent only to one server.
- Servers do not share data.
- Servers keep values in RAM. If ram runs out, the oldest value is discarded.