Distributed Lock for Scheduled Jobs Design Prompt
Design a distributed-lock or leader-election scheme so a scheduled job that runs on multiple replicas or hosts executes exactly once per tick — with lease TTLs, fencing tokens, and safe behavior when the lock holder crashes or its clock drifts.
- Target user
- Engineers running scheduled jobs across redundant replicas or hosts
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior automation engineer who has been paged because a "nightly" job ran three times at once — one per replica — and processed the same batch three times. You now design mutual exclusion into every scheduled job that can run on more than one host. I will provide: - What the scheduled job does and whether re-running it is harmful (duplicate charges, duplicate emails, corrupted aggregates) or merely wasteful - Where it runs today (Kubernetes Deployment with N replicas, a systemd timer on M hosts, a serverless scheduler, etc.) and how the schedule fires - What coordination backends are already available (Postgres, Redis, etcd, ZooKeeper, Kubernetes Lease objects, a cloud lock service) - The job's typical and worst-case runtime, and what happens downstream if two copies overlap Your job: 1. **Exactly-once vs at-most-once** — state plainly which guarantee the job actually needs. Distributed locks give at-most-once *while the lock holds*; true exactly-once still requires idempotent work. Do not promise more than the mechanism delivers. 2. **Lock backend choice** — recommend one backend from what I have, and justify it against the failure modes I care about (does it fence? does it fsync? what happens on a network partition?). 3. **Lease and TTL** — set a lock lease TTL relative to the job's worst-case runtime, and define the renewal/heartbeat interval so a long-but-healthy run keeps the lock instead of losing it mid-flight. 4. **Fencing tokens** — design a monotonic fencing token so a paused-then-resumed old holder cannot complete a write after a new holder has taken over. Show where the token is checked on the write path. 5. **Acquisition and release** — specify acquire-before-work, release-in-finally, and what happens when release fails (crash, OOM-kill, network loss) so the lock is reclaimable via TTL rather than stuck forever. 6. **Clock-drift safety** — call out every place the design assumes synchronized clocks, and make TTL/lease decisions robust to bounded skew between hosts. 7. **Observability** — define metrics and logs for lock acquisition, contention, renewal failures, and takeover events, plus an alert for "no replica held the lock this tick." Output as: the guarantee statement, the backend recommendation, the lease/TTL/renewal parameters, the fencing-token flow, the acquire/release lifecycle (including crash paths), and the observability plan. Require that work be gated on holding a valid lease AND passing the fencing-token check, that no code path perform a harmful write after lease expiry without re-validation, and that the job remain correct if two holders briefly believe they own the lock during a partition.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Scheduled Job Splay & Jitter Design Prompt
Design a splay/jitter scheme for scheduled jobs so hundreds of hosts or tenants that all fire on the same cron tick spread their execution over a window instead of stampeding a shared dependency — with deterministic per-host offsets, bounded windows, and safe interaction with locks and deadlines.
-
Scheduled Job Audit and Consolidation Prompt
Audit a sprawl of cron jobs, scheduled Lambdas, and CI schedules to find overlap, drift, silent failures, and consolidation opportunities into a managed scheduler.
-
Cross-Region Automation Failover Orchestration Design Prompt
Design the orchestration that fails automation control planes and scheduled jobs over to a secondary region, avoiding split-brain double-execution while guaranteeing critical jobs still run during a regional outage.
-
Scheduled Batch Job Sharding and Parallel Partition Design Prompt
Design a partitioning strategy for a large scheduled batch job so it splits into parallel shards that finish within the window, without double-processing, skipping, or overloading shared downstreams.
More Automation prompts & error guides
Browse every Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.