Note: This blog post is referenced in two books.
- "Memcached Comprehensive Analysis", the book market should not, I reached the Baidu Cloud disk, link as follows: Http://pan.baidu.com/s/1qXqXmri
- "Large Web site Technology architecture: Core Principles and Case studies"
Premise:
- This article is based on the memcached1.4 version, the previous version is not the same as this version in some places (eg. "memcached comprehensive analysis" memcached1.2 memory management mode is different from 1.4)
- Before looking at this article, it is best to look at memcached in the actual development of how to operate, link "Eighth Enterprise project development--distributed cache memcached"
1. memcached characteristics
- Protocol simple (Text protocol, Binary protocol )
- Based on Libevent event processing, libevent encapsulates the time-processing capabilities of the Linux Epoll model.
- Slab Storage Model
- The server in the cluster does not communicate with each other (in the case of large cluster, its performance is far superior to other buffers of synchronous update cache, of course, the performance of memcached is excellent under small cluster)
2. memcached Access model
Description
Xmemcached's specific use code to see the "Java Enterprise project development Practice" series of the "eighth Chapter enterprise project development-distributed Cache memcached", the following explanation will be based on this code.
In, the Memcached client assumes the use of xmemcached
- Server list: Configured in the root Pom.xml file
- There are two types of routing algorithms: (can be specified in the program)
- Consistent hash algorithm (recommended)
- Simple method of finding remainder
- Communication module:
- Communication protocol: TCP protocol
- Serialization protocol: Binary Protocol (recommended), text-basedprotocol
- Memcached API (cache additions and deletions): write in the program
The entire process:
Application (Adminservice) calls the memcached API (assuming an add operation) and adds a cache to the memcached server, The program will first select a server (assuming Node1) in the server list based on the configured routing algorithm (assuming it is a consistent hash algorithm), after which the API serializes the object via the serialization protocol (of course, this is not possible, Eg.value is a string, and the key-value pair that will be stored is deposited to the appropriate server via the TCP protocol. At get, whenever you use the same hash algorithm as add, the server at add is selected.
After reading this paragraph, the process is clear. But there are a few questions:
- How are the two routing algorithms implemented? Why use a consistent hash algorithm
- How exactly is the cache stored when it arrives at the server? (Slab memory model)
- When the cache exceeds a certain capacity, the automatic deletion of the cache is what strategy, how to delete it? (LRU)
- What are the advantages and disadvantages of the two serialization protocols?
3. Hash algorithm
3.1. Simple method of finding remainder
Principle step: Obtain the integer hash value of key (for Java object, directly use its hashcode () method is good), divided by the number of servers, get the remainder, based on the remainder to select the server.
Note: If the selected server is unable to connect, it will be rehash, that is: Add the number of connections to the key, recalculate the hash value, and then reconnect. Of course, rehash can be banned.
Advantages:
- Simple
- Good hash dispersion (because the value of hashcode () is random)
Disadvantages:
- When adding or removing a server, the cache gets a problem (because the number of servers has changed, when the denominator changes, the remainder is likely to change), assuming that in 99 memcached servers added another one, the cache is 99%, that is n/(n+1), n represents the original server.
Attention:
- The algorithm is still retained in the xmemcached
- Suitable for no need to consider cluster scalability (that is, the total number of machines is unchanged)
3.2. Consistent hash algorithm
For the vast majority of systems, the scalability of the cluster is one of the more important five non-functional requirements, that is, the shortcomings of "simple redundancy method" must be overcome.
- Principle: First constructs an integer ring of length 0~232 (uses two fork tree constructs), according to the node (memcached server) name hash value puts the cache server node on this hash ring, then calculates its hash value according to the key which needs to cache the data, Then on the hash ring clockwise to find the hash value of the key from the nearest cache server node, complete the key to the server hash mapping lookup.
- If more than 232 are not found, then there is the first memcached (still clockwise)
- Problems: When the number of servers is relatively small, it is possible to cause load imbalance situation, in order to prevent this situation, using the physical server is virtualized into multiple virtual servers, and then put the hash value of these virtual servers on the ring, when the client routes to a virtual server, Locate the physical server that corresponds to the virtual server.
- In general, a physical server virtualization to 150 virtual servers is the most appropriate , too little load uneven, too many will affect performance
- Memcached using such an algorithm, when we join a server or a server in a cluster outage, will not have a significant impact, only a small segment (see), to ensure the availability and scalability of the cluster
Attention:
- The hash ring is a binary tree, and the last leaf is connected to the left side as a ring.
- The entire cache lookup process is looking for a minimum value that is just greater than or equal to the lookup number
Question: (This does not find the information)
- What is the hash algorithm of the server?
- Calculates whether the hash algorithm of the cache key is consistent with the server, and can also use the original Hashcode ()
4. Slab Memory model
4.1. Why use slab memory model?
At the very beginning, memory allocations and recoveries are handled by malloc and free, which creates memory fragmentation, increases the burden on the memory manager, and severely caches operations that affect efficiency.
The advent of the slab model is to:
- Improve cache operation efficiency
- Complete resolution of memory fragmentation issues.
Attention:
- First goal: already implemented (because direct positioning of the appropriate chunk will soon)
- Second objective: Using the slab mechanism will still produce memory fragmentation, or memory waste.
4.2. Slab Model Principle
Description: The figure was taken from a blog (marked in the picture, but not visible), but it was picked a long time ago, forgotten. I found it later, I will mark the source.
Memcached memory allocation is the following sentence: the use of Group Management , pre-allocation method.
4.2.1, group Management
- GROUP BY: memcached divides the memory space into a set of slab, each slab is fixed to 1M, each slab contains a set of chunk, and each slab in the same chunk is the same size. Depending on the size of the chunk in these slab, these slab are numbered slab class (that is, classes I in).
- Storage principle: When it comes to a key-value pair to be stored, we look at the size of the data and choose the most suitable slab class for the idle chunk to place the object.
- The most appropriate chunk: the size of the chunk is just larger than the size of the stored data, and smaller than the size of the chunk is just smaller than the data to be stored.
This approach can cause a lot of memory waste (which I think is also memory fragmentation).
- The way to reduce memory waste: Estimate the size of your own cache data, and then reasonably specify the parameter-F (growth factor) and-N (chunk minimum size) when starting memcached to divide the memory size by formula chunk size = 80*f* (n-1) Allocates memory as several slab class.
Question: How much is this number on the top?
We can determine according to F,n, and a slab maximum of 1M. (example, I don't give up, think about it myself)
4.2.2, pre-distribution
Allocate available memory with the-m parameter for memcached when starting memcached (assuming-m 1024, which allocates 1G of memory), but does not allocate all of the memory at boot time, but allocates several slab by default first Class (the number depends on the-F and-n parameters), when one of the slab classes is exhausted, memcached will reapply for 1M space to produce a slab class. This together with the LRU algorithm in the cache removal mechanism is seen. (If this piece is wrong, ask the great God to help point it out)
5. Cache deletion mechanism
- Memcached does not release allocated memory, and after the record times out, its storage space can be reused
- memcached internal does not monitor whether the cache is out of date (that is, memcached does not consume CPU time on expired monitoring), check the cached timestamp on Get, and check that the cache is out of date
- Memcached takes precedence over the cached space that has timed out, but when all the space has not timed out and all the memory has been allocated, delete the least recently used (LRU) cache and allocate its space to the new cache
Note: The third article is combined with the pre-allocation of the Memory allocation section.
6. Two kinds of serialization protocols
- Text protocol:
- XML, JSON
- The length of the key is 256 bytes
- Binary protocol: Compared to text protocol
- JDK serialization mechanism, PROTOBUF
- No parsing of text protocols is required, faster
- With a longer key, theoretically maximum 65536-byte-length key can be used
- After 1.3, it is recommended to use
Note: For both of these protocols, choose your own.
- Binary protocol +JDK the serialization mechanism, so because the JDK's own serialization mechanism is inefficient, it is not necessarily faster than using the Fastjson text protocol.
- Binary protocol +protobuf, fast, but not very convenient to use.
- Text Protocol +fastjson
7. Some APIs
- Add: Save only if there is no data for the same key in the storage space
- Replace: Replaces. That is, save only if the same data exists in the storage space
- Set:add+replace. That is, no matter when it is saved
- Delete (key, ' blocking time (seconds) ')
- Increase 1, minus 1 operation, do counter
- Get_multi (Key1, Key2): One-time non-synchronous simultaneous (i.e. concurrent) acquisition of multiple keys, dozens of times times more than the loop call Getkia
Note the point:
- For memcached monitoring: "Nagios" can be used
The sixth chapter memcached analysis