Sample golang http webservice with graceful shutdown mechanism. GCP & AWS provides ephemeral VM deployment (preemptive / spot instance) with cheaper cost (up to 80%). The caveat are its host can only live up to 24 hours & can be halted within ~30s.
- Backend service API provide healthcheck endpoint, in this case "/actuator/health".
- Load Balancer monitor the healthcheck for every 2-5 seconds, any unhealthy nodes will be removed from serving backend group.
- When kill/interrupt OS signal is received, service will mark itself as unhealthy (in this case "OUT_OF_SERVICE"), and then wait for n seconds so LB will remove it from backend group.
- Wait for t seconds for ongoing request to be completed.
- Shutdown the service.
For any given "h" halt signal timeout, n + t < h.