Nginx load balancing policy analysis [1] (round_robin module overview)

Source: Internet
Author: User
1. Basic Information

Module name: round_robin

File Location: ngx_http_upstream_round_assist.c

Running stage: content_phase

Rr policy as the Default policy

Each request is distributed to different backend servers one by one in chronological order. If the backend servers are down, they can be removed automatically.
For example:
Upstream upload ATS {
Server 10.1.1.107: 88 max_fails = 3 fail_timeout = 3 s Weight = 9;
Server 10.1.1.132: 80 max_fails = 3 fail_timeout = 3 s Weight = 9;
}

Upstream call: ngx_http_upstream.c ngx_http_upstream_ip_hash_module.c
Function: When nginx is used as a reverse proxy server, two kinds of shunting policies can be adopted for backend servers: weighted shunting and IP hash. This module mainly implements the weighted shunting function. For machines with higher weights, the selected machine has a high probability. For machines with the same weights, the round robin method is used.
Highlights:
1. Set the single field in the data structure to check whether there is only one backend server. If yes, you can directly return the traffic distribution policy module;
2. Both the failure count and failure time are set. If the maximum number of failures is reached, the server does not participate in traffic delivery within a period of time.
3. Use the backup server. The backup server is requested only when the existing Server is invalid.

2. Key Data Structure

Typedef struct {// basic socket information struct sockaddr * sockaddr; socklen_t socklen; ngx_str_t name; // The current weight value and the set weight value ngx_int_t current_weight; ngx_int_t weight; // number of failures and access time ngx_uint_t fails; time_t accessed; // upper limit of failure times and failure time threshold ngx_uint_t max_fails; time_t fail_timeout; // whether the server participates in the ngx_uint_t down policy; /* unsigned down: 1; * // SSL related # If (ngx_http_ssl) ngx_ssl_session_t * ssl_session;/* local to a process */# endif} ngx_http_upstream_rr_peer_t; // information of the backend server // round robin backend server information typedef struct ngx_http_upstream_rr_peers_s sequence; struct sequence {ngx_uint_t single; // whether the group has only one server ngx_uint_t number; // Number of backend servers of the group ngx_uint_t last_cached;/* ngx_mutex_t * mutex; */ngx_connection_t ** cached; ngx_str_t * Name; required * Next; // The next upstream node, that is, the pointer to the next server group. Next generally points to the backupserver (Backup Server group) ngx_http_upstream_rr_peer_t peer [1]; // The first server in the server group. If there are other servers, another peer will be applied continuously}; typedef struct {ngx_http_upstream_rr_peers_t * peers; // the pointer ngx_uint_t current for all Server Clusters; // The current server uintptr_t * tried; // The server bitmap pointer, used to record the current status of the server uintptr_t data; // The actual storage location of the tried bitmap} ngx_http_upstream_rr_peer_data_t;

3. Function Description

The main function list is as follows:

ngx_int_t   ngx_http_upstream_init_round_robin(ngx_conf_t *cf,    ngx_http_upstream_srv_conf_t *us)ngx_int_t   ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r,    ngx_http_upstream_srv_conf_t *us)ngx_int_t   ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,    ngx_http_upstream_resolved_t *ur)ngx_int_t   ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)static ngx_http_upstream_rr_peer_t *   ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)void   ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,    ngx_uint_t state)

References:

Analysis of nginx round_robin Module

Open source China ngx_http_upstream_round_assist.c

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.