Skip to content
tezvyn:
Menu

Idempotency in infrastructure provisioning scripts

Source: interviewMediumHow cards are made

Summary

Safe-retry design.

Key points

idempotency means repeated runs converge to one end state; achieve it via desired-state reconciliation or idempotency keys with read-before-write.

What's really being asked

Whether you can build provisioning automation that survives retries, network blips, and reruns without creating duplicate or corrupted resources.

The full answer

Idempotency means running the operation once or many times yields the same final state. If a network timeout makes you unsure whether a create succeeded, you can safely retry without spinning up a second instance. Technique one is declarative desired-state reconciliation: describe the target state, read the current state, compute the diff, and apply only what is missing. Tools like Terraform and Kubernetes controllers work this way. Technique two is idempotency keys: the client generates a unique token per logical operation and sends it with each request; the server records the token and returns the original result for duplicates instead of acting again. A simpler variant is read-before-write or an upsert that creates only if absent.

The mistakes people make

Saying idempotency just means retrying, claiming POST is inherently safe, or relying on naming alone without checking existing state, which races under concurrency.

What usually comes next

How long do you store idempotency keys? How do you handle partial failures mid-provision? What about concurrent runs racing on the same resource? How does this interact with eventual consistency in the cloud API?

A concrete example

A script provisions a virtual machine. With desired-state reconciliation, it lists existing VMs by a stable tag, finds none, and creates one; on rerun it finds the VM and does nothing. Alternatively, it sends the create call with an idempotency key derived from a request UUID; if a timeout triggers a retry, the cloud API recognizes the same key and returns the existing VM rather than launching a duplicate, so a flaky network never doubles your fleet or your bill.

Interview question

An infrastructure script times out after sending a create-VM request, so it retries. What property prevents a duplicate VM?

  • a.Higher request timeouts so retries never happen
  • b.Atomicity, because the request runs inside a database transaction
  • c.Statelessness of the HTTP protocol guarantees safe retries
  • d.Idempotency, via desired-state reconciliation or an idempotency keyCorrect
Why?

Idempotency ensures repeated operations converge to one end state, so a retry is a no-op. Atomicity concerns all-or-nothing execution of one operation and does not by itself dedup a second create request after a timeout.

Just read this? Test yourself on what you have been reading.

Read the original → cloud.google.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on idempotency — each one lists the topics its interview covers.

See open roles