Imperative vs Declarative

Model of Computation

The remainder of this post will adopt a simple state machine model of computation. In the state machine model, a computation is defined as applying a command which transitions the system from one state to another. A running system is therefore a series of commands that transitions the system through various states.

Figure 1. State Machine Model of Computation
Figure 2. Structural Diagram highlighting the actors involved in the State Machine Model of Computation

Imperative Systems

In an imperative system, the user knows the desired state, determines the sequence of commands to transition the system to the desired state, and supplies a representation of the commands to the system. The component of the system that applies the command to transition the state is called a processor.

Figure 3. Imperative Systems

Declarative Systems

By contrast, in a declarative system, the user knows the desired state, supplies a representation of the desired state to the system, then the system reads the current state and determines the sequence of commands to transition the system to the desired state. The component that determines the necessary sequence of commands is called a controller.

Figure 4. Declarative Systems

Kubernetes

Kubernetes is a declarative system. Instead of a single controller, however, it has several controllers that are each responsible for a different Kubernetes object type, such as ReplicaSet or Deployment, for example. The API Server acts as the processor, applying the commands given by the controllers to the system state, which is stored in the Kubernetes Object Store.

Figure 5. Kubernetes as a Declarative System

In summary

The defining aspect of imperative and declarative systems is the answer to the question of who is in the driver’s seat. Is the user or the system responsible for orchestrating the transition to the desired state?

About this post

This blog post is part of a collaborative effort between the CNCF, Google, and SAP to advance the understanding of Kubernetes and its underlying concepts.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store