Principle of Distributed Cache--memcache

Source: Internet
Author: User
Tags memcached


content :1. What is memcached

the difference between 2.MemCache and memcached

3.memcache Access Model

4.Memcached as a distributed cache server with high-speed operation has the following characteristics

5.Memcached of memory algorithms

            6 memcached's cache policy

            7. distributed algorithm (consistent Hashing)

8. Summary of features and limitations of Memcache


1. What is memcached
        memcache is a free, open source, high performance, distributed, distributed memory object caching system for dynamic Web applications to reduce the load on the database. It improves the speed of website access by caching data and objects in memory to reduce the number of times a database is read.   Memcache is a hashmap that stores key-value pairs, in memory for arbitrary data (such as strings, objects, etc.) used by Key-value storage, data can come from database calls, API calls, or page rendering results. Memcache design concept is small and powerful, its simple design facilitates rapid deployment, easy to develop and solve many challenges facing large-scale data cache, and the open API enables Memcache to be used in Java, c/c++/c#, Perl, Python, PHP, Most popular programming languages like Ruby.

Many Web applications save data to an RDBMS, where the application server reads the data and displays it in the browser. But with the increase of data volume, the concentration of access, there will be Rebms burden, database response deterioration, site display delay and other significant impact. Memcached is a high-performance distributed memory cache server. The purpose of the general use is to reduce the number of database accesses by caching database query results, so as to improve the speed and expansibility of dynamic Web applications.


The difference between 2.MemCache and memcached:

1, Memcache is the name of the project

2, memcached is Memcache server can execute the name of the file


3.memcache Access model

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/87/26/wKiom1fV6rewEf1BAAEKuSzMIzw678.jpg "title=" 56e8c992e136f_middle.jpg "alt=" Wkiom1fv6rewef1baaekuszmizw678.jpg "/>

At the same time, based on this graph, the process of memcache once write cache:

1. Application input requires write-cached data

2, API will key input routing algorithm module, routing algorithm based on key and memcache Cluster Server list to get a server number

3, the server number to get memcache and its IP address and port number

4, the API calls the communication module and the specified number of server communication, writes data to the server, completes a distributed cache write operation

Read cache and write cache, as long as the same routing algorithm and server list, as long as the application query is the same Key,memcache client always access the same client to read the data, as long as the server also caches the data, can guarantee the cache hit.

This way of Memcache cluster is also from the aspect of partition fault tolerance, if Node2 down, then Node2 stored on the data are not available, at this time because the cluster Node0 and Node1 still exist, the next request Node2 stored in the key value, Must be no hit, then first get the data to be cached from the database, and then the routing algorithm module according to the key value in Node0 and Node1 Select a node, the corresponding data into the next time you can go cache, this cluster approach is very good, but the disadvantage is the cost is relatively large.


4.Memcached as a distributed cache server with high-speed operation has the following characteristics.

The protocol is simple : memcached Server client communication does not use a complex MXL format, but rather uses a simple text-based protocol.

event handling based on Libevent : Libevent is a library that encapsulates the time-processing functions of Linux epoll, BSD-like operating systems, and so on, into a unified interface. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems.

built-in memory storage : To improve performance, the data saved in memcached is stored in Memcached's built-in memory storage space. Since the data only exists in memory, restarting memcached and restarting the operating system will cause all data to disappear. Additionally, when the content capacity reaches the specified value, memcached automatically deletes the non-applicable cache.

memcached non-interoperable distributed : memcached Although it is a "distributed" cache server, there is no distributed functionality on the server side. Each memcached does not communicate with each other to share information. His distribution is mainly implemented through the client.


5.Memcached of memory algorithm:
memcached uses the slab allocation mechanism to allocate and manage memory, which divides the allocated memory into chunks of memory of a certain length according to a predetermined size, then divides the same memory blocks into groups, and the data is stored, matching the slab size according to the size of the key value, Find the nearest slab storage, so there is space waste phenomenon.
The traditional way of memory management is to use the memory allocated through malloc to reclaim memory through free, which is prone to memory fragmentation and reduce the operating system's memory management efficiency.

Memcached According to the size of the data received, select the most appropriate data size slab (Figure 2) Memcached in the slab to save the list of idle chunk, based on the list to select Chunk, The data is then cached in it.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/87/26/wKiom1fV7NvQKmenAAB8_f1xGko435.jpg "title=" 153423331.jpg "alt=" Wkiom1fv7nvqkmenaab8_f1xgko435.jpg "/>


6. Cache policy for memcached:
the memcached cache policy is the LRU (least recently used) plus the expiry expiration policy. When you store data items in a memcached, you may specify that it will expire at cache time, and default to permanent. When the memcached server runs out of allocations, the invalidated data is replaced first, and then the data that is not used recently. In LRU, Memcached uses a lazy expiration policy that does not monitor the expiration of the key/vlue being deposited, but instead checks the timestamp of the record when it gets the key value, checking to see if Key/value is out of space, which reduces the load on the server.


      7. distributed algorithm (consistent Hashing):

  when depositing/removing Key/value to the memcached cluster, the memcached client program calculates which server to store on a certain algorithm and then saves the Key/value value to this server. In other words, access to data in two steps, the first step, select the server, the second step to access data.   

      remainder algorithm:
    first evaluates the integer hash value of the key, divided by the number of servers, and determines the access server based on the remainder. This method is simple and efficient, but when the memcached server is increased or decreased, almost all of the caches will fail.
      hashing algorithm:
    calculates the hash value of the memcached server first, and distributes it to 0 to 2 of the 32 square of the circle, and then the same method to calculate the key to store the hash value of the data and map to the circle, and finally from the data map to the position of the first to find, save the data to find a server, if more than 2 32 times, still cannot find the server, Save the data to the first memcached server. If you add a memcached server, the keys on the first server that only increase the counter-clockwise direction of the server on the circle will be affected.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/24/wKioL1fV7cyR3JZJAABdL_YkxAE380.jpg "title=" 152344672.jpg "alt=" Wkiol1fv7cyr3jzjaabdl_ykxae380.jpg "/>


8. Summary of features and limitations of Memcache:

The above has done a more detailed interpretation of memcache, here again summarizes the limitations and characteristics of memcache:

1. There is no limit to the amount of item data that can be saved in memcache, as long as the memory is sufficient

2, Memcache single process in 32-bit machine The maximum use of memory 2G, this previous article mentioned several times, 64-bit machine is not limited

3, key Max is 250 bytes, more than this length can not be stored

4, single item maximum data is 1MB, more than 1MB of data is not stored

5, Memcache server is not secure, such as a known memcache node, you can telnet to the past, and through the flush_all to let existing key value pairs immediately expire

6. It is not possible to traverse all the item in Memcache because the operation is relatively slow and will block other operations

7, Memcache high-performance from the two-stage hash structure: the first stage in the client, the hash algorithm based on the key value of a node, the second stage on the server, through an internal hash algorithm, find the real item and return to the client. From the implementation point of view, Memcache is a non-blocking, event-based server program

8, Memcache set to add a key value, the incoming expiry 0 means that the key value is permanently valid, the key value will expire after 30 days,



This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1851819

Principle of Distributed Cache--memcache

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.