In modern applications, continuous connectivity is key—especially for mobile apps relying on backend services. In this blog, we’ll walk through a Python-based solution that monitors the health of your app service servers and automatically fails over to a secondary server if needed. This sample code uses HTTP health checks and WebSocket connection endpoints to ensure that your application always connects to a healthy service.

Overview

The solution involves two types of endpoints:

    1. Health check URLs
      • These endpoints (e.g., https://.../_ping) are polled using HTTP HEAD requests.
      • They determine if the app service server is healthy.
    2. Connection endpoints
      • These are the WebSocket URLs (e.g., wss://.../primary) that your application uses to interact with the backend.
      • The active connection endpoint is updated based on the health check results.

If the primary server’s health check fails consecutively, the failover logic will switch the application’s connection to the secondary server.

The code in detail

Below is the complete code with inline comments and detailed explanations:

Key technical points

    • Health checks on App Service Servers:
      The code separates the health-check endpoints (used for monitoring) from the connection endpoints (used by your application). This allows you to check server health independently while maintaining a stable connection endpoint.
    • HTTP HEAD Requests:
      Using HEAD requests to the/_ping endpoint minimizes data transfer while still providing status codes and headers for diagnostics.
    • Background Thread:
      The health_check_worker runs in its own daemon thread, allowing continuous health monitoring without blocking the main application thread.
    • Failover Logic:
      • A counter (consecutive_failures) tracks consecutive failures.
      • If the count exceeds a set threshold (9 failures), the script attempts a failover by checking the health of the alternate server.
      • Upon a successful health check on the secondary server, the active connection endpoint is updated.
    • Logging:
      Detailed logging provides insights into the health check process, including HTTP response status, headers, and failover events. This aids in troubleshooting and monitoring.

Adapting for your application

    • You can easily translate and adapt this code to your preferred programming language such as  Swift and Kotlin to fit your application’s needs.
    • You might integrate this script or logic into your mobile code (iOS/Android) or a backend service that updates the active endpoint.
    • If you are on iOS or Android, consider how often and where you run this code. For example, background tasks or push notifications can trigger health checks in a mobile context.
    • If you have a microservice architecture, you might run this failover logic in a small service that exposes a current active URL to the mobile apps, so they always connect to the correct WSS endpoint.

Conclusion

This sample code provides a straightforward yet powerful mechanism for ensuring high availability in applications by automatically failing over to a backup server when the primary becomes unreachable. By separating the health checks from the connection endpoints, the application ensures that it always connects to a healthy server via WebSocket.

In a production environment, you may need to adapt and extend the logic to suit your specific requirements, network conditions, and security policies.

Implementing this logic in your mobile application or backend service can greatly improve uptime and resilience, ensuring your users remain connected even during unexpected service interruptions.

 

Author

Posted by Nishant Bhatia - Cloud Architect

Leave a reply