Chapter 6 memcached analysis and memcached Analysis

Source: Internet
Author: User

Chapter 6 memcached analysis and memcached Analysis

Note: This blog is based on two books.

  • Memcached comprehensive analysis, the book on the market should not, I passed to Baidu cloud disk, the link is as follows: http://pan.baidu.com/s/1qX00Lti
  • Technical Architecture of large websites: Core Principles and case analysis

Prerequisites:

  • This article is based onMemcached1.4The previous version is different from this version in some places (for example, memcached comprehensive analysis, memcached1.2's memory management method is different from 1.4)
  • Before reading this article, you 'd better take a look at how memcached operates in actual development. For more information, see Chapter 8 Enterprise Project Development-distributed cache memcached.

1. memcached features

  • Simple protocol (text protocol and binary Protocol)
  • Based on libevent event processing, libevent encapsulates the time processing function of Linux epoll model.
  • Slab Storage Model
  • The servers in the cluster do not communicate with each other (in the case of a large cluster, the performance of memcached is far higher than that of other caches that synchronously update the cache. Of course, the performance of memcached in a small cluster is also very good)

 

2. memcached Access Model

Note:

For details about Xmemcached usage code, refer to Chapter 8 Enterprise Project Development-distributed cache memcached in the "Java Enterprise project development practices" blog series. The following explanations are based on the code.

In, the memcached client assumes that XMemcached is used

  • Server LIST: configured in the root pom. xml file
  • There are two routing algorithms: (you can specify them in the Program)
    • Consistent hash algorithm (recommended)
    • Simple remainder Method
  • Communication Module:
    • Communication Protocol: TCP
    • Serialization Protocol: Binary protocol (recommended) and text protocol
  • Memcached API (add, delete, modify, and query cache): write in a program

Entire Process:

The application (AdminService) calls the Memcached API (for example, the add operation) to add a cache to the memcached server. At this time, the program will first follow the configured routing algorithm (for example, the consistent hash algorithm) select a server (assuming node1) from the server list and then serialize the object through the serialization protocol (of course, this is dispensable, for example. value is a String), and the key-value pair to be stored in the corresponding server through the TCP protocol. In get, as long as the same hash algorithm is used as in add, the server at add is selected.

After reading this section, the process is clear. But there are several questions:

  • How are two routing algorithms implemented? Why is consistent hash algorithm used?
  • How can I store the cache when it reaches the server? (Slab Memory Model)
  • When the cache exceeds a certain capacity, what policy is used for automatic deletion of the cache and how is it deleted? (LRU)
  • What are the advantages and disadvantages of the two serialization protocols?

 

3. hash Algorithm

3.1 simple remainder Method

Principle step: Obtain the integer hash Value of the key (for Java objects, use its hashCode () method directly), divide it by the number of servers, obtain the remainder, and select the server based on the remainder.

Note: If the selected server cannot be connected, rehash is performed, that is, the number of connections is added to the key, the hash value is recalculated, and then the connection is reconnected. Of course, rehash can be disabled.

Advantages:

  • Simple
  • Hash is well dispersed (because the value of hashCode () is random)

Disadvantages:

  • When a server is added or deleted, a problem occurs when the cache is obtained (because the number of servers changes, the denominator changes when the remainder is obtained, and the remainder may change ), assume that one of the 99 memcached servers is added, the cache hit rate is 99%, that is, n/(n + 1), and n indicates the original server.

Note:

  • The algorithm is retained in XMemcached.
  • This method is applicable when you do not need to consider cluster Scalability (that is, the total number of machines remains unchanged)

3.2 consistent hash Algorithm

For most systems, the scalability of clusters is one of the five non-functional requirements. That is to say, the disadvantages of the simple method of remainder must be overcome.

The best choice for 150 virtual servers is that too few will cause uneven loads, and too many will affect performance.

  • Memcached uses this algorithm. When we add a new server or a server in the cluster to go down, it will not have much impact, but it will only affect a small segment (see ), ensures cluster availability and scalability
  • Note:

    • The hash ring is a binary tree, and the last side of the leaf is connected to the leftmost side.
    • The entire cache search process is to find a minimum value that is just greater than or equal to the number of searches.

    Question: (no information is found in this workshop)

    • What is the server's hash algorithm?
    • Whether the hash algorithm used to calculate the cache key must be consistent with the server, and whether the original hashCode () can be used ()

     

    4. slab Memory Model

    4.1. Why is the slab Memory Model Used?

    At the beginning, memory allocation and recovery are handled through malloc and free, which will generate memory fragments, increase the burden on the memory manager, and seriously affect the efficiency of cache operations.

    The slab model is used:

    • Improves cache Operation Efficiency
    • Completely solves the memory fragmentation problem.

    Note:

    • The first objective has been achieved (because it will soon be possible to directly locate the appropriate chunk)
    • Objective 2: slab mechanism will still generate memory fragments, or a waste of memory.

    4.2 slab model principles

    Note: This figure is taken from a blog (marked but not clearly visible in the figure), but it was picked a long time ago and forgotten. Later, I will mark the source.

    Memcached memory allocation is the following statement:Group Management,Pre-allocationMethod.

    4.2.1 Group Management

    • Group mode: Memcached divides the memory space into a group of slab. The size of each slab is fixed to 1 MB, and each slab contains a group of chunks. The size of each chunk in the same slab is the same. According to the chunk size in the slab, the slab numbers slab class (that is, the Classes I in ).
    • Storage principle: when a key-value pair is to be stored, we can view the data size and select the idle chunk in the most suitable slab class to place the object.
      • The most suitable chunk: that is, the chunk size is just greater than or equal to the size of the stored data, and the size of a level smaller than the chunk is just smaller than the data to be stored.

    This method will cause a large amount of memory waste (I think this is also memory fragmentation ).

    • Reduce memory waste: estimate the size of your cache data, and then specify the parameter-f (growth factor) and-n (minimum chunk size) when starting Memcached) according to the formula chunk size = 80 * f * (n-1), the memory is allocated to several slab classes.

    Q: What are the numbers above?

    We can determine the maximum value of f, n, and an slab is 1 MB. (For example, if you don't want to mention it, think about it)

    4.2.2 pre-allocation

    When starting Memcached, use the-m parameter to allocate available memory for Memcached (assuming-m 1024, that is, 1 GB memory is allocated). However, when starting Memcached, the memory is not allocated all at once, instead, several slab classes are allocated by default (the number depends on the-f and-n parameters). When one slab class is used up, Memcached will apply for another 1 MB space, generate an slab class. This is combined with the LRU algorithm in the cache deletion mechanism. (If this part is incorrect, please help us to point it out)

     

    5. cache deletion Mechanism

    • Memcached does not release allocated memory. After the record times out, its storage space can be reused.
    • Memcached does not internally monitor whether the cache expires (that is, memcached does not consume CPU time on the expired monitoring). Check the cache timestamp during get and check whether the cache expires.
    • Memcached preferentially uses the cache space that has timed out. However, when no space has timed out and all memory has been allocated, it deletes the least recently used (LRU) cache, allocate its space to the new cache

    Note: The third part should be combined with the pre-allocation of the memory allocation part.

     

    6. Two serialization protocols

    • Text protocol:
      • XML, JSON
      • The key is 256 bytes in length.
    • Binary Protocol: Compared with text protocol
      • Jdk serialization mechanism, protobuf
      • Text protocol Parsing is not required, which is faster
      • A key with a longer length. Theoretically, a key with a maximum length of 65536 bytes can be used.
      • It appears in 1.4. We recommend that you use

    Note: You can select the two Protocols.

    • Binary protocol + JDK serialization mechanism, because JDK's own serialization mechanism is inefficient, the speed may not be faster than the text protocol using fastjson.
    • Binary protocol + protobuf, fast, but not easy to use
    • Text protocol + fastjson

     

    7. Some APIs

    • Add: Save only when data with the same key does not exist in the bucket
    • Replace: replace. That is, it is saved only when the same data exists in the bucket.
    • Set: add + replace. That is, save it at any time
    • Delete (key, 'blocking time (seconds )')
    • Add 1, subtract 1, and counter
    • Get_multi (key1, key2): multiple keys are obtained at the same time (that is, concurrently) when one-time non-synchronization is performed, dozens of times better than getaskmethod called cyclically.

     

    Note:

    • Memcached monitoring: You can use "nagios"

    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.