Analyze how the memcached client distributes cached data to multiple servers-2

Source: Internet
Author: User

Let's take a look at the following in the above methodCode:

Double-click all code
123456789 Int
Bucket = HV % _ buckets. count;  
If(Bucket <0)  Bucket + = _ buckets. count;
    Sockio sock = getconnection ((String) _ Buckets [Bucket]);

Select the serverAlgorithmYes: the hash value of the unique key value and the number of servers in the server list (the server address record is not unique) are used for modulus calculation to select the server address. Therefore, the data cached on that server depends on the hash value generated by the unique key value of the cached data and the number of servers in the server list, therefore, the same hash algorithm and the same server list configuration must be used to access all client operation data of the memcached service. Otherwise, data may not be obtained or accessed repeatedly. Because the unique keys of different data have different hash values, different data may be distributed to different servers to achieve load balancing among multiple servers.

If the load capacity of several servers is different, you can configure the load of each server based on the actual situation. Read the code above to knowProgramThe server address is obtained from _ buckets, and _ buckets stores the server address information. The server address is not unique in the _ bucket list and can be recorded repeatedly. The more duplicate records are recorded for the same server address in the _ bucket, the higher the chance of being selected, and the greater the load.

How to set the server to make it play a greater load, as shown in the following code:

Double-click all code
1234567891011 string [] serverlist = { "192.168.1.2: 11211 " ,
" 192.168.1.3: 11211 " };
int [] weights =
New int [] {5, 2};
sockiopool pool = sockiopool. getinstance ();
pool. setservers (serverlist);
pool. setweights (weights);
pool. initialize ();

The pool. setweights (weights) method is used to set the load factor of each server. The larger the coefficient, the greater the load. In the preceding example, the server load factor of 192.168.1.2 is set to 5, and the server load factor of 192.168.1.3 is 2, which means that the server load of 192.168.1.2 is greater than that of 192.168.1.3.

The program determines the cache Location Based on the hash value of the unique key identifier in the cached data and the modulo operation of the number of server records in the server list. Advantages of the algorithm: data can be evenly distributed to servers for load balancing. Of course, different servers can have different loads through configuration. However, there are also disadvantages: The data of the same type is too scattered, and the data of the same module is scattered to different data, so it is difficult to manage and protect data in a unified manner. For example: now, four servers A, B, C, and D are used as cache servers. After a few months, the C servers suddenly die and become unavailable, the data cached on the C server by algorithm is unavailable, but the client still obtains the operation data based on the algorithms of the original four servers, therefore, data distributed in service C is unavailable before server C becomes available. data must be read from the database and cannot be added to the cache, as long as the key of the cached data remains unchanged, it will still be calculated and allocated to the C server. If you want to allocate all data to server C, you must Initialize all data on servers A, server B, and server D and remove server C from the server list.

If we can classify and distribute data to each server, data of the same type will be distributed to the same server. For example, server a stores user log module information and server B stores user album module information, server C stores music module information, and server D stores basic user information. If the C server is unavailable, you can change the configuration so that it is stored on other servers without affecting the cache information of other servers.

Solution 1: different modules use different memcached client instances, so that different modules can be configured with Different server lists, so that the data of different modules is cached on different servers. In this way, when a server is unavailable, only the data of the corresponding memcached client instance will be affected, and the data of other client instances will not be affected.

Solution 2: modify or add a new algorithm and add a namespace to the unique data key. The algorithm selects different socket connections based on the configuration and the namespace in the unique data key, that is, the server.

The definition of the unique key of a data item: namespace. the data item ID is the same as the "namespace" in programming. For example, if the user has a log ID of "999999", the unique key of this log is SNS. userlogs. log.999999, of course, we consider performance issues during storage. We can use a short value to replace the namespace. In this way, you can select a socket based on the unique key in the data item.

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.