Memcached comprehensive analysis-4. memcached distributed algorithm-PHP Tutorial

Source: Internet
Author: User
Tags crc32 rehash perl script
Memcached comprehensive analysis-4. memcached distributed algorithm. Original article link: gihyo. jpdevfeature01memcached0004 this article series links here: 1st times: www.phpchina.comhtml29n-35329.html 2nd times: www. php original article link: http://gihyo.jp/dev/feature/01/memcached/0004

Here is the link to this series of articles:

  • 1st times: http://www.phpchina.com/html/29/n-35329.html
  • 2nd times: http://www.phpchina.com/html/30/n-35330.html
  • 3rd times: http://www.phpchina.com/html/31/n-35331.html
  • 4th times: http://www.phpchina.com/html/32/n-35332.html
  • 5th times: http://www.phpchina.com/html/32/n-35333.html

I am Nagano of Mixi. 2nd and 3rd times the internal situation of memcached was introduced by Qian ban. This article will not introduce the internal structure of memcached, but will introduce the distributed memcached.

  • Distributed memcached
    • What does memcached mean by its distributed architecture?
  • Cache: Memcached distributed method
    • Scattered based on remainder calculation
    • Disadvantages of scattered calculation based on remainder
  • Consistent Hashing
    • A brief description of Consistent Hashing
    • Function libraries supporting Consistent Hashing
  • Summary
Distributed memcached

As described in 1st, although memcached is called a "distributed" cache server, the server does not have a "distributed" function. The server only includes the memory storage features described in 2nd and 3rd times, which is very simple to implement. The distributed memcached is fully implemented by the client library. This type of distribution is the biggest feature of memcached.

What does memcached mean by its distributed architecture?

The term "distributed" is used many times, but it is not explained in detail. Now, let's briefly introduce the principle of the solution. the implementation of each client is basically the same.

Assume that the memcached server has node1 ~ Three node3 servers. The application will save the data with the key name "tokyo" "kanagawa" "CHBA" "saitama" "gunma.

Distributed introduction: Preparation

First, 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.

Distributed introduction: When adding

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.

Distributed introduction: Get

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.

Next we will introduce the distributed method of the Perl client function library Cache: Memcached implementation mentioned in 1st.

Cache: Memcached distributed method

Perl's memcached client function library Cache: Memcached is the work of Brad Fitzpatrick, creator of memcached. it can be said that it is the original function library.

  • Cache: Memcached-search.cpan.org

This function library implements distributed functions and is a standard distributed method for memcached.

Scattered based on remainder calculation

Cache: The distributed method of Memcached is simply to say, "distribution based on the remainder of the number of servers ". Calculate the integer hash value of the key, divide it by the number of servers, and select the server based on the remaining number.

The Cache: Memcached is simplified to the following Perl script.

Use strict; use warnings; use String: CRC32; my @ nodes = ('node1', 'node2', 'node3 '); my @ keys = ('Tokyo ', 'kagawa ', 'kiba', 'saitama ', 'gunm'); foreach my $ key (@ keys) {my $ crc = crc32 ($ key ); # CRC nodes my $ mod = $ crc % ($ # nodes + 1); my $ server = $ nodes [$ mod]; # select the server printf "% s => % s \ n", $ key, $ server;} based on the remainder ;}

Cache: Memcached uses CRC when calculating the hash value.

  • String: CRC32-search.cpan.org

First, obtain the CRC value of the string. the server is determined by dividing the CRC value by the remainder of the number of server nodes. After the above code is executed, enter the following results:

tokyo => node2 kanagawa => node3 chiba => node2 saitama => node1 gunma => node1

According to this result, "tokyo" is distributed to node2, and "kanagawa" is distributed to node3. When the selected server cannot be connected, Cache: Memcached adds the number of connections to the key, computes the hash value again, and tries to connect. This action is called rehash. If you do not want rehash, you can specify the "rehash => 0" option when generating the Cache: Memcached object.

Disadvantages of scattered calculation based on remainder

The remainder calculation method is simple and data dispersion is excellent, but it also has its disadvantages. That is, when a server is added or removed, the cost of cache reorganization is huge. After a server is added, the remainder will change dramatically, so that the server that is the same as the storage cannot be obtained, thus affecting the cache hit rate. Use Perl to write code segments to verify the cost.

use strict; use warnings; use String::CRC32; my @nodes = @ARGV; my @keys = ('a'..'z'); my %nodes; foreach my $key ( @keys ) { my $hash = crc32($key); my $mod = $hash % ( $#nodes + 1 ); my $server = $nodes[ $mod ]; push @{ $nodes{ $server } }, $key; } foreach my $node ( sort keys %nodes ) { printf "%s: %s\n", $node, join ",", @{ $nodes{$node} }; }

This Perl script demonstrates how to save the key "a" to "z" to memcached and access it. Save it as mod. pl and execute it.

First, when there are only three servers:

$ mod.pl node1 node2 nod3 node1: a,c,d,e,h,j,n,u,w,x node2: g,i,k,l,p,r,s,y node3: b,f,m,o,q,t,v,z

The result is as follows: node1 stores a, c, d, e ......, Node2 stores g, I, k ......, Each server stores 8 to 10 data records.

Next we will add a memcached server.

$ mod.pl node1 node2 node3 node4 node1: d,f,m,o,t,v node2: b,i,k,p,r,y node3: e,g,l,n,u,w node4: a,c,h,j,q,s,x,z

Node4. It can be seen that only d, I, k, p, r, and y are hit. In this way, after adding nodes, the keys distributed to the server will change dramatically. Only six of the 26 keys are accessing the original server, and all others are moved to other servers. The hit rate is reduced to 23%. When memcached is used in Web applications, the cache efficiency will be greatly reduced when the memcached server is added, and the load will be concentrated on the database server, which may cause failure to provide normal services.

This problem also exists in the use of mixi Web applications, resulting in the inability to add memcached servers. However, with the new distributed method, you can easily add memcached servers. This distributed method is called Consistent Hashing.

Consistent Hashing

The idea of Consistent Hashing has been introduced in many places, such as the development blog of mixi Corporation. here we only briefly describe it.

  • Mixi Engineers 'blog-wide spread faster than ever before.
  • ConsistentHashing
A brief description of Consistent Hashing

Consistent Hashing is as follows: first obtain the hash value of the memcached server (node) and configure it to 0 ~ 232 of the circle (continuum. Then, use the same method to obtain the hash value of the key for storing the data and map it to the circle. Search clockwise from the location where the data is mapped, and save the data to the first server. If more than 232 still cannot find the server, it will be saved to the first memcached server.

Consistent Hashing: Basic principle

Add a memcached server from the status. The residual number distributed algorithm affects the cache hit rate because the server that saves the key changes significantly. However, in Consistent Hashing, the keys on the first server that is added to the continuum server in a counterclockwise direction will be affected.

Consistent Hashing: add a server

Therefore, Consistent Hashing minimizes key redistribution. In addition, some Consistent Hashing implementation methods also adopt the idea of virtual nodes. If a common hash function is used, the server's ING locations are unevenly distributed. Therefore, the idea of virtual nodes is used to allocate 100 ~ 200 points. In this way, the distribution will be restrained unevenly, minimizing the cache redistribution when servers increase or decrease.

The memcached client function library using the Consistent Hashing algorithm described below is tested by the number of servers (n) and the number of servers (m) the formula for calculating the hit rate after adding a server is as follows:

(1-n/(n + m) * 100

Function libraries supporting Consistent Hashing

Although Memcached does not support Consistent Hashing, several client function libraries support this new distributed algorithm. The first memcached client function library that supports Consistent Hashing and virtual nodes is the PHP library named libketama, developed by last. fm.

  • Libketama-a consistent hashing algo for memcache clients-RJ has already existed-Users at Last. fm

As for the Perl client, Cache: Memcached: Fast and Cache: Memcached: libmemcached support for Consistent Hashing as described in the 1st serialization.

  • Cache: Memcached: Fast-search.cpan.org
  • Cache: Memcached: libmemcached-search.cpan.org

Both interfaces are almost the same as those of Cache: Memcached. If Cache: Memcached is being used, it can be conveniently replaced. Cache: Memcached: Fast implements libketama again. when using Consistent Hashing to create an object, you can specify the ketama_points option.

my $memcached = Cache::Memcached::Fast->new({ servers => ["192.168.0.1:11211","192.168.0.2:11211"], ketama_points => 150 });

In addition, Cache: Memcached: libmemcached is a Perl module that uses the C function library libmemcached developed by Brain Aker. Libmemcached supports several distributed algorithms and Consistent Hashing. its Perl binding also supports Consistent Hashing.

  • Tangent Software: libmemcached
Summary

This article introduces the memcached distributed algorithm, which is mainly implemented by the client function library and the Consistent Hashing algorithm that efficiently disperses data. Next, we will introduce some of mixi's experience in memcached applications and related compatible applications.

Copyright notice: AnyReprintedHowever, the original author charlee, original link, and this statement must be indicated during reprinting.

Links to this series of articles here: 1st times: http://www.phpchina.com/html/29/n-35329.html 2nd times: http://www.php...

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.