When using Haproxy, balance is used in the backend segment to indicate how the backend server is scheduled to be implemented.
Balance: Indicating scheduling algorithm
Dynamic algorithm: Weights can be dynamically adjusted
Static algorithm: Adjustment weights do not take effect in real time, only restart to take effect
Balance algorithms are: ROUNDROBIN,STATIC-RR,LEASTCONN,SOURCE,URI,URI_PARAM,HDR, etc.
Roundrobin algorithm:
Polling, polling based on weights, this algorithm is dynamic, each back-end server can only accept up to 4,128 connections;
Requires the backend can not have status and session aspects, etc., can only be static pages
When the backend has a session and other dynamic information, combined with the use of cookies, the best effect
STATIC-RR algorithm:
Static Roundrobin, does not support dynamic adjustment, static algorithm, the number of each back-end host support is not capped;
Leastconn algorithm:
The new connection request is dispatched to the backend server with the least number of connections based on the number of load on the backend host;
This method is recommended in scenarios with long-connected sessions, not HTTP, dynamic algorithms
SOURCE algorithm:
The source address of the request is hashed and distributed to a matching server by dividing the total number of weights from the backend server, which allows the same IP request to be sent to the same server; However, when the total number of server weights changes, such as when a server goes down or a new server is added, Many client requests may be distributed to servers that are different from the previous request, and are commonly used for TCP-based protocols that load-balance without cookies, which are implicitly static, but can also be modified using Hash-type;
Hash-type:
Map-based: Modulus method (except for modulus-of-rest method); (Static, default)
Consistent: consistent hashing; (dynamic, makes it necessary to specify after: Hash-type consistent)
URI algorithm:
Hash the left half of the URI (the part before the "question mark" tag) or the entire URI and distribute it to a matching server by dividing it by the total weight of the server; This allows requests for the same URI to always be dispatched to a particular server, unless the server's total weight has changed This algorithm is commonly used for proxy caches or anti-virus proxies to increase cache hit ratios
Example:http://www.baidu.com/abc/query? item=123 #红色标记的部分是uri部分
Hash-type:
Map-based: Modulus method (except for modulus-of-rest method); (Static, default)
Consistent: consistent hashing; (dynamic, makes it necessary to specify after: Hash-type consistent)
Note: When a URI is used, all subsequent requests to this page are sent to the server after a page is accessed
When the end of a page is not accessed, using the Roundrobin algorithm, randomly pick a response, and then all the requests for this page are sent to this host, this algorithm, when the backend cache is very effective!!!!
The Roundrobin algorithm combines the use of cookies:
The role of cookies is the basic browser implementation session sticky;
How to use cookies: Cookies <name> [Rewrite|insert|prefix] [indirect] [NoCache]
[Rewrite|insert|prefix]: How to add cookies
Rewrite: Replace the original cookie
Insert: Inserts the #此种方式最为常用 after the original cookie
Prefix: Insert before the original cookie
How cookies are used:
Cookie ServerID Insert Indirect nocache
Note:
A.serverid, is his own casually designated, in the following example is Serverid=websrv1,serverid=websrv2;insert: To insert the original cookie behind
B. Do not bind according to the source IP, according to the different client binding, this way I feel better than source, because source is the same IP to the same back-end server, according to different client bindings, you can implement the same public IP user, the request sent to a different backend
The c.roundrobin algorithm, combined with cookies, can be attempted, which makes up for the disadvantage that Roundrobin does not maintain conversation
Usage examples:
Balance Roundrobin
Cookie ServerID Insert Indirect
Server Web1 192.168.0.100 Check weight 1 cookie websrv1
Server web2 192.168.0.101 Check weight 3 cookie Websrv2
Note: websrv1: is a web1 proprietary cookie identifier that can be seen in a cookie in the client browser
WEBSRV2: is a web2 proprietary cookie identifier that can be seen in a cookie in the client browser
The most common algorithms are:
Roundrobin
Source
Uri
This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1927975
Haproxy Scheduling algorithm