-
Notifications
You must be signed in to change notification settings - Fork 2
12 factor app #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
12 factor app #122
Changes from all commits
2d9fd30
53e39e2
0c00a8c
f96448d
7d8793e
afb4e0e
16f2ca1
e8037f8
9d005e6
e161917
f31cb4b
3133f14
38f3d61
2b28b3e
d339a4d
13c91f5
7fa0d12
08d3aa6
ad31a43
9d86b2c
8a69ab3
4de0f08
fb6c1be
f1054ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
# Building Reliable and Scalable Applications: A Guide to 12-Factor Apps with Java and Spring Boot | ||
Check warning on line 1 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Modern software demands robust, scalable apps. Enter 12-Factor Apps, your guide to building cloud-native champions. | ||
 | ||
|
||
Authors: Amith Kumar Madhusoodhanan | ||
Check warning on line 6 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
Date: unpublished | ||
Category: backend | ||
|
||
tags: 12-factor app, Java, Spring Boot, Cloud-native applications, DevOps, CI/CD, Microservices architecture, Docker, Containerization, Maven, Gradle, Configuration management, Dependency management, Backing services, Externalized configuration, Concurrency management, Logging, Monitoring, Scalability, Resilience | ||
Check warning on line 10 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
--- | ||
## What are 12-Factor Apps? | ||
Check warning on line 13 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
The 12-Factor App methodology, originally introduced by Heroku, outlines a set of guidelines for building applications that are: | ||
Check warning on line 15 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
- **Portable**: Easy to deploy across different environments. | ||
Check warning on line 17 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Scalable**: Can readily scale up or down based on demand. | ||
Check warning on line 18 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Maintainable**: Simple to understand and modify. | ||
Check warning on line 19 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Disposable**: Can be easily restarted or replaced without impacting functionality. | ||
Check warning on line 20 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
tarun-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Let's explore how to implement these principles using Java and Spring Boot: | ||
Check warning on line 22 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
--- | ||
|
||
### 1. Codebase | ||
Check warning on line 26 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Maintain a single codebase for your application, typically stored in a centralized version control system like Subversion or decentralized version control system like Git. This ensures consistent code across development, staging, and production environments. | ||
Check warning on line 28 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
 | ||
|
||
Multiple apps should not share same codebase, rather shared code as libraries which can be added as [dependencies](#2-dependencies). In reality there can be multple deployments of an app from the same codebase, although code versions may differ in each deployment. | ||
Check warning on line 31 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
 | ||
|
||
### 2. Dependencies | ||
Check warning on line 34 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Declare all dependencies explicitly and isolate dependencies in a manifest file like a Maven POM (pom.xml) or Gradle build script. This makes sure that the dependecies are pulled during build phase and the does not assume the dependencies to be present on the build system/runtime. This helps to provide consistency between development and production environments, simplifies the setup for developers new to the application, and supports portability between cloud platforms. | ||
Check warning on line 36 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
```xml | ||
<project> | ||
<groupId>com.example</groupId> | ||
<artifactId>my-spring-boot-app</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<!-- Add other dependencies here --> | ||
</dependencies> | ||
</project> | ||
``` | ||
|
||
### 3. Configuration | ||
Check warning on line 54 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Store configuration details (database connection strings, API keys, and other [backing services](#4-backing-services)) outside your codebase as it is likely to vary between deployments(dev, stage, prod). Use environment variables to load configuration specific to each environment. Spring Boot allows for externalized configuration sources like property files and environment variables. Here's an example of configuring Backbase's user-manager service for [local development using docker-compose](https://github.com/Backbase/local-backend-setup/blob/main/development/docker-compose/docker-compose.yaml): | ||
Check warning on line 56 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as discussed offline, lets have dos and don't for each factor for consistency |
||
```yaml | ||
user-manager: | ||
image: repo.backbase.com/backbase-docker-releases/user-manager:${BB_VERSION} | ||
ports: | ||
- "8060:8080" | ||
environment: | ||
<<: [*common-variables, *message-broker-variables, *database-variables] | ||
spring.datasource.url: jdbc:mysql://mysql:3306/user-manager?useSSL=false&allowPublicKeyRetrieval=true&cacheServerConfiguration=true&createDatabaseIfNotExist=true | ||
backbase.users.identity-endpoints-enabled: true | ||
backbase.users.identity-integration-enabled: true | ||
backbase.users.sync-primary-to-identity-enabled: true | ||
spring.cloud.discovery.client.simple.instances.user-integration-outbound-service[0].uri: http://wiremock:8080 | ||
volumes: | ||
- ./exe/HealthCheck.jar:/tmp/HealthCheck.jar | ||
healthcheck: | ||
<<: *healthcheck-defaults | ||
test: [ "CMD", "java", "-jar", "-Xms5M", "-Xmx10M", "/tmp/HealthCheck.jar", "http://registry:8080/eureka/apps/user-manager", "<status>UP</status>" ] | ||
depends_on: | ||
mysql: | ||
condition: service_healthy | ||
links: | ||
- activemq | ||
- registry | ||
``` | ||
|
||
|
||
### 4. Backing Services | ||
Check warning on line 83 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Treat backing services (databases and other external services) as attached resources which can be accessed via a URL or credentials stored in the [config](#3-configuration). A deployment of the twelve-factor app should be able to swap out a local MySQL database with one managed by a third party (such as Azure SQL Database) without any changes to the app’s code. In Backbase we have helm charts which contains backing services configuations for all environments. This allows to deploy the application without making code change and re-packaging. | ||
Check warning on line 85 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
 | ||
|
||
### 5. Build, Release, Run | ||
Check warning on line 89 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Separate the build, release, and run stages. | ||
- **build**: Build automation tools like maven can pull the [dependencies](#2-dependencies) and package into a docker image which can be pushed to a docker repository. | ||
Check warning on line 92 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **release**: Combining the docker image generated from build process with the environment specific [configuration](#3-configuration) will produce a release and the release should be labelled with unique ID. | ||
Check warning on line 93 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **run**: The deployed app runs in an execution environment, by launching app's [process](#6-processes). A continous deployment tool like Argo CD, usually uses tooling like containers and processes to launch the application. Once that operation is running, the cloud runtime is responsible for its maintenance, health, and dynamic scaling. | ||
Check warning on line 94 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
 | ||
|
||
### 6. Processes | ||
Check warning on line 98 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Treat applications as a stateless processes and [share-nothing](http://en.wikipedia.org/wiki/Shared_nothing_architecture). Any data that needs to persist must be stored in a stateful [backing service](#4-backing-services), typically a database. | ||
Check warning on line 100 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
Applications build using REST as transport protocol using JAX-RS RESTful architecture are stateless. REST based microservices can scale up and down without losing any information. While designing REST applications "sticky sessions" (caching user session data in app memory) should not be used and it is a violation of twelve-factor. Session state data is a good candidate for a datastore that offers time-expiration, such as Memcached or Redis. | ||
Check warning on line 101 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
### 7. Port Binding | ||
Check warning on line 103 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
Bind applications to a port using environment variables. Spring Boot applications typically listen on a port defined in the application properties or environment variables. | ||
Check warning on line 104 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Spring Boot allows configuring the port dynamically using environment variables. Below is an example configuration in `application.properties`: | ||
|
||
```properties | ||
# Server port | ||
server.port=${PORT:8080} | ||
``` | ||
|
||
### 8. Concurrency | ||
Check warning on line 113 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Leverage process-based concurrency for scaling. Spring Boot applications can be easily deployed across multiple instances to handle increased traffic. | ||
Check warning on line 115 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
 | ||
|
||
|
||
### 9. Disposability | ||
Check warning on line 120 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Design applications to be fast to start and stop. Spring Boot applications typically have quick startup times, making them suitable for containerization. | ||
|
||
Spring Boot applications can gracefully handle shutdown signals. You can perform cleanup tasks using `@PreDestroy` annotation: | ||
Check warning on line 124 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
```java | ||
import javax.annotation.PreDestroy; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MyComponent { | ||
|
||
@PreDestroy | ||
public void cleanup() { | ||
// Cleanup tasks | ||
} | ||
} | ||
``` | ||
|
||
### 10. Dev/Prod Parity | ||
Check warning on line 140 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
theamith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Keep development, staging, and production environments as similar as possible to ensure that all potential bugs/failures can be identified in development and testing instead of when the application is put into production. By using environment variables and externalized configuration, you can achieve parity between environments. | ||
Check warning on line 142 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Docker Containers enable the creation and use of the same image in development, staging, and production. It also helps to ensure that the same backing services are used in every environment. Utilizing this concept of containerization, testing tools like MicroShed Testing enable us to ensure that the testing environment is as close to production as possible, too. | ||
Check warning on line 144 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
### 11. Logs | ||
Check warning on line 146 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
yauheni-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Treat logs as event streams. Cloud-native applications can elastically scale from 1 to over 100 instances, which makes it difficult to track those instances and organize all of the logs. Spring Boot integrates seamlessly with logging frameworks like Logback, allowing you to centralize log collection and analysis using tools like (e.g., ELK stack, Splunk, Sumologic, etc.). | ||
Check warning on line 148 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
[Disposing](#9-disposability) an instance should not cause logs to vanish. Spring Boot uses SLF4J and Logback by default and helps to configure stdout and stderr logs. You can configure logging levels and appenders in `logback-spring.xml`: | ||
|
||
```xml | ||
<configuration> | ||
<root level="INFO"> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
</configuration> | ||
``` | ||
|
||
### 12. Admin Processes | ||
Check warning on line 160 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Run administrative tasks as one-off processes and they can be run as Kubernetes tasks. This factor discourages putting one-off admin or management tasks like migrating databases and running one-time scripts to do clean-up inside your microservices instead microservice should focus on business logic. | ||
Check warning on line 162 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
While running admin process make sure to run in an identical environment as the regular app. They should use the same [codebase](#1-codebase) and [config](#3-configuration) as the app to avoid synchronization issues. | ||
Check warning on line 164 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
Spring Boot applications can be extended with custom admin commands for specific tasks. Here's an example of a scheduled task in Spring Boot: | ||
Check warning on line 166 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
```java | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ScheduledTasks { | ||
|
||
@Scheduled(fixedRate = 10000) | ||
public void performDatabaseCleanup() { | ||
// Database cleanup task | ||
} | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Benefits of 12-Factor Apps: | ||
Check warning on line 184 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
tarun-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- **Increased Developer Productivity**: Consistent environments and clear separation of concerns make development and maintenance easier. | ||
Check warning on line 186 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Improved Scalability**: Stateless processes allow for easy scaling based on demand. | ||
Check warning on line 187 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Enhanced Portabilit**y: Applications can be deployed across different platforms with minimal changes. | ||
Check warning on line 188 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
- **Simplified Maintainability**: Clear separation of code, configuration, and dependencies simplifies application management. | ||
Check warning on line 189 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
## Code Examples | ||
Check warning on line 191 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
tarun-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Here's a simple Spring Boot application demonstrating some 12-Factor principles: | ||
Check warning on line 193 in content/posts/unpublished/building-reliable-and-scalable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
|
||
|
||
- [Backbase golden sample services](https://github.com/Backbase/golden-sample-services) | ||
|
||
## References | ||
|
||
- [12factor.net](https://12factor.net/) | ||
- [IBM Developer](https://developer.ibm.com/articles/creating-a-12-factor-application-with-open-liberty/) | ||
- [Redhat](https://www.redhat.com/architect/12-factor-app-containers) | ||
- [Google cloud](https://cloud.google.com/architecture/twelve-factor-app-development-on-gcp) |
Uh oh!
There was an error while loading. Please reload this page.