Documentation Index
Fetch the complete documentation index at: https://cerebrium-fix-make-entrypoint-docs-explicit.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Graceful Termination
Cerebrium runs in a shared, multi-tenant environment. To efficiently scale, optimize compute usage, and roll out updates, the platform continuously adjusts its capacity - spinning down nodes and launching new ones as needed. During this process, workloads are seamlessly migrated to new nodes. In addition, your application has its own metric-based autoscaling criteria that dictate when instances should scale or remain active, as well as handle instance shifting during new app deployments. Therefore, in order to prevent requests from ending prematurely when we mark app instances for termination, you need to implement graceful termination.Understanding Instance Termination
For both application autoscaling and our own internal node scaling, we will send your application a SIGTERM signal, as a warning to the application that we are intending to shut down this instance. For cortex applications (Cerebriums default runtime), this is handled. On custom runtimes, should you wish to gracefully shut down, you will need to catch and handle this signal. Once at leastresponse_grace_period has elapsed, we will send your application a SIGKILL signal, terminating the instance immediately.
When Cerebrium needs to terminate an contanier, we do the following:
- Stop routing new requests to the container.
- Send a SIGTERM signal to your container.
- Waits for
response_grace_periodseconds to elaspse. - Sends SIGKILL if the container hasn’t stopped
SIGTERM, which can interrupt in-flight requests and cause 502 errors.
Implementation
For custom runtimes using FastAPI, implement thelifespan pattern to respond to SIGTERM.
main.py
Create a new file namedmain.py with the following:
cerebrium.toml
If you already have acerebrium.toml file, add or update these sections. If you don’t have one, create a new file with the following:
replica_concurrencyshould matchmax_concurrencyin yourAppStateclass (if you add that field)portmust match the port in your Dockerfile CMD- Adjust hardware settings based on your application needs
Dockerfile
Create a new file namedDockerfile with the following:
requirements.txt
Add to your existingrequirements.txt or create a new file with:
Key Points
- The
/readyendpoint is essential for proper load balancing during scaling events - Without a proper
/readyendpoint, Cerebrium uses TCP ping which only checks if the port is open, potentially routing traffic to replicas that are shutting down - The 30-second shutdown timeout in the lifespan function is configurable
- All request tracking uses asyncio locks to ensure thread safety