CodeBlanks Logo Icon CodeBlanks

Zero-downtime deployment

PHP course · Expert · Lesson 48/54

Learn zero-downtime deployment with symlink swapping and atomic deploys.

Zero-downtime deployment

Zero-downtime deploy activates the new release without stopping the old one. Users never see a 502 or a timeout.

The common pattern: ship each release into its own folder like releases/2024-10-25, then atomically swap the current symlink to the new folder.

In PHP symlink($target, $link) creates a symbolic link; it is atomic like rename: all or nothing.

The health check endpoint

Before activating the new release a HealthCheck endpoint is called: is the database, cache and opcache ready?

If /health returns 200 OK with {"status":"ok"} the link swaps; otherwise the deploy halts and rollback kicks in.

This check lets the automated pipeline confirm nothing is broken; no human intervention needed.

Exercise Write HealthCheck and deploy

Define a HealthCheck class with a status() method that returns "ok". Then write a deploy() function: print deploy: started, instantiate HealthCheck and print its status() result prefixed with health: , then print deploy: done. Add the deploy() call.

Show example solution
string { return "ok"; } }. Then write function deploy(): void { echo "deploy: started\n"; $hc = new HealthCheck(); echo "health: " . $hc->status() . "\n"; echo "deploy: done\n"; }. Finish with deploy();.
Example solution:
<?php
// HealthCheck class and deploy function
class HealthCheck {
    public function status(): string {
        return "ok";
    }
}

function deploy(): void {
    echo "deploy: started\n";
    $hc = new HealthCheck();
    echo "health: " . $hc->status() . "\n";
    echo "deploy: done\n";
}

deploy();

Test yourself

  1. Which PHP function creates a symbolic link?

    • symlink
    • link
    • rename
    • mkdir
    Show answer

    symlink — Correct. symlink($target, $link) creates a symbolic link.

  2. Complete the symlink call

    <?php
    // Switch to new release
    ____('releases/2024-10-25', 'current');
    print_r(["link" => "current"]);
    
    Show answer
    <?php
    // Switch to new release
    symlink('releases/2024-10-25', 'current');
    print_r(["link" => "current"]);
    
  3. Match the deployment concepts

    symlink
    Creates a symbolic link
    rollback
    Returns to previous release
    atomic
    Indivisible operation
    health check
    Liveness probe

All lessons in Expert