DEV Community

Michael Di Prisco
Michael Di Prisco

Posted on

Backing Services - The Twelve Factor App Methodology

Treat backing services as attached resources

Welcome back to our exploration of the Twelve Factors in Software Development. In this segment, we'll unravel Factor 4: Backing Services. Backing services include databases, message queues, caching systems, and other external resources that your application relies on.

Backing Services: Treat Them as Attached Resources

The Backing Services factor emphasizes the importance of treating backing services as attached resources. This means your application should be designed to connect to these services as if they were local components, regardless of whether they run in the same environment or are provided as third-party services.

Why It Matters

In modern application architecture, it's common to use external services for essential functionalities like data storage, messaging, and caching. By treating these services as attached resources, you decouple your application from the specifics of the service implementation. This enhances flexibility, scalability, and maintainability.

How to Implement

Use configuration and environment variables to abstract the details of backing services. The idea is to make it easy to switch between different service providers or configurations without modifying your code. For example, if your application relies on a database, specify its connection details through environment variables.

// Database connection using environment variables
const dbConnection = process.env.DATABASE_CONNECTION_STRING;
Enter fullscreen mode Exit fullscreen mode

Example in Action

Consider a web application that uses a cloud-based database service. Instead of hardcoding the database connection details in your code, configure them as environment variables. This way, whether your application is running locally or in a cloud environment, the necessary connection details are easily adjustable.

# Set the database connection string as an environment variable
export DATABASE_CONNECTION_STRING="your_actual_connection_string"

# Run your application
node app.js
Enter fullscreen mode Exit fullscreen mode

Resources can be attached to and detached from deploys at will. For example, if the app’s database is misbehaving due to a hardware issue, the app’s administrator might spin up a new database server restored from a recent backup. The current production database could be detached, and the new database attached – all without any code changes.

By following the Backing Services factor, you ensure that your application is versatile and can seamlessly adapt to different environments and service providers.

Stay tuned for Factor 5: Build, release, run, where we'll explore the importance of a consistent and repeatable build process for your application.

Top comments (0)