Advanced Settings
By using "Advanced Settings" in Arvancloud CDN, you can manage user traffic and load balancing behavior more precisely. You can access these features after selecting the domain in Arvancloud's CDN panel in the "Traffic" section and then "Advanced Settings".
Load Balancing type
In this section, you can specify the algorithm based on which an address is selected from the addresses of the origin servers.
Round Robin Algorithm
This algorithm is the simplest and most common load balancing algorithm. User requests are distributed between your main servers in a simple rotation. For example, imagine you have 6 users (u1, u2, u3, u4, u5, and u6) who want to connect to your application and you have 3 servers (s1, s2, and s3). u1 is connected to s1, u2 to s2 and u3 to s3, then u4 to s1, u5 to s2 and u6 to s3. Round Robin algorithm is suitable for predictable requests and servers with relatively equal processing power and resources.
But if one of the servers has more capacity to handle requests than the others, you can use the maximum power of that server with weighting.
Client IP Hash Algorithm
Client IP Hash is an algorithm that sends user requests to a specific server based on their IP address.
When a user sends a request, the Load Balancer receives it first. The Load Balancer then uses the user's IP address in a hash function. The properties of this Hash function can be different. For example, it might convert the last octet of the IP address to an integer and perform a mod operation on the number of servers to get the server index. In fact, the hash function determines which server should process the request based on the Hash of the user's IP address.
As long as the number of servers remains constant and the IP does not change, all future requests from that IP address will be directed to the same server.
This algorithm guarantees the session continuity. This means that as long as the IP address of a user does not change, they will continuously connect to the same server during the session. This feature is effective in situations where it is important to keep session information about the client and on a specific server.
Sending User Request to the Next Server in Case of an Error
By activating this feature, if your subdomain is connected to more than one server, requests that encounter Connection Refused, Connection Error, etc. errors will be automatically transferred to the next server, and in this case, the main server error will not be displayed to the user.
This feature is available in Growth plans and above.
Stop Sending Traffic to Faulty Server
By enabling this, by default, if the number of errors of a server exceeds 5, that server will be removed from the list of servers to which traffic is sent for 15 seconds.
The number of errors and the length of time that traffic is not sent to the server with an error can be changed in the settings section.
This feature is available in Growth plans and above.
Maintaining Connection with the Origin Server
By enabling this feature, connections established with the origin server are kept open for use in subsequent requests. As a result, the number of connections made with the origin server is reduced.
In HTTP/1.0, a new connection is established and terminated for each Request-Response pair. This process greatly increases the load on the servers.
But in HTTP/1.1, the Keep-Alive Connection feature has become the default; That is, after the client communicates with the server, the same connection is used for subsequent requests. This reduces the cost of creating a new connection for each Request-Response pair.
Overall, Keep-Alive Connection greatly improves the performance of websites by reducing connection latency and saving CPU usage.
Using API
To configure and manage the advanced Arvancloud CDN load balancing settings through API, you can use the following sample request:
curl --location --request PATCH 'https://napi.arvancloud.ir/cdn/4.0/domains/arian.run/load-balancers/settings' \
--header 'authority: napi.arvancloud.ir' \
--header 'accept: application/json, text/plain, */*' \
--header 'authorization: API KEY' \
--header 'Content-Type: text/plain' \
--data '{"method":"cluster_chash","next_upstream_tcp":"on","protocol":"https","keepalive":"on","max_fails":5,"fail_timeout":"10s"}'
- In this request, the key
method
is used to manage the type of load balancing and accepts the valuecluster-rr
for the Round Robin algorithm and the valuecluster-chash
for the Client IP Hash algorithm. - To manage the "Sending User Request to the Next Server in Case of an Error" feature, the
next_upstream_tcp
key can receive two values:on
andoff
. - "Stop Sending Traffic to Faulty Server" settings is managed using two keys
max_fails
andfail_timeout
, which accept a positive integer and a time value in seconds, respectively. - For "Maintaining Connection with the Origin Server" you must use the
keepalive
key. This parameter can receive two valueson
oroff
.