|
| 1 | +--- |
| 2 | +parser: v2 |
| 3 | +auto_validation: true |
| 4 | +time: 15 |
| 5 | +tags: [ tutorial>beginner, software-product>sap-btp--cloud-foundry-runtime-and-environment] |
| 6 | +primary_tag: software-product>sap-btp--cloud-foundry-runtime-and-environment |
| 7 | +author_name: Beyhan Veli |
| 8 | +author_profile: https://github.com/beyhan |
| 9 | +--- |
| 10 | + |
| 11 | +# Application Readiness Check |
| 12 | +<!-- description --> How the Application Readiness Health Check works and how you can configure it for your own application |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | +- **Groups** [Create Your First App on Cloud Foundry](group.scp-3-first-app) |
| 16 | +- **Tutorials** [Install the Cloud Foundry Command Line Interface (CLI)](cp-cf-download-cli) |
| 17 | +- **Tutorials** [Application Health Check](cp-cf-application-health-check) |
| 18 | + |
| 19 | +## You will learn |
| 20 | +- The difference between liveness and readiness health checks in Cloud Foundry |
| 21 | +- How readiness health checks prevent premature routing of traffic to app instances |
| 22 | +- How to configure readiness health checks using the app manifest |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +### Overview of Health Checks |
| 27 | + |
| 28 | + |
| 29 | +Cloud Foundry provides two categories of health checks to keep your applications healthy and available: **liveness** health checks and **readiness** health checks. Understanding the difference between them is essential for running reliable applications. |
| 30 | + |
| 31 | +- **Liveness health checks** validate that app instances are running. When a liveness health check fails, Cloud Foundry considers the instance crashed, stops it, and restarts it. |
| 32 | +- **Readiness health checks** validate that app instances are ready to serve requests. When a readiness health check fails, the app instance is removed from the route pool so that it no longer receives traffic, but it is **not** restarted. |
| 33 | + |
| 34 | +There are three types available for both liveness and readiness health checks, as explained in the tutorial for [Application Health Check](cp-cf-application-health-check): |
| 35 | + |
| 36 | +- `http` health checks perform a GET request to an endpoint. |
| 37 | +- `port` health checks make a TCP connection to the port (or ports) configured for the app. |
| 38 | +- `process` health checks check that a process stays running. |
| 39 | + |
| 40 | +The default liveness health check type is `port`, while the default readiness health check type is `process`. |
| 41 | + |
| 42 | +For more detailed information about the types of health checks, see [Using Cloud Foundry Health Checks](https://docs.cloudfoundry.org/devguide/deploy-apps/healthchecks.html). |
| 43 | + |
| 44 | +Readiness health checks are particularly useful when your application needs time after startup to load caches, establish database connections, or warm up before it can handle real traffic. Without a readiness health check, Cloud Foundry may route requests to an instance that is running but not yet ready, resulting in errors for your users. |
| 45 | + |
| 46 | +Additionally, readiness health checks help in situations where an application instance becomes temporarily overloaded. If the instance determines it cannot handle new requests based on its internal metrics or logic, it can intentionally fail its readiness health check. This causes Cloud Foundry to automatically remove the overloaded instance from the routing pool, so it stops receiving traffic while still remaining running. Once the instance recovers and is able to handle new requests, its readiness health check will succeed again, and Cloud Foundry will add it back to the request queue. This mechanism helps prevent further overload, increases application stability, and allows instances to flexibly remove and re-add themselves as they become healthy. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +### The Lifecycle of a Readiness Health Check |
| 51 | + |
| 52 | + |
| 53 | +Understanding how readiness health checks fit into the app lifecycle is important for configuring them correctly. The lifecycle is similar to the one described for [Application Health Check](cp-cf-application-health-check), with key differences in how readiness and liveness are handled. |
| 54 | + |
| 55 | +1. When you deploy an application, you can specify both a liveness and a readiness health check in the app manifest. |
| 56 | +2. Cloud Controller creates a Long Running Process (LRP) for Diego with the specified health check definitions. If no readiness health check is provided, Cloud Controller configures a `process` readiness health check type by default. |
| 57 | +3. When Diego starts an app instance, a **startup health check** (using the liveness health check configuration) runs every 2 seconds until a healthy response is received or the startup timeout elapses. This 2-second interval is not configurable. |
| 58 | +4. After the startup health check succeeds, the app instance is marked as **Running** and Diego begins performing both the liveness and readiness health checks at their configured intervals (default: 30 seconds). |
| 59 | +5. Once the readiness health check succeeds, the app instance's route is advertised and the instance starts receiving traffic. |
| 60 | +6. If the readiness health check later fails, the route to that app instance is **removed** and the instance stops receiving traffic but continues running. Once the readiness health check succeeds again, the route is re-advertised. |
| 61 | +7. If the liveness health check fails, Diego stops and deletes the app instance entirely, then reschedules a new one. This is reported as a crash event. |
| 62 | + |
| 63 | +> The key distinction: a failed readiness health check removes the instance from routing (no traffic), while a failed liveness health check causes the instance to be restarted. This separation allows your app to temporarily become unavailable to traffic without being unnecessarily restarted. |
| 64 | +
|
| 65 | + |
| 66 | + |
| 67 | +### Configuring a Readiness Health Check via the App Manifest |
| 68 | + |
| 69 | + |
| 70 | +Readiness health checks are configured through the app manifest. Unlike liveness health checks, they cannot be set using `cf set-health-check`. The following manifest attributes are available: |
| 71 | + |
| 72 | +| Attribute | Description | Default | |
| 73 | +|---|---|---| |
| 74 | +| `readiness-health-check-type` | The type of readiness health check: `http`, `port`, or `process` | `process` | |
| 75 | +| `readiness-health-check-http-endpoint` | The endpoint for `http` type readiness health checks | `/` | |
| 76 | +| `readiness-health-check-invocation-timeout` | Timeout in seconds for individual readiness health check requests | `1` | |
| 77 | +| `readiness-health-check-interval` | Time in seconds between readiness health check requests | `30` | |
| 78 | + |
| 79 | +To configure an `http` readiness health check for your [cf-nodejs](https://github.com/SAP-samples/cf-sample-app-nodejs.git) application, update your `manifest.yml` as follows: |
| 80 | + |
| 81 | +```YAML |
| 82 | +--- |
| 83 | +applications: |
| 84 | +- name: cf-nodejs |
| 85 | + memory: 192M |
| 86 | + instances: 1 |
| 87 | + random-route: false |
| 88 | + readiness-health-check-type: http |
| 89 | + readiness-health-check-http-endpoint: / |
| 90 | +``` |
| 91 | +
|
| 92 | +In the example above, Cloud Foundry will perform a GET request to the `/` endpoint to determine if the app instance is ready to receive traffic. You can point this to a dedicated readiness endpoint (e.g., `/ready`) if your application provides one. |
| 93 | + |
| 94 | +You can also combine a readiness health check with a liveness health check in the same manifest: |
| 95 | + |
| 96 | +```YAML |
| 97 | +--- |
| 98 | +applications: |
| 99 | +- name: cf-nodejs |
| 100 | + memory: 192M |
| 101 | + instances: 1 |
| 102 | + random-route: false |
| 103 | + health-check-type: http |
| 104 | + health-check-http-endpoint: / |
| 105 | + readiness-health-check-type: http |
| 106 | + readiness-health-check-http-endpoint: /ready |
| 107 | +``` |
| 108 | + |
| 109 | +After updating your manifest, push or re-push your application for the changes to take effect: |
| 110 | + |
| 111 | +``` |
| 112 | +cf push cf-nodejs |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +### Validate the readiness health check |
| 117 | + |
| 118 | + |
| 119 | +To validate that the readiness health check is working, you can use the `cf logs` command: |
| 120 | + |
| 121 | +``` |
| 122 | +cf logs cf-nodejs --recent |
| 123 | +``` |
| 124 | + |
| 125 | +You should see a log line similar to: |
| 126 | + |
| 127 | +``` |
| 128 | +[HEALTH/0] OUT Container passed the readiness health check. Container marked ready and added to route pool. |
| 129 | +``` |
| 130 | + |
| 131 | +This indicates that the readiness health check has been configured and was successful. Additional log messages may also show that both the liveness and readiness health checks are being evaluated. |
| 132 | + |
| 133 | + |
| 134 | +You can also check the current health check configuration of your app by running: |
| 135 | + |
| 136 | +``` |
| 137 | +cf get-readiness-health-check cf-nodejs |
| 138 | +``` |
| 139 | + |
| 140 | +This displays the configured readiness check type and endpoint. Note that readiness health check details are reflected in the manifest and the app's process configuration. |
| 141 | + |
| 142 | + |
| 143 | +--- |
0 commit comments