Quick revision
The short answer
I would cut profiles back to a few environment-level choices, name the valid combinations with profile groups (spring.profiles.group) so environments activate one group instead of a hand-made list, and turn feature choices such as kafka or nokafka into explicit properties. Most environment-specific values move out of profile files in the jar into deployment configuration, such as environment variables and config trees, and CI starts the application once for every supported combination, so a broken one fails the build instead of production.
What to remember
In this answer, we saw that bugs from profile combinations come from too many independent profiles whose order and conditions interact. We simplify by keeping profiles for a few environment-level choices, naming valid combinations with spring.profiles.group, replacing feature profiles such as kafka and nokafka with explicit properties and @ConditionalOnProperty, moving environment values from profile files in the jar to deployment configuration, and starting the context for every supported combination in CI.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would cut profiles back to a few environment-level choices, name the valid combinations with profile groups (spring.profiles.group) so environments activate one group instead of a hand-made list, and turn feature choices such as kafka or nokafka into explicit properties. Most environment-specific values move out of profile files in the jar into deployment configuration, such as environment variables and config trees, and CI starts the application once for every supported combination, so a broken one fails the build instead of production.
Why this question is asked
Profiles start as a clean idea: dev, test, prod. Then someone adds eu and us, then kafka and nokafka for a migration, then a profile for a customer, and soon each environment activates five profiles in its own order. With eight profiles there are hundreds of possible combinations, the team tests three of them, and production runs a fourth. Interviewers ask this scenario because it is common in real codebases and the solution needs both Spring knowledge and design judgement. In this answer, we will see why combinations cause bugs, then simplify in steps: keep profiles for environment choices, name the valid combinations with groups, turn features into properties, move values out of the jar, and test every supported combination in CI.
1Why Combinations Cause Bugs
A profile does two things: it adds property values from profile-specific files or documents, and it switches beans on or off with @Profile. When several profiles are active, the order in which they are listed decides which property file wins for a shared key, and each @Profile condition is evaluated on its own. So prod,eu,kafka and prod,kafka,eu can give different values for the same key, and a combination like stage,nokafka,kafka, which nobody intended, activates beans that conflict.
There is also a counting problem. With eight independent profiles there are 256 possible sets. Nobody can reason about 256 configurations, so in practice we only know the behaviour of the few we run. The goal is to make the number of valid configurations small, named and tested.
2Keep Profiles for Environment-Level Choices
The first decision is what a profile is for. I would keep profiles only for choices about the kind of environment, for example local for a developer machine with embedded or containerised dependencies, and cloud for anything deployed. Some teams also keep test-only profiles. Regions, features and customers are not environment kinds, so they should not be profiles.
A small rule is easy to enforce in review: a new profile needs a reason why a property cannot do the job. That alone stops most of the growth.
3Name Valid Combinations With Profile Groups
Where we do need several profiles together, a profile group gives the combination a name. Activating the group activates its members in a fixed order, so every environment activates exactly one name and the order is no longer typed by hand in each deployment.
Notice that groups must be declared in a document that is not profile-specific, for the same reason as spring.profiles.active. Groups are a good intermediate step: they document the supported combinations and remove ordering mistakes, even before we reduce the number of profiles.
4Turn Feature Toggles Into Explicit Properties
kafka and nokafka are not environments; they are a feature switch with two states expressed as two names that can both be on, or both off. A single property with a default expresses the same choice with no invalid states.
So the property has one value at a time, which makes the conflicting combination impossible, and it has a default, which makes the missing combination impossible too. A property also shows up in the Actuator env endpoint and in configuration metadata, so operators can see the choice. The same treatment applies to regional behaviour: a property such as app.region: eu, bound to a typed @ConfigurationProperties class, is clearer than eu and us profiles.
5Move Environment Values Out of the Jar
The profile files are often full of values that simply differ per environment: URLs, pool sizes, topic names. Keeping them in application-prod-eu.yml inside the jar means every value change needs a new build, and the jar contains knowledge of every environment. Moving those values into deployment configuration, such as environment variables in the Helm values file or a ConfigMap mounted as a config tree, leaves the jar with safe defaults and the deployment with its own differences, reviewed where the deployment is reviewed.
| Setting | Before | After |
|---|---|---|
| Environment kind | dev, qa, stage, prod profiles | two profiles: local and cloud |
| Region | eu and us profiles | app.region property |
| Messaging feature | kafka and nokafka profiles | events.transport property |
| URLs, sizes, topics | application-prod-eu.yml in the jar | deployment env vars or config tree |
| Valid combinations | typed per deployment | profile groups, tested in CI |
6Test Every Supported Combination in CI
Finally, we make the supported set executable. A parameterised test starts the application context once for each named group, with external systems replaced by test containers or mocks, and fails if any bean is missing, duplicated or misconfigured. With five supported combinations that is five context starts, a minute or two in CI, and it turns a production surprise into a red build.
This test also documents the supported list. When someone adds a new combination, they add it here, and the review discussion happens before production.
7Follow-up questions
How would you migrate without a big-bang change?
One profile at a time. Introduce groups first so deployments use one name, then replace one feature profile with a property, keeping the old profile as an alias that sets the property for a release, and remove it once no deployment uses it. Each step is small and can be verified with the env endpoint.
Is @ConditionalOnProperty better than @Profile for features?
For features, yes. A property has one value at a time, can have a default, and is visible as configuration. @Profile is a set membership test, so two contradictory profiles can be active together, and nothing tells us which features a profile list really enables.
Should local development still use a profile?
Yes, that is one of the few places profiles shine. A local profile can switch on Docker Compose support, test containers or relaxed security for a developer machine, and it never ships to a real environment.
8Conclusion
In this answer, we saw that bugs from profile combinations come from too many independent profiles whose order and conditions interact. We simplify by keeping profiles for a few environment-level choices, naming valid combinations with spring.profiles.group, replacing feature profiles such as kafka and nokafka with explicit properties and @ConditionalOnProperty, moving environment values from profile files in the jar to deployment configuration, and starting the context for every supported combination in CI.
Open with
The root problem is that eight independent profiles give hundreds of possible combinations, and both the order of profiles and contradictory pairs like kafka and nokafka change behaviour, while we only test a few.
Then, if they want more
I would simplify in steps. First, profiles are only for environment-level choices, ideally just local and cloud, plus test profiles, and any new profile needs a reason a property cannot do the job. Second, the combinations we really need get names with spring.profiles.group, declared in the default document.
A deployment then activates one name like prod-eu instead of a hand-typed list. Third, features become explicit properties. kafka and nokafka become one property, events.transport, with a default, and the beans use @ConditionalOnProperty, so exactly one publisher exists and contradictory states are impossible. The same goes for region, which becomes app.region in a typed properties class.
Fourth, I move environment values like URLs and pool sizes out of profile files in the jar into deployment configuration, meaning environment variables or a config tree from a ConfigMap. The jar keeps safe defaults. Finally, CI runs a parameterised test that starts the application context once for every supported group, so a broken combination fails the build.
I would migrate gradually, one profile at a time, checking the env endpoint before and after.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A codebase has profiles dev, qa, stage, prod, eu, us, kafka, nokafka and more, combined...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/ef20d5fbd9/