A cleanup job runs from cron on all 3 nodes of a cluster but must execute on exactly one. How do you do it?
AdvancedScenarioScenarios: text processing and scheduling decisions
Answer
Situation read → Local locks do not coordinate across hosts. You need leader election or a shared lock.
What I'd do →
- Simplest: schedule it in one place (a single admin host, a Kubernetes CronJob with
concurrencyPolicy: Forbid, or a CI schedule). - If it must run from the nodes, use a shared lock with a TTL:
# DynamoDB conditional put as a lock that expires after 30 minutes now=$(date +%s) aws dynamodb put-item --table-name locks \ --item "{\"id\":{\"S\":\"cleanup\"},\"owner\":{\"S\":\"$(hostname)\"},\"exp\":{\"N\":\"$((now + 1800))\"}}" \ --condition-expression 'attribute_not_exists(id) OR exp < :now' \ --expression-attribute-values "{\":now\":{\"N\":\"$now\"}}" 2>/dev/null || exit 0 - Or a Postgres advisory lock, Consul
consul lock, or etcd. - Make the job idempotent anyway, in case two runs happen.
Why → A distributed lock with expiry avoids both double runs and a stuck lock after a crash.
Interview tip → Lead with "move the schedule to one place"; it is simpler than any lock.
More on Scenarios: text processing and scheduling decisions
Users report errors in the last hour. From the Nginx access log, find which endpoints produce the most 5xx responses.BeginnerA MySQL slow log is 3 GB. You need the 10 slowest query patterns. What do you do?IntermediateTwo servers that should be identical behave differently. Compare their installed packages.BeginnerFind which pods were OOMKilled in a namespace, with their container names and restart counts.IntermediateProduce a CSV of running EC2 instances that are missing an `Owner` tag.IntermediateYou have access logs from five web servers and need one timeline sorted by time. How?Intermediate