tezvyn:

Declarative vs. Imperative Automation

AI-drafted, machine-checkedSource: Wikipedia: Declarative programmingbeginner

Declarative automation defines the desired end state (“what”), not the steps to get there (“how”). It's used in tools like Kubernetes to manage complex infrastructure, letting the system figure out the details.

WHY IT EXISTS As systems grow, managing them with step-by-step scripts becomes fragile and error-prone. A script to create a server doesn't know what to do if the server already exists but is misconfigured. Declarative automation was developed to manage this complexity by focusing on the desired final state, not the intermediate steps.

THE MENTAL MODEL The core difference is "what" versus "how." Imperative is like giving a chef a detailed recipe: "First, chop onions. Second, heat the pan...". You control the exact steps. Declarative is like showing the chef a picture of the finished dish and a list of ingredients, trusting them to figure out the cooking process. The imperative approach is a script; the declarative approach is a configuration file or manifest.

HOW IT WORKS An imperative system executes a list of commands in order. A shell script is a perfect example. A declarative system works in a control loop. It reads a configuration file defining the desired state, inspects the actual state of the system, calculates the difference (a "diff" or "plan"), and then executes the specific actions needed to make the actual state match the desired state. This process is often idempotent, meaning you can run it multiple times and it will produce the same result.

WHEN TO USE IT Use declarative automation for managing the state of complex, long-lived systems. This includes infrastructure as code (Terraform, CloudFormation), configuration management (Ansible, Puppet), and container orchestration (Kubernetes). It ensures consistency and enables self-healing, as the system constantly works to enforce the declared state. Use imperative for simple, one-off tasks where the order of operations is critical, such as a database migration script.

WHEN NOT TO USE IT Avoid declarative models for tasks that are not about maintaining a state, but about performing a one-time, sequential process. For example, a script that builds a software artifact, runs tests, and then pushes it to a registry is inherently imperative. Conversely, avoid using imperative scripts to manage infrastructure, as they can't easily handle state drift or recover from partial failures.

ONE CANONICAL EXAMPLE To ensure a web server is running, an imperative approach is a shell script that checks if the process exists and starts it if it doesn't. A declarative approach is a Kubernetes manifest that includes replicas: 1 for an Nginx container. If that container dies for any reason, the Kubernetes control plane sees the discrepancy between the desired state (1 replica) and the actual state (0 replicas) and automatically starts a new one to correct it.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.