Understand targets' weights

A target's weight determines how much traffic it will receive.


For the examples here, we'll continue with the HTTP load balancer from Create a HTTP load balancer. This assumes three web servers as targets, each one serving a web page with the server name in the <title> tag.

If we had assigned the first server—web1—a weight of five, while the other two servers each had a weight of one, the first server would receive five requests while the other two would each receive one request.

We run the test script below again.

test-lb.sh
#!/bin/bash
x=1
while [ $x -le 15 ]
do
  curl -s http://203.0.113.155 | grep "<title>"
  x=$(( $x + 1 ))
done

Running the script will, in this example, where the first server has a weight of five, output the following (or similar to it):

Example output
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 3</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 3</title>
  <title>Web Server 1</title>
  <title>Web Server 1</title>
  <title>Web Server 2</title>

If we instead lower the weight of the first server to two, that server will receive two requests while the other servers will each receive one. A new test confirms this to be correct:

Last updated

Was this helpful?