Deep Learning about Memcached and memcached

Source: Internet
Author: User

Deep Learning about Memcached and memcached

Because Memcached distributed cache is used in a project in the lab, I have also learned about the distributed cache in depth during this time. This article will summarize my achievements and let's talk about what Memcached is.

What is Memcached?

Memcached is a high-performance distributed memory object cache system that can reduce the direct access to the database by the application system, reduce the database load, and improve the response speed of applications. Memcached can be compared to a huge storage space.

LRU Algorithm

This algorithm is also included in the page replacement algorithm of the operating system, which is very suitable for the cache system. It is based on the assumption that data with the lowest frequency has been used for the past period of time and will be rarely used in the future. The following is a cache implemented by myself based on the LRU algorithm. It implements the basic cache function, but one thing to improve is that the cache should be a singleton.

Package colin. cache;/***** @ author Colin Wang * Created on Apr 18,201 5 */public class LRUCache {private static final int DEFAULT_MAX_SIZE = 10; private Object [] elements; private int size; public LRUCache (int maxSize) {elements = new Object [maxSize];} public LRUCache () {this (DEFAULT_MAX_SIZE);} public boolean isEmpty () {return size = 0;} public int size () {return size;} public boolean I SFull () {return size = elements. length;}/*** return the position of the element in the array ** @ param element * @ return */public int indexOf (Object element) {for (int I = 0; I <size; I ++) {if (element. equals (elements [I]) {return I;} return-1;} public Object push (Object element) {int index =-1; if (! IsFull () & indexOf (element) =-1) {// The cache is not full and does not include the element elements to be inserted [size ++] = element ;} else if (isFull () & indexOf (element) =-1) {// The cache is full and does not contain the element to be inserted for (int I = 0; I <size-1; I ++) {elements [I] = elements [I + 1];} elements [size-1] = element ;} else {index = indexOf (element); for (int I = index; I <size-1; I ++) {elements [I] = elements [I + 1];} elements [size-1] = element;} r Eturn index =-1? Null: elements [index];} public void show () {for (int I = 0; I <size; I ++) {System. out. print (elements [I]. toString () + "");}}}
Consistent Hash Algorithm

This algorithm is widely used in distributed cache systems. Its key lies in the use of circular hash space. First, consider the implementation of HashMap in JDK. We know that HashMap has an initial capacity and load factor. When the number of elements in HashMap reachesInitial capacity * load factorIn such a large amount, HashMap will be resized (to 2 times the original size) and then hashed. When there are many elements, this re-hash operation actually consumes a lot of resources. Although HashMap uses a relatively optimized method, for example, the size of HashMap is always the n power of 2 (the default size is 16), which reduces the probability of hash collision (becauseN power of 2-1When converted to binary, all bits are 1, which prevents some bits from being ineffective. This increases the efficiency of HashMap when locating elements, for example:

static int indexFor(int h, int length) {    return h & (length-1);}

When length is always 2 to the Npower,h & (length - 1)The operation is equivalent to the modulo of length. However, these still cannot completely make up for the weakness that all elements must be re-hashed. For example, if we have N cache servers and all the hot data has been mapped to these N servers through the Hash algorithm, when one of the cache servers fails, or when we add several new cache servers, all the data that has been mapped will be invalid, and our backend database will bear this! Therefore, the consistent Hash algorithm comes in handy. As mentioned above, the consistent Hash algorithm uses a circular Hash space, as shown in:

The conventional Hash algorithm maps a key to a 32-bit integer. we connect the numbers between 0 and 2 ^ 32-1 to form a ring. Then, we map our cache server to this ring through a hash (hash of IP addresses or machine names), so that when an object is hashed to a location, we will cache this objectClockwiseThe cache server node closest to it. In this way, when one of the cache server nodes becomes invalid, it will not cause all the cache data to become invalid, you only need to cache the data originally cached on the node to the node nearest clockwise. When a new node is added, only a few pieces of data need to be hashed and mapped to the new node. In order to avoid uneven data distribution, for example, the vast majority of data is cached on one node, while other nodes cache little data, the consistent hash algorithm is also proposed.Virtual node. As shown in:

A1 and A2 are virtual nodes of node A. the hash value of the virtual node can be calculated using the IP address of the corresponding node plus A digital suffix, such as "192.168.1.1 #1 ". In this way, data that is hashed to A1 and A2 can be cached in node A to avoid unbalanced data distribution.

Related Article

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.