Author: Ayouurlhash is an architecture algorithm used to increase squid hit rate. Generally, the current architecture uses dns round robin or lvs to balance traffic load to several squids, in this way, the access volume of squid is balanced, but the data volume is ignored. In this architecture, although the data volume of each squid is consistent
Author: Ayou
Url hash is used to increase squIdHit rateArchitectureAlgorithm. The current architecture generally uses dns round robin or lvsServer Load balancerTo several squids, this can achieve a balanced access to squid, but ignores an important aspect-data volume. In this architecture, although the data volume of each squid is consistent, it is usually full load and data is cached repeatedly. If the data capacity of the backend server or the number of users' hot points of access far exceeds the memory capacity of the cache machine, or even the disk cache capacity configured, squid will use a large number of disks or keep requesting content from the backend servers.
In the new architecture, nginx is used before squid. If squid has four machines, nginx is installed on these four machines, and nginx uses port 80, change squid to port 3128 or another port. Nginx is very efficient and consumes a very small amount of memory. Therefore, you do not need to consider the performance loss caused by nginx installation. Then configure the url hash on nginx so that the traffic is evenly distributed to each squid Based on the url. After the traffic is distributed according to the url, each url will only exist in one squid, the data of each squid is completely different. We have four machines, each with 2 GB of memory, which may have been due to a large amount of data duplication, and the memory usage is still 2 GB. Now we are going through a balanced data distribution, 8 GB memory can be fully utilized.
Will there be uneven access? Yes, but according to the principle of large numbers, the access traffic can basically be consistent, as long as there is no single, especially exaggerated hot spot.
If squid uses squidclient to refresh data, the new architecture provides a more efficient method: Simulate the url hash algorithm on the backend server to find the squid of the content, then, refresh the content of the server. In the old architecture, it is inefficient to traverse all servers.
The specific configuration is as follows:
Nginx does not provide the url hash function (temporarily). You need to install the third-party module ngx_http_ups.TrEam_hash_moDuLe
Http://wiki.codemongers.com/NginxHttpUpstreamRequestHashModule? Action=AttachFile&do=get&target=nginx_upstream_hash-0.2.tar.gz
CdNginx-0.5.xx
Patch-P0 </path/to/upstream/hash/directory/nginx-0.5.xx.patch
./Add parameters when configuring configure
--Dd-Module = path/to/upstream/hash/directory
Make; make install
Complete installation
Configuration:
Add a hash statement to upstream. Other parameters such as weight cannot be written to server statements. hash_method is the hash algorithm used.
Upstream backend {
Server squid1: 3128;
Server squid2: 3128;
Hash $ request_uri;
Hash_method crc32;
}
The hash algorithm can use crc32 and the default simple, and java. util.Zip. CRC32 class implementation, the C language implementation of the simple algorithm is as follows
# Define ngx_hash (key, c) (u_int) key * 31 c)
U_int ngx_hash_key (u_char * data, size_t len)
{
U_int I, key;
Key = 0;
For (I = 0; I <len; I ){
Key * = 31;
Key = data [I];
}
Return key;
}
Java code (not tested with handwriting ):
PublicStatIc long getSimpleHash (String data)
{
Long key = 0;
Char [] chars = data. toCharArray ();
For (int I = 0; I key * = 31;
Key = (int) chars [I];
}
Return key;
}
Perform a remainder calculation on the generated key and the number of servers in the upstream to obtain the server number.
The purpose of providing the hash algorithm is to help the backend server quickly find the corresponding squid server.
There is a hash_again tag in the ngx_http_upstream_hash_module to solve the squid unexpected crash. However, if this label is used, the method of the backend computing server may be incorrect. You can use this method to provide a backup squid server. If squid crashes, set error_page 404 and 502 to this backup server in nginx, when the backend refreshes the cache, it also needs to fl the backup server.