Failover to a secondary backend

If primary backend fails, retry with a different backend without caching the failure or reducing cache efficiency.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
match req.get_method() {
&Method::GET | &Method::HEAD => {
let mut beresp1 = req.send(PRIMARY_BACKEND)?;
// return backend response, if not 403 and not 5xx
if beresp1.get_status() != 403 && !beresp1.get_status().is_server_error() {
return Ok(beresp1);
}
// Reconstruct request and try again, though without the original request body
let beresp2 = beresp1
.take_backend_request()
.unwrap()
.send(FAILOVER_BACKEND)?;
if !beresp2.get_status().is_server_error() {
Ok(beresp2)
} else {
Ok(beresp1)
}
}
_ => {
// nothing special for other methods
let beresp = req.send(PRIMARY_BACKEND)?;
Ok(beresp)
}
}