What is idempotency, and why is it important in automation?
BeginnerConceptInfrastructure & cloud basics
Answer
In one line: An operation is idempotent if running it once or many times gives the same result — and that's what makes automation safe to re-run.
Simple examples:
- Idempotent: "Make sure nginx is installed" — if it's already installed, nothing changes.
- Not idempotent: "Append this line to the config file" — run it three times and the line appears three times.
In practice:
# Not idempotent
echo "export PATH=$PATH:/opt/app/bin" >> ~/.bashrc
# Idempotent
grep -qxF 'export PATH=$PATH:/opt/app/bin' ~/.bashrc || echo 'export PATH=$PATH:/opt/app/bin' >> ~/.bashrc
mkdir /data # fails if it already exists
mkdir -p /data # idempotent
Tools built around idempotency:
- Ansible:
apt: name=nginx state=presentonly installs if missing; the output showschangedvsok. - Terraform: applying the same code again shows "No changes".
- Kubernetes:
kubectl apply -f deploy.yamlrepeatedly gives the same desired state.
Why it matters:
- Safe retries — if a pipeline fails halfway, you can simply run it again.
- Self-healing — config management and GitOps tools keep re-applying the desired state to fix drift.
- Predictability — the same input always gives the same system.
- Less fear — nobody worries about "running the script twice by mistake".
Idempotency in APIs: PUT and DELETE should be idempotent; for payments, clients send an idempotency key so a retried request doesn't charge the customer twice.
Tip for interviews: give one non-idempotent example and show how to make it idempotent — it proves you understand it beyond the definition.
More on Infrastructure & cloud basics
What is the difference between declarative and imperative infrastructure?BeginnerWhat is configuration drift, and how do you prevent it?BeginnerWhat is immutable infrastructure?BeginnerWhat does "pets vs cattle" mean?BeginnerWhat is cloud computing, and what are IaaS, PaaS and SaaS?BeginnerWhat is the shared responsibility model in the cloud?Beginner