Identifying count of open requests to each upstream for load balancing

22 views
Skip to first unread message

Ashley Lowde

unread,
Aug 9, 2022, 2:51:09 AM8/9/22
to openresty-en
Hi all,

I would like to be able to use lua-resty-balancer to load-balance between multiple upstream servers and preferentially select upstreams with fewer open connections.

It seems to me that it would be possible to maintain a count of connections to each server by incrementing a shared dict in a balancer_by_lua_block and decrementing in a log_by_lua_block. I can then select an upstream with fewest connections in the balancer_by_lua_block. 

Can anyone identify a reason why this would be a bad idea, or any better ways to achieve the same thing?

Thanks,
Ashley

BeeTimBee

unread,
Aug 10, 2022, 12:44:59 PM8/10/22
to openre...@googlegroups.com
Your approach sounds good to me, in fact I am looking at something similar (to solve a very different problem). Just be aware of this from the docs on balancer_by_lua:

"The Lua code handler registered by this directive might get called more than once in a single downstream request when the Nginx upstream mechanism retries the request on conditions specified by directives like the proxy_next_upstream directive."

You don't want to double-count a request under any condition. You might put the selected server into ngx.var and check for a previously selected upstream (in order to decrement before replacing during a retry). You could also use this variable in log_by_lua to know which server to decrement.
Reply all
Reply to author
Forward
0 new messages