Memcached Memory Management

Source: Internet
Author: User

Memcached Memory Management

Memcached uses the mechanism named Slab Allocation to allocate and manage the memory.

The principle of Slab Allocation is quite simple. Divide the allocated memory into blocks of various sizes (chucnk), and divide the blocks of the same size into groups (a set of chcnk)

In addition, slab Allocation can reuse allocated memory. That is to say, the allocated memory is not released, but reused.

Slab Allocation terminology

  • Page: memory space allocated to Slab. The default value is 1 MB. After being allocated to Slab, it is split into chunk Based on the size of slab.
  • Chunk: memory space used to cache records.
  • Slab Class: Group of chunks of a specific size.

Principle of caching records in Slab

Memcached selects the Slab with the most suitable data size based on the size of the received data (Figure 2) memcached stores the list of idle chunks in slab, and selects chunk according to the list, then, cache the data in it.

Memcached data expiration and Deletion

Memcached does not monitor whether the record expires. Instead, it checks the timestamp when getting the record to check whether the record expires. This technology is called lazy expiration. Therefore, memcached does not consume CPU time on expired monitoring.

When new data is added, Memcached preferentially uses the time-out record space. If the space is insufficient, use the Least Recently Used (Least Recently Used LRU) mechanism to allocate space. Therefore, when memcached has insufficient memory space (the new space cannot be obtained from the slab class), it searches for the recently unused records and allocates the space to the new records.

Distributed principle of Memcached

Memcached is distributed through the client. It selects a server with the key of new data (key-Value Pair) and stores it in Memcached.

For example:

Add "tokyo" to memcached ". After "tokyo" is passed to the client library, the algorithm implemented by the client determines the memcached server that stores data based on the "key. After the server is selected, run the command to save "tokyo" and its value. Similarly, "kanagawa", "CHBA", "saitama", and "gunma" are both saved on the server first. Next, obtain the saved data. The key "tokyo" to be obtained is also passed to the function library. The function library uses the same algorithm as the data storage algorithm to select a server based on the "key. If the algorithm used is the same, you can select the same server as the storage server and then send the get command. As long as the data is not deleted for some reason, you can get the saved value.

In this way, memcached is distributed by storing different keys on different servers. When the number of memcached servers increases, keys will be dispersed. Even if a memcached server fails to connect, other caches will not be affected, and the system will continue to run.

Memcached installation and startup script

Performance problems of using Memcached in PHP

Install Memcached in Ubuntu and its command explanation

Install and apply Memcached

Use Nginx + Memcached's small image storage solution

Getting started with Memcached

For details about Memcached, click here
Memcached: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.