Health checks report process liveness separately from readiness for real application work.
What It Does
Health checks report process liveness separately from readiness for real application work. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
A web route whose dependencies can be checked without leaking secrets.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Understand the primary operation and its inputs.
<?php
function liveness(): array { return ['status' => 'ok']; }
echo json_encode(liveness(), JSON_THROW_ON_ERROR);
Example 2: Another Case
Check failure behavior or a different execution path.
<?php
$checks = ['database' => true, 'queue' => false];
$ready = !in_array(false, $checks, true);
echo $ready ? 'ready' : 'not ready';
Practical Example: Putting It to Use
Readiness may check dependencies while liveness should remain inexpensive.
<?php
$checks = ['database' => true, 'queue' => true]; $ready = array_reduce($checks, fn ($carry, $ok) => $carry && $ok, true);
http_response_code($ready ? 200 : 503);
echo json_encode(['ready' => $ready], JSON_THROW_ON_ERROR);
Important Behavior and Edge Cases
Avoid long or expensive checks that can overload a failing dependency.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Setting Up Health Checks for Services | Health checks report process liveness separately from readiness for real application work. |
| Related approach | Return simple HTTP statuses without disclosing implementation details. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Setting Up Health Checks for Services | Avoid long or expensive checks that can overload a failing dependency. |
| Assuming a related mechanism has the same behavior | Return simple HTTP statuses without disclosing implementation details. |
Key Takeaways
- Health checks report process liveness separately from readiness for real application work.
- Avoid long or expensive checks that can overload a failing dependency.
- Return simple HTTP statuses without disclosing implementation details.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.