liveBook · Manning

Singleton pattern

As explained in Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform

A singleton pattern is a design pattern in which a resource or set of resources has one authoritative instance within a system.

A singleton pattern declares a set of infrastructure resources as singular instances in a system and deploys them with one command. A static file or directory holds environment-specific parameters and expresses relationships among resources in one place. section 3.1

Use the pattern deliberately when:

  • A resource is unique, changes rarely, and has no need for multiple independently managed instances. section 3.6 section 3.7
  • A small configuration benefits from a single reference for provisioning order, required parameters, debugging, and troubleshooting. section 3.1
  • Expect the configuration to become a coordination and scaling liability when many resources share state, changes overlap, or one update affects a broad blast radius. section 9.0 section 10.0

Database infrastructure in GCP

One configuration defines a GCP project, a database network using 10.0.3.0/16, a server template that installs MySQL packages, and a server group containing three database servers. The configuration captures their relationships and provisions them together. section 3.1

Figure 3.1 A singleton pattern expresses the configuration of an initial set of resources—such as a project, network, and database—in one file. This pattern captures the relationships among resources in one place.

Shows how a singleton configuration places a project, network, and database resources in one file and captures their relationships. section 3.1

How Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform covers it

How deep it goes
Defines the pattern, demonstrates a GCP configuration, and defers detailed modular decomposition to factory, builder, and composite patterns.
Examples
Uses a GCP project, database network, MySQL server template, three-server group, and a testing workflow for an application server.
What it assumes
Assumes familiarity with infrastructure resources and configuration while teaching resource relationships, parameters, and provisioning order through one configuration.
What you can do after
Operate/configure a small set of unique infrastructure resources from one static configuration and one deployment command.
How it connects
Connects singleton configurations with infrastructure as code, factory pattern, builder pattern, composite pattern, immutability, and end-to-end testing.

Also explained in

Compare how 6 books explain Singleton pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform. The section links point to the parts of the book it draws on.