All Products
Search
Document Center

Server Load Balancer:Use UDP health checks to detect unhealthy servers

Last Updated:Dec 17, 2025

UDP health checks can be used to improve service smoothness and user experience for UDP applications, such as online games and instant messaging. UDP health checks send UDP packets to probe backend servers. This feature can help you quickly identify potential issues and perform efficient troubleshooting to increase service liability and availability.

Scenario

An online game company runs multiple backend servers, which process game data of players over UDP. To ensure game smoothness, the company wants to monitor the health status of each server in real time. If a server becomes unhealthy, UDP health checks efficiently identify and isolate the server. Requests from players are no longer forwarded to the unhealthy server. When the server recovers, requests are automatically forwarded to the server.

To meet such requirements, the company can use UDP health checks of Network Load Balancer (NLB) to periodically probe each backend server. If UDP health checks identify an unhealthy server, NLB no longer forwards requests to the unhealthy server until it recovers. When an unhealthy server recovers, NLB automatically forwards requests to the server.

image

Prerequisites

  • A virtual private cloud (VPC) is created in the China (Hangzhou) region. A vSwitch is created in each of the two zones. For more information, see Create and manage a VPC.

  • An Elastic Compute Service (ECS) instance (ECS01) is deployed in VSW1 and another ECS instance (ECS02) is deployed in VSW2.

  • An NLB instance is created and running in the VPC. In this example, an internal-facing NLB instance is used. For more information, see Create and manage an NLB instance.

Usage notes

  • If UDP applications are deployed on your backend server, you can use UDP, TCP, or HTTP health checks to monitor the health status of the server. However, TCP and HTTP health checks require a TCP port. This may increase the network complexity and cause potential risks. To ensure consistent service capabilities in Classic Load Balancer (CLB)-to-NLB migration scenarios, we recommend that you use UDP health checks.

  • Make sure that the security groups of the backend server allow the UDP health check port.

Procedure

Step 1: Deploy an application

The following example shows how to deploy a Python application that can respond to UDP requests. In this example, the ECS instances use the CentOS 7.9 operating system. The example is for reference only. Adjust the configurations for your programs and applications.

Deploy a UDP application on ECS01 to respond to requests

  1. Log on to ECS01.

  2. Run the vi udp_server.py command and press the I key to enter the edit mode.

    Copy and paste the following code:

    Sample code for deploying a test application

    Note
    • Replace the IP address of UDP_IP in the fourth line with the private IP address of ECS01.

    import socket
    
    # Configure the IP address and port number of the UDP server.
    UDP_IP = "10.1.*.*"      # Replace the IP address with the actual IP address of your backend server.
    UDP_PORT = 161            
    
    # Create a UDP socket.
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        # Associate the UDP socket with the IP address and port number.
        sock.bind((UDP_IP, UDP_PORT))
        print("UDP server up and listening ")
    
        # The server keeps running.
        while True:
            # Receive data from clients. The buffer size is 1,024 bytes.
            data, addr = sock.recvfrom(1024)
            print(f"Received message: {data} from {addr}")
    
            # Check the content of the received data.
            if data == b"PING":
                sock.sendto(b"PONG1", addr)
                print("Sent PONG1")
    
    
  3. Press the Esc key and enter :wq to save the configurations.

  4. Run the sudo python3 udp_server.py command to execute the script.

  5. The UDP server up and listening response message indicates that the UDP application is enabled.

If you fail to enable the application, check whether the specified port is occupied by another application or errors exist in the commands or code.

Deploy a UDP application on ECS02 to respond to requests

  1. Log on to ECS02.

  2. Run the vi udp_server.py command and press the I key to enter the edit mode.

    Copy and paste the following code:

    Sample code for deploying a test application

    Note
    • Replace the IP address of UDP_IP in the fourth line with the private IP address of ECS02.

    import socket
    
    # Configure the IP address and port number of the UDP server.
    UDP_IP = "10.2.*.*"      # Replace the IP address with the actual IP address of your backend server.
    UDP_PORT = 161        
    
    # Create a UDP socket.
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        # Associate the UDP socket with the IP address and port number.
        sock.bind((UDP_IP, UDP_PORT))
        print("UDP server up and listening ")
    
        # The server keeps running.
        while True:
            # Receive data from clients. The buffer size is 1,024 bytes.
            data, addr = sock.recvfrom(1024)
            print(f"Received message: {data} from {addr}")
    
            # Check the content of the received data.
            if data == b"PING":
                sock.sendto(b"PONG2", addr)
                print("Sent PONG2")
    
    
  3. Press the Esc key and enter :wq to save the configurations.

  4. Run the sudo python3 udp_server.py command to execute the script.

  5. The UDP server up and listening response message indicates that the UDP application is enabled.

If you fail to enable the application, check whether the specified port is occupied by another application or errors exist in the commands or code.

Step 2: Create a server group and enable health checks

  1. Log on to the NLB console.

  2. In the top navigation bar, select the region where the NLB instance is deployed.

  3. In the left-side navigation pane, choose NLB > Server Groups.

  4. On the Server Groups page, click Create Server Group.

  5. In the Create Server Group dialog box, configure the parameters. The following table describes some of the parameters. Other parameters use the default values. For more information, see NLB server groups.

    Parameter

    Description

    Server Group Type

    Select the type of server group that you want to create.

    Backend Server Protocol

    In this example, UDP is selected.

    Configure Health Check

    Enable health checks and click Modify on the right side of Health Check Settings.

    Health Check Protocol

    In this example, UDP is selected.

    Health Check Port

    In this example, Backend Server Port is selected.

  6. After you complete the configurations, click Create. After the server group is created, click Add Backend Server in the message that appears.

  7. In the Add Backend Server panel, select ECS01 and ECS02 and click Next.

  8. In the Ports/Weights step, enter port 161, retain the default weight, and then click OK.

Step 3: Create a UDP listener

  1. Log on to the NLB console.

  2. In the top navigation bar, select the region where the NLB instance is deployed.

  3. On the Instances page, find the NLB instance and click Create Listener in the Actions column.

  4. On the Configure Server Load Balancer page, configure the parameters. The following table describes some of the parameters. Other parameters use the default values. For more information, see Add a UDP listener. After you complete the configuration, click Submit.

    Configure the listener:

    Parameter

    Description

    Listener Protocol

    In this example, UDP is selected.

    Listener Port

    In this example, port 161 is used.

    Associate the listener with a server group:

    Parameter

    Description

    Server Group

    Select the server group that contains ECS01 and ECS02.

Step 4: Run tests

  1. Probe a healthy server

    1. In this example, a Linux client in the VPC is used to run tests.

      1. Run the vi udp_client.py command and press the I key to enter the edit mode.

        Copy and paste the following code:

        Sample code for testing the deployment

        import socket
        
        # Create a UDP socket.
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        
        server_address = ('10.0.*.*', 161)  # Replace the IP address with the virtual IP address (VIP) of your NLB instance.
        
        # Send data.
        message = "PING"
        client_socket.sendto(message.encode(), server_address)
        
        # Receive and respond to the request.
        data, address = client_socket.recvfrom(1024)
        print(f"Received data from server: {data.decode()}")
        
        # Close the socket.
        client_socket.close()
        
      2. Press the Esc key and enter :wq to save the configurations.

    2. Run the sudo python3 udp_client.py command. The following response message which contains PONG1 indicates that ECS01 can receive requests.

      image

    3. Run the sudo python3 udp_client.py command. The following response message which contains PONG2 indicates that ECS02 can receive requests.

      image

  2. Simulate faults

    Stop the backend UDP service on the backend server ECS01. In the script execution interface, press Ctrl+C to terminate the script. When you are returned to the command prompt, it confirms that the backend UDP service has stopped.

    image

    On the Linux client, run the sudo python3 udp_client.py command for multiple times. If the command output contains PONG2, as shown in the following figure, it indicates that ECS02 can properly respond to requests.

    image

  3. Log on to the NLB console and view the cause of health check failures.

    1. On the Instances page, click the ID of the NLB instance that you want to manage.

    2. Click the Listener tab. In the Health Check Status column, find the Unhealthy health check state.

    3. Click Unhealthy. A dialog box appears and displays the unhealthy servers and cause of health check failures.

      image

    4. In the dialog box that appears, click Instance Diagnostics. In the Instance Diagnostics panel, check Diagnostic Items.

      image

  4. Run the sudo python3 udp_server.py command on ECS01 again to enable the backend UDP service. The command output contains UDP server up and listening, which indicates that the backend UDP service is enabled.

  5. Go to the Listener tab. The Health Check Status column displays Healthy.

    image

Disable health checks.

Important

If you disable the health check feature, NLB continues to distribute requests to unhealthy backend ECS instances, which causes service interruptions. We recommend that you enable the health check feature.

  1. Log on to the NLB console.

  2. In the top navigation bar, select the region where the NLB instance is deployed.

  3. In the left-side navigation pane, choose NLB > Server Groups.

  4. On the Server Groups page, find the server group that you want to manage and click Modify Health Check Settings in the Actions column.

  5. In the Modify Health Check Settings dialog box, disable health checks and click Save.

Reference

For more information about UDP health checks, see NLB health checks.