apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.28k stars 2.49k forks source link

help request: Questions Regarding the `least_conn` Load Balancer #10992

Open diaosj opened 6 months ago

diaosj commented 6 months ago

Description

Hi,

I have some questions about the least_conn load balancing algorithm and would appreciate some clarification:

  1. I want to confirm my understanding: When using least_conn, the metric used is the number of connections in Nginx rather than the actual number of requests to the upstream servers. To achieve load balancing based on the number of requests, would it be necessary to configure Nginx to use a single worker and disable keepalive? Specifically, are these the configurations to adjust?

    nginx_config:
    worker_processes: 1
    enable_cpu_affinity: false
    worker_rlimit_nofile: 20480
    worker_shutdown_timeout: 240s
    event:
    worker_connections: 10620
    http:
    keepalive_timeout: 0s
    upstream:
      keepalive: 320
      keepalive_requests: 1000
      keepalive_timeout: 0s

    By doing so, would this effectively balance the load based on the number of requests received by the upstream servers?

  2. Upon reviewing the code for the least_conn algorithm, I noticed that the weight is updated in the after_balance() method, which is called during the http_log_phase. init.lua

    if api_ctx.server_picker and api_ctx.server_picker.after_balance then
        api_ctx.server_picker.after_balance(api_ctx, false)
    end

least_conn.lua

            local info = servers_heap:valueByPayload(server)
            info.score = info.score - info.effect_weight
            servers_heap:update(server, info)

At this stage, has the request/connection already been released? Could updating at this time cause a brief inaccuracy in the connection count? Might this lead to a slight imbalance in the distribution of a small number of requests?

Thank you for your time and assistance.

Environment

theweakgod commented 6 months ago
  1. To achieve load balancing based on the number of requests, would it be necessary to configure Nginx to use a single worker and disable keepalive

least active connection.

shreemaan-abhishek commented 5 months ago

To achieve load balancing based on the number of requests, would it be necessary to configure Nginx to use a single worker and disable keepalive? Specifically, are these the configurations to adjust?

I'm saying this by my obvious reasoning that you don't have to do this to make it work. If least-conn was supposed to be used this way, it would make no sense.

diaosj commented 5 months ago

To achieve load balancing based on the number of requests, would it be necessary to configure Nginx to use a single worker and disable keepalive? Specifically, are these the configurations to adjust?

I'm saying this by my obvious reasoning that you don't have to do this to make it work. If least-conn was supposed to be used this way, it would make no sense.

Thank you for your response.

May I clarify if this implies that the least_conn algorithm is independent of the keepalive setting? In my understanding, if keepalive connections are reused, wouldn't this result in an inaccurate count of connections for each node in the upstream? Greatly appreciate your insight on this matter.

theweakgod commented 5 months ago

wouldn't this result in an inaccurate count of connections for each node in the upstream

least active connection. When the pool has an idle connection, the least active connection policy will be satisfied. Worker will count the upstream with the smallest number of active connections. Connection will not close if keepalive and http request with header connection: keep-alive. Worker will monitor the status of this connection in pool.