Interview Prep/DevOps Fundamentals/Infrastructure & cloud basics

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=present only installs if missing; the output shows changed vs ok.
  • Terraform: applying the same code again shows "No changes".
  • Kubernetes: kubectl apply -f deploy.yaml repeatedly gives the same desired state.

Why it matters:

  1. Safe retries — if a pipeline fails halfway, you can simply run it again.
  2. Self-healing — config management and GitOps tools keep re-applying the desired state to fix drift.
  3. Predictability — the same input always gives the same system.
  4. 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.

All DevOps Fundamentals questions

More on Infrastructure & cloud basics