-
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
Open
theamith
wants to merge
24
commits into
main
Choose a base branch
from
amithku/12-factor-app
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
12 factor app #122
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2d9fd30
12 factor app initial push
theamith 53e39e2
Upgrade angular 17.3.6 (#121)
robinsonzimmermann 0c00a8c
12 factor app initial push
theamith f96448d
Upgrade angular 17.3.6 (#121)
robinsonzimmermann 7d8793e
12 factor app initial push
theamith afb4e0e
Merge branch 'main' into amithku/12-factor-app
theamith 16f2ca1
Merge branch 'main' into amithku/12-factor-app
theamith e8037f8
Merge branch 'main' into amithku/12-factor-app
yauheni-bb 9d005e6
Merge branch 'main' into amithku/12-factor-app
theamith e161917
adding more reference links
theamith f31cb4b
Merge branch 'main' into amithku/12-factor-app
theamith 3133f14
add image and refactor content
theamith 38f3d61
codebase image theme update
theamith 2b28b3e
update image theme for config and backing services
theamith d339a4d
update concurrency theme
theamith 13c91f5
Merge branch 'main' into amithku/12-factor-app
theamith 7fa0d12
Merge branch 'main' into amithku/12-factor-app
theamith 08d3aa6
Merge branch 'main' into amithku/12-factor-app
theamith ad31a43
Merge branch 'main' into amithku/12-factor-app
theamith 9d86b2c
Merge branch 'main' into amithku/12-factor-app
theamith 8a69ab3
Merge branch 'main' into amithku/12-factor-app
theamith 4de0f08
Merge branch 'main' into amithku/12-factor-app
theamith fb6c1be
Merge branch 'main' into amithku/12-factor-app
theamith f1054ff
Merge branch 'main' into amithku/12-factor-app
theamith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+138 KB
...ns-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/12-factor-app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+111 KB
...ications-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+155 KB
...ications-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+211 KB
...ications-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+168 KB
...ications-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+217 KB
...plications-a-guide-to-12-factor-apps-with-java-and-spring-boot/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
202 changes: 202 additions & 0 deletions
202
...alable-applications-a-guide-to-12-factor-apps-with-java-and-spring-boot/post.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
Modern software demands robust, scalable apps. Enter 12-Factor Apps, your guide to building cloud-native champions. | ||
 | ||
|
||
Authors: Amith Kumar Madhusoodhanan | ||
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 | ||
|
||
--- | ||
## What are 12-Factor Apps? | ||
|
||
The 12-Factor App methodology, originally introduced by Heroku, outlines a set of guidelines for building applications that are: | ||
|
||
- **Portable**: Easy to deploy across different environments. | ||
- **Scalable**: Can readily scale up or down based on demand. | ||
- **Maintainable**: Simple to understand and modify. | ||
- **Disposable**: Can be easily restarted or replaced without impacting functionality. | ||
|
||
tarun-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Let's explore how to implement these principles using Java and Spring Boot: | ||
|
||
--- | ||
|
||
### 1. Codebase | ||
|
||
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. | ||
 | ||
|
||
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. | ||
 | ||
|
||
### 2. Dependencies | ||
|
||
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. | ||
|
||
```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 | ||
|
||
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): | ||
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 | ||
|
||
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. | ||
|
||
 | ||
|
||
### 5. Build, Release, Run | ||
|
||
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. | ||
- **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. | ||
- **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. | ||
|
||
 | ||
|
||
### 6. Processes | ||
|
||
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. | ||
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. | ||
|
||
### 7. Port Binding | ||
Bind applications to a port using environment variables. Spring Boot applications typically listen on a port defined in the application properties or environment variables. | ||
|
||
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 | ||
|
||
Leverage process-based concurrency for scaling. Spring Boot applications can be easily deployed across multiple instances to handle increased traffic. | ||
|
||
 | ||
|
||
|
||
### 9. Disposability | ||
|
||
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: | ||
|
||
```java | ||
import javax.annotation.PreDestroy; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MyComponent { | ||
|
||
@PreDestroy | ||
public void cleanup() { | ||
// Cleanup tasks | ||
} | ||
} | ||
``` | ||
|
||
### 10. Dev/Prod Parity | ||
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. | ||
|
||
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. | ||
|
||
### 11. Logs | ||
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.). | ||
|
||
[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 | ||
|
||
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. | ||
|
||
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. | ||
|
||
Spring Boot applications can be extended with custom admin commands for specific tasks. Here's an example of a scheduled task in Spring Boot: | ||
|
||
```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: | ||
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. | ||
- **Improved Scalability**: Stateless processes allow for easy scaling based on demand. | ||
- **Enhanced Portabilit**y: Applications can be deployed across different platforms with minimal changes. | ||
- **Simplified Maintainability**: Clear separation of code, configuration, and dependencies simplifies application management. | ||
|
||
## Code Examples | ||
|
||
tarun-bb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Here's a simple Spring Boot application demonstrating some 12-Factor principles: | ||
|
||
- [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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.