Integrated Backend Design While Preserving Service Boundaries and the Process of Promoting Common Auto-Configuration
1. Introduction
As the number of microservices increases, it is not only business functionality that grows. Each service requires a Pod and a JVM, and a Spring Context, thread pool, and connection pool are created inside each JVM. The deployment and monitoring targets also increase in proportion to the number of services. If even services with low traffic are maintained as independent execution units, the service boundaries remain clear, but this comes at the cost of repeated runtime resources and operational overhead.
The direct motivation for reviewing an integration approach was a healthcare project. At the time, approximately 90 Bounded Contexts had been identified, and assuming that each BC contained 3–5 services, 250–300 execution instances might have been required. This is a natural configuration if every service must scale independently, but the fact that the same execution cost would be incurred even for services with low actual load required further consideration.
We did not intend to introduce MultiDatasource from the beginning. The starting point was the following question.
Can we reduce the recurring cost of Pods and JVMs while preserving the logical boundaries of services?
To address this question, this article summarizes the integration approaches we reviewed, the source-level integration PoC, the process of applying MultiDatasource, and our experience resolving Spring Context conflicts. It also explains why configurations repeated across multiple services were promoted to Prologue's common auto-configuration and describes the results of applying it.
2. The Number of Pods and the Number of JVMs Are Different Problems
The first approach that comes to mind is to place multiple containers in a single Pod. From a Kubernetes perspective, this can reduce the number of Pods, and containers in the same Pod can share a network namespace. However, if an independent Spring Boot application is run in each container, the number of JVMs remains unchanged.
Each JVM independently has its own heap, GC, thread pool, Spring Context, and connection pool. Therefore, combining only the Pods while retaining the JVMs may reduce the number of Kubernetes objects, but it does not sufficiently reduce the duplicated runtime costs that this work aimed to address.
To substantially reduce operational resources, we needed to assemble modules from multiple services into a single Boot application and run them within one JVM and Spring Context.
Figure 1. Transition from independent Pod and JVM configurations to an integrated Boot configuration
This does not mean that we intend to redesign multiple services as a single service. The key is to combine the deployment and execution units while preserving each service's code and data ownership.
3. Reversibility Was the Goal of Integration
The main applications currently using this approach are configurations that assemble Gate and Guard, Metro and Inflo, and Bro and Porto into a single Boot application. Each case assembles two services, but the structure itself is not limited to two services. By adding services to the topology, five or more services can also be assembled into a single Boot and JVM according to site requirements.
After integration, traffic for a particular service may increase, or stronger fault isolation may become necessary. In such cases, it must be possible to separate that service again into an independent execution unit. If integration becomes a one-way change, we may lose long-term operational flexibility in exchange for short-term resource savings.
For these reasons, we established the following principles.
• Share execution resources wherever possible.
• Preserve the package and Gradle module boundaries for each service.
• Preserve data access and transaction boundaries for each service.
• Allow the service composition required for each site to be changed.
• Ensure that integrated services can be separated again into independent Boot applications when necessary.
The main source of the cost problem was the duplication of Pods and JVMs, not the DB. At the time, there were no significant issues with DB resources. If the DB configuration were changed at the same time, data ownership, migration, the scope of failures, and the rollback strategy would all change at once. Therefore, we treated runtime integration and DB integration as separate decisions.
We integrated the execution units while preserving the existing persistence boundaries for each service. This was the background for using multiple DataSources in a single Boot application.
4. We Reviewed Multiple Integration Approaches
There was more than one way to integrate the runtime. We reviewed approaches such as placing multiple containers in a Pod, combining existing JARs on the classpath, using Parent and Child ApplicationContexts, and assembling services at the source level into a single build graph. We also examined proxies or shared connection pools for reducing DB connections themselves, as well as Native Image for reducing runtime memory.
|
Approach |
JVM Reduction |
Impact on Existing Code |
Service Composition Flexibility |
Review Result |
|---|---|---|---|---|
|
Multiple containers in a single Pod |
Low |
Low |
High |
The number of Pods is reduced, but JVM costs remain. |
|
Combining existing JARs on the classpath |
High |
Low |
High |
Managing consistency between Config and artifact versions is complex. |
|
Parent and Child ApplicationContexts |
High |
Medium |
High |
Context isolation is possible, but implementation and operational complexity is high. |
|
Source-level integrated build |
High |
Medium |
High |
Conflicts can be detected early in a single build graph. |
|
Sharing a DB proxy or connection pool |
None |
Low |
Irrelevant |
It handles only DB connections and cannot reduce the number of JVMs. |
|
Native Image |
Partially possible |
High |
Irrelevant |
It does not directly resolve conflicts between service composition and the Spring Context. |
The JAR composition approach is also technically possible. Each repository can publish a library artifact rather than an executable Boot JAR, and a separate assembly project can select the required versions and package them into a single Boot JAR. However, simply combining applications designed for independent execution immediately before deployment proved difficult.
DataSource, JPA, Flyway, SecurityFilterChain, Component Scan, and Bean names were all configured on the assumption of independent execution. Managing the artifact version matrix for each service on top of this increases the complexity of deployment combinations.
Ultimately, we first validated an approach in which the source modules, excluding each service's Boot module, were assembled in a single Gradle workspace, with a Boot module dedicated to integration serving as the sole execution entry point. Although this approach incurs an initial integration cost, it made it possible to detect conflicts quickly during the compile and startup phases.
5. We validated integration feasibility with five services
In the PoC, we assembled Checkpoint, Metro, Gallery, Pigeon, and Vault into a single repository and a single Boot application. This configuration was intended to verify whether combinations of five or more services were possible, rather than merely supporting a pair of services.
integrated
├─ checkpoint-* modules
├─ metro-* modules
├─ gallery-* modules
├─ pigeon-* modules
├─ vault-* modules
└─ integrated-boot
The integration proceeded in the following order.
1. We brought in the service modules from each project, excluding the Boot modules responsible for independent execution.
2. We created integrated-boot as the entry point for the entire application.
3. We moved the Config objects in the existing Boot modules to the service modules or the integration Boot module according to their actual responsibilities.
4. We changed external artifact dependencies to Gradle project dependencies within the same build.
5. We explicitly configured client auto-configuration and removed duplicate Component registrations.
6. We started a single application and called the initialization API.
7. We verified that each service's data was stored correctly in its service-specific DataSource.
The functional validation showed that the five services ran in a single Pod and JVM, and that data was correctly loaded into each service's DataSource. This confirmed that the number of services that can be integrated is not fixed at two, but can scale according to the topology and resource limits.
However, this PoC did not measure CPU usage, heap usage, or the cost reduction rate under actual production traffic. We confirmed the structural effect of reducing the number of Pods and duplicated runtime resources, but did not directly express this as a cost-reduction figure. The actual savings must be measured in the production environment by considering CPU, heap, GC, thread count, and DB connections together.
6. The difficult part was the Spring Context, not packaging
During the PoC, the part that required more time than placing the source files in a single repository was resolving Bean and Config conflicts.
In independent applications, each Bean existed only once, but in the integrated Context they were discovered simultaneously. In particular, RestClient was registered as a @Component while also being created by AutoConfiguration, resulting in conflicts involving Bean names or types. When a specific client package was excluded from Component Scan, external integration clients with the same name were also excluded, causing another problem.
Successful compilation alone did not mean that the integration was complete. As a single Spring Context scanned all services together, duplicate registrations and conditional configuration issues that had not appeared in the independent execution environments surfaced.
This experience confirmed that the following rules are required for service modules that can be integrated.
• The integration Boot owns the execution entry point.
• A reusable client must be registered using only one of Component Scan and AutoConfiguration.
• Package names must represent service boundaries not only in routing, but also in scan and exclude rules.
• Service modules must not create duplicate Beans in independent and integrated execution modes.
• Common infrastructure configuration must be distinguished from service-specific adjustments.
Ultimately, the more important issue than the difference between source integration and JAR composition was enabling multiple services to coexist without conflicts in a single Spring Context. This experience became the criterion for determining the scope of subsequent Prologue standardization.
7. We preserved the DataSource and DB boundaries
Using a single JVM does not necessarily mean that there must be a single DataSource. Runtime boundaries and data boundaries are separate decisions.
Even after integration, each service retained the following persistence components independently.
• Each service retains its own DataSource.
• Each service retains its own EntityManagerFactory.
• Each service retains its own JpaTransactionManager.
• Each service retains its own EntityManager.
• Each service retains its own Spring Data JPA Repository scan scope.
• Each service retains its own Flyway migration.
Even when Gate and Guard are assembled into a single Boot application, their persistence units remain separated as follows.
Figure 2. Persistence boundaries of Gate and Guard maintained within a single Boot
With this structure, one service's Entity and Repository are not mixed into another service's persistence unit. Each service's migration history and execution order can be managed independently, and changes to the data layer can be minimized when a particular service is separated back into an independent Boot. Another important advantage is that integration of the execution units and changes to the DB topology can be decided separately.
On the other hand, each DataSource creates its own connection pool, so the overall connection budget must be divided among the services. If HikariCP's defaults are applied unchanged to every DataSource, the number of DB connections after integration may be higher than expected. Therefore, maximumPoolSize and minimumIdle for each pool, along with the DB's max_connections, must be calculated from the perspective of the entire integrated topology.
MultiDatasource itself is not the cause of the connection problem. However, if multiple pools are not managed together, it can become a new operational problem.
8. We selected transactions based on the package
Registering multiple DataSources alone is not sufficient. We must determine which TransactionManager a typical @Transactional should use.
You can repeatedly specify a qualifier in each service's code as follows.
@Transactional("gateTransactionManager")
However, with this approach, the service code must be directly aware of the integrated deployment structure. If the services are separated again or configured in a different combination, the number of places requiring modification also increases.
Because the service package was already being used as a stable logical boundary, we chose package-based routing.
io.vizend.gate.* → gateTransactionManager
io.vizend.guard.* → guardTransactionManager
other package → default service TransactionManager
If a developer explicitly specifies a qualifier, such as @Transactional("customTransactionManager"), that decision is preserved. Only when no qualifier is present do we connect the service and TransactionManager based on the package of the declaring class.
The @Primary EntityManager is provided as a routing proxy. If only one EntityManagerFactory is bound to the current transaction, it delegates to the shared EntityManager for that service. If multiple transaction resources are nested, it selects the target using the current transaction name and package rules. If there is no active transaction, it uses the configured default service.
This behavior must not be hidden as if it were magic. Moving a package can change transaction routing, and packages outside the rules fall back to the default service. Therefore, we fixed the package conventions, qualifier precedence, and fallback behavior through tests and documentation.
In addition, each service's JpaTransactionManager represents an independent local transaction. It does not combine changes to two DataSources into a single atomic transaction. If multiple databases must be changed together, requirements should be reviewed for REQUIRES_NEW, compensating actions, outbox, retries and idempotency, or a separate distributed transaction strategy.
9. We identified the repeated configuration in Gate, Metro, and Bro
Initially, the integrated Boot owned the MultiDatasource Config directly. Rather than putting it into a common library before the requirements and exceptions had been sufficiently verified, we decided it was safer to first confirm the necessary rules by applying them in actual services.
The situation changed as the same integration pattern spread to Gate, Metro, and Bro.
|
Integrated Boot |
Services assembled together |
Characteristics identified during application |
|---|---|---|
|
Gate |
Gate + Guard |
We confirmed the basic case of package-based transaction routing. |
|
Metro |
Metro + Inflo |
We confirmed failures in inter-service calls and transaction propagation boundaries. |
|
Bro |
Bro + Porto |
When some services were disabled, we had to select the actual default service again. |
|
Extension PoC |
Checkpoint + Metro + Gallery + Pigeon + Vault |
We confirmed that more than five services could also be assembled according to the same principles. |
The following code was repeated in each integrated Boot.
• Created DataSource and JPA Beans with consistent names.
• Configured Entity and Repository package scanning.
• Configured service-specific Flyway settings and execution.
• Registered the default Bean aliases.
• Handled whether each service was enabled.
• Configured TransactionManager and EntityManager routing.
Whenever the combination of services increased in production, we had to copy the same Config. When modifying common logic, we had to find every integrated Boot and apply the same change, and the likelihood that each service would implement it in a slightly different form also increased. Instead of reducing Pod operating costs through runtime integration, we had incurred new maintenance costs from repetitive Java Config and service-specific variations.
10. We elevated the repeated rules into Prologue
After identifying the common rules and exceptions in Gate, Metro, and Bro, we moved the MultiDatasource configuration into Prologue. This was not simply a matter of moving service code into a common module; it was the work of elevating rules repeatedly verified across multiple services into a platform contract.
Prologue's MultiDatasource auto-configuration uses an opt-in approach. To avoid affecting existing single-DataSource services, it operates only in applications that explicitly enable it. For consuming services, only which services to assemble and the package boundaries remain.
vizend:
prologue:
multi-datasource:
enabled: true
default-service: gate
services:
gate:
base-package: io.vizend.gate
guard:
enabled: ${vizend.guard.enabled:true}
base-package: io.vizend.guard
Connection information, HikariCP, and Flyway overrides remain in the existing service-specific datasource settings. Based on the topology, Prologue registers the persistence Beans and routing configuration for each service.
Figure 3. The role of Prologue in converting topology configuration into the actual persistence configuration
The boundary of commonization was also important. We did not push every difference into Prologue. Adjustments needed only for a particular service's call structure, such as the code-generated Store or name-based TransactionTemplate, were left in the consuming service.
A common library is not a place to absorb every exception. It should provide contracts that multiple services must follow consistently, while service-specific responsibilities should remain in each service.
11. What changed after commonizing with Prologue
First, there is less Java Config to copy when creating a new integrated Boot. Services can focus on the topology and connection information, while Prologue applies the Bean creation rules consistently.
Second, changes to the service combination became explicit configuration. A specific persistence topology can be excluded through the service's enabled value. We also handled combinations in which the configured default service is disabled, so that a default service can be selected from among the services that are actually active.
Third, the cost of integration and separation decreased. The transaction annotations and repository implementations in the service code do not need to know the deployment form directly. In an integrated Boot, Prologue handles routing; in an independent Boot, only that service's persistence configuration needs to be used.
Fourth, the contracts that need to be tested became clear. We organized the following items as the primary verification targets for the auto-configuration.
• Verify that Prologue does not create persistence Beans when the feature is disabled.
• Verify that all Beans required by each active service are created.
• Verify that the Repository uses the correct persistence unit.
• Verify that the standard @Transactional selects the TransactionManager appropriate for the package.
• Verify that explicit qualifiers are preserved.
• Verify that the default service is determined correctly according to the combination of enabled services.
• Verify that Flyway runs against each service’s DB and schema.
• Verify that the existing startup method for single-DataSource services is not affected.
It is difficult to evaluate standardization solely by the reduction in lines of code. The greater change was that we no longer need to repeatedly modify Config in every integrated Boot requiring the same changes, and new service combinations can now be described through topology declarations.
12. Lessons Learned During the Implementation
12.1 Architectural Changes Should Start with Operational Problems
MultiDatasource was not the goal. It was a means of preserving service boundaries while reducing the recurring costs of Pods and JVMs. Because the problem to be solved was clear, we were able to avoid changes unrelated to the problem, such as merging all DataSources into one.
12.2 Execution Boundaries and Data Boundaries Do Not Need to Move Together
Running services in a single JVM and using a single DB are independent choices. Separating these decisions reduces the scope of changes and increases the flexibility to separate the services again as they grow.
12.3 It Is Safer to Validate in Services First and Then Promote the Pattern to the Platform
If we had designed general-purpose auto-configuration from the beginning, it would have been difficult to fully understand actual Bean conflicts, inactive services, default service transitions, and service-specific adjustments. Because we standardized only after the same problems had recurred across multiple services, we were able to define Prologue’s responsibilities and boundaries concretely.
12.4 The More Convenient the Routing, the More Explicit the Contract Must Be
Package-based routing makes service code concise, but without understanding package rules, fallback behavior, and transaction limitations, its behavior can be difficult to predict. The more we automate, the more explicit the configuration, documentation, and integration tests must be.
12.5 Pod Reduction Indicates Cost-Saving Potential and Must Be Distinguished from Measurement Results
Combining services into a single JVM is likely to reduce duplicated heap and thread pools. On the other hand, the scope in which a single failure affects multiple services becomes larger, and the connection pools of multiple DataSources still need to be managed. The final evaluation should measure CPU, heap, GC, DB connections, and the scope of failures together in the actual deployment environment.
13. Conclusion
What we integrated in this work was execution cost, not service ownership or data boundaries.
We reduced the number of Pods and JVMs while preserving each service’s package, persistence unit, and transaction boundaries. Through a source-level integration PoC, we confirmed that five services could operate in a single Spring Context. As we subsequently applied the pattern mainly to Gate, Metro, and Bro, we discovered the problem of the same Config being repeated and promoted the sufficiently validated MultiDatasource rules to Prologue’s common auto-configuration.
This structure is not suitable for every service. Services for which independent scaling, different deployment cycles, or strong fault isolation are important may be better suited to separate Pods. The important point is not to commit to a single deployment form from the outset. By preserving service boundaries and data ownership, services can be combined or separated again according to the operational environment.
A good architecture is not completed simply by splitting things into small parts. It must be assembled to fit the current operational scale while preserving the necessary boundaries, and it must be possible to separate it again when circumstances change.
David