Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

You deploy to one canary host first and want to proceed only if its error rate is not worse than the rest. How do you script the decision?

AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Compare canary and baseline error rates over comparable recent traffic, with a minimum request count to avoid noise.

What I'd do →

rate() {  # rate HOST -> "errors total" for the most recent 5,000 requests
  ssh -n "$1" "tail -n 5000 /var/log/nginx/access.log" |
    awk '{t++; if ($9 ~ /^5/) e++} END {print e+0, t+0}'
}
read -r ce ct < <(rate "$canary")
be=0 bt=0; for h in "${baseline[@]}"; do read -r e t < <(rate "$h"); be=$((be + e)); bt=$((bt + t)); done
(( ct >= 500 )) || die "canary has only $ct requests; not enough data"
awk -v ce="$ce" -v ct="$ct" -v be="$be" -v bt="$bt" \
  'BEGIN {c = ce / ct; b = (bt ? be / bt : 0); printf "canary %.3f%% baseline %.3f%%\n", 100*c, 100*b
          exit !(c <= b * 1.5 + 0.001)}' || { rollback "$canary"; exit 1; }
echo "canary healthy, continuing rollout"

Why → Relative comparison handles normal background errors; a minimum sample avoids judging on 10 requests.

Interview tip → Mention that Argo Rollouts or Flagger automate this with Prometheus queries, and that latency matters as much as errors.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations