Sunday, July 7, 2024

Twelve-Factor App methodology applying the Developing a Microservice with Spring Boot

12-Factor App Methodology

The 12-Factor App methodology is a set of best practices for building modern web-based applications. It provides guidelines for creating scalable, maintainable, and portable applications that can be deployed across various environments. Here are the twelve factors with examples:

1. Codebase

One codebase tracked in revision control, many deploys. Example: A single Git repository for your e-commerce application that includes all code for various environments (development, staging, production). Branches can be used for feature development and bug fixes.
git clone

2. Dependencies

Explicitly declare and isolate dependencies. Example: Using pom.xml in a Maven-based Java project to declare dependencies.

3. Config

Store config in the environment. Example: Using environment variables to manage configuration.
Setting environment variables:
export DATABASE_URL=jdbc:mysql://localhost:3306/mydb

4. Backing Services

Treat backing services as attached resources. Example: Configuring a database connection in Spring Boot.
This allows you to switch databases easily without changing the code.

5. Build, Release, Run

Strictly separate build and run stages. Example: Using Jenkins to manage build and release pipelines. Build stage: Compile the code, run tests, and package the application.
mvn clean package
Release stage: Combine the build with environment-specific configuration.
java -jar target/myapp.jar --spring.config.location=/path/to/config/
Run stage: Execute the application.
java -jar target/myapp.jar

6. Processes

Execute the app as one or more stateless processes. Example: Running a Spring Boot application as a stateless process.
java -jar target/myapp.jar
State (like session data) is stored in external services like Redis.

7. Port Binding

Export services via port binding. Example: Configuring a Spring Boot application to run on a specific port.
Accessing the application:
curl http://localhost:8080

8. Concurrency

Scale out via the process model. Example: Running multiple instances of a Spring Boot application using Docker.
docker run -d -p 8080:8080 myapp:latest
docker run -d -p 8081:8080 myapp:latest
Load balancing these instances using Nginx or another load balancer.

9. Disposability

Maximize robustness with fast startup and graceful shutdown. Example: Implementing graceful shutdown in Spring Boot.
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        connector.setProperty("server.shutdown.graceful", "true");
    return tomcat;

10. Dev/Prod Parity

Keep development, staging, and production as similar as possible. Example: Using Docker to ensure the same environment in all stages.
docker build -t myapp:latest .
docker run -e DATABASE_URL=jdbc:mysql://localhost:3306/mydb myapp:latest

11. Logs

Treat logs as event streams. Example: Using a logging framework like Logback to write logs to stdout.
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
    <root level="info">
        <appender-ref ref="STDOUT" />

12. Admin Processes

Run admin/management tasks as one-off processes. Example: Running a database migration using Flyway in a Spring Boot application.
java -cp myapp.jar org.flywaydb.core.Flyway migrate

Developing a Microservice with Spring Boot: Applying the Twelve-Factor Methodology

Developing a microservice using Spring Boot involves several steps, from setting up your development environment to implementing features and ensuring the application adheres to best practices, including the twelve-factor methodology. Here's a step-by-step guide:

Step 1: Set Up Your Development Environment

  1. Install JDK: Ensure you have Java Development Kit (JDK) installed. Spring Boot typically requires JDK 8 or later.
  2. Install an IDE: Use an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code.
  3. Install Maven/Gradle: These build tools help manage project dependencies and build lifecycle. Maven is commonly used with Spring Boot.

Step 2: Create a Spring Boot Application

  1. Initialize the Project:
    • Use Spring Initializr ( to generate a basic Spring Boot project. Select dependencies such as Spring Web, Spring Data JPA, and any database connector (e.g., H2, MySQL).
    • Download the generated project and import it into your IDE.
  2. Structure Your Project: A typical Spring Boot project follows the Maven structure:
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── example
    │   │   │           └── mymicroservice
    │   │   │               ├──
    │   │   │               ├── controller
    │   │   │               ├── service
    │   │   │               └── repository
    │   │   ├── resources
    │   │       └──
    │   └── test

Step 3: Develop Your Microservice

  1. Define Models: Create Java classes representing your data model.
    package com.example.mymicroservice.model;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    public class Product {
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String name;
        private Double price;
        // getters and setters
  2. Create Repositories: Use Spring Data JPA to handle database interactions.
    package com.example.mymicroservice.repository;
    import com.example.mymicroservice.model.Product;
    public interface ProductRepository extends JpaRepository<Product, Long> {
  3. Implement Services: Write business logic in service classes.
    package com.example.mymicroservice.service;
    import com.example.mymicroservice.model.Product;
    import com.example.mymicroservice.repository.ProductRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    public class ProductService {
        private ProductRepository productRepository;
        public List getAllProducts() {
            return productRepository.findAll();
        public Product getProductById(Long id) {
            return productRepository.findById(id).orElse(null);
        public Product saveProduct(Product product) {
        public void deleteProduct(Long id) {
  4. Create Controllers: Define REST endpoints to handle HTTP requests.
    package com.example.mymicroservice.controller;
    import com.example.mymicroservice.model.Product;
    import com.example.mymicroservice.service.ProductService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import java.util.List;
    public class ProductController {
        private ProductService productService;
        public List getAllProducts() {
            return productService.getAllProducts();
        public Product getProductById(@PathVariable Long id) {
            return productService.getProductById(id);
        public Product createProduct(@RequestBody Product product) {
            return productService.saveProduct(product);
        public void deleteProduct(@PathVariable Long id) {

Step 4: Apply the Twelve-Factor Methodology

  1. Codebase: One codebase tracked in version control, many deploys. Use a version control system like Git.
  2. Dependencies: Explicitly declare and isolate dependencies. Manage dependencies using Maven or Gradle.
  3. Config: Store config in the environment.
  4. Backing Services: Treat backing services as attached resources. Configure external resources (e.g., databases, message brokers) using environment variables.
  5. Build, Release, Run: Strictly separate build and run stages. Use CI/CD pipelines to automate the build and deployment process.
  6. Processes: Execute the app as one or more stateless processes. Ensure the application is stateless, storing any needed state in a database or external service.
  7. Port Binding: Export services via port binding. Spring Boot applications bind to a port and serve requests.
  8. Concurrency: Scale out via the process model. Scale the application horizontally by running multiple instances.
  9. Disposability: Maximize robustness with fast startup and graceful shutdown. Implement graceful shutdown and ensure the application can handle interruptions.
  10. Dev/Prod Parity: Keep development, staging, and production as similar as possible. Use Docker or similar technologies to ensure consistency across environments.
  11. Logs: Treat logs as event streams. Use a logging framework like Logback and externalize logs to a centralized logging system.
  12. Admin Processes: Run admin/management tasks as one-off processes. Use tools like Spring Boot Actuator for management and monitoring.

Step 5: Testing and Deployment

  1. Write Tests: Implement unit tests, integration tests, and end-to-end tests.
    package com.example.mymicroservice;
    import com.example.mymicroservice.model.Product;
    import com.example.mymicroservice.service.ProductService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import static org.junit.jupiter.api.Assertions.assertNotNull;
    class MyMicroserviceApplicationTests {
        private ProductService productService;
        void testGetAllProducts() {
  2. Package and Deploy: Package the application as a JAR or WAR file and deploy it to your server or cloud platform.
    mvn clean package

Step 6: Monitoring and Maintenance

  1. Monitoring: Use tools like Prometheus and Grafana for monitoring application performance.
  2. Logging: Implement centralized logging using tools like ELK Stack (Elasticsearch, Logstash, Kibana).
  3. Security: Regularly update dependencies and use security practices like OAuth2 for authentication and authorization.
By following these steps and adhering to the twelve-factor methodology, you can develop a robust, scalable, and maintainable microservice using Spring Boot.


Post a Comment


Get in touch with me


Bangalore, India