Configuration Management
Configuration management means separating configuration from code so the same code runs in different environments.
Why Externalize Configuration?
Development → localhost:5432, debug=true, log=DEBUG
Staging → staging-db:5432, debug=false, log=INFO
Production → prod-db:5432, debug=false, log=WARN
You don't want to change code for each environment. Spring makes this easy.
application.yml (Primary Config)
# application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: ${DB_USERNAME:postgres}
password: ${DB_PASSWORD:password}
jpa:
hibernate:
ddl-auto: validate
show-sql: false
app:
jwt:
secret: ${JWT_SECRET:myDefaultSecret}
expiration: 86400000
logging:
level:
root: INFO
com.example: DEBUG
Profile-Specific Configuration
# application-dev.yml (Development)
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb_dev
jpa:
show-sql: true
hibernate:
ddl-auto: create-drop
logging:
level:
com.example: DEBUG
---
# application-prod.yml (Production)
spring:
datasource:
url: jdbc:postgresql://prod-db:5432/mydb
jpa:
show-sql: false
hibernate:
ddl-auto: validate
logging:
level:
root: WARN
com.example: INFO
Activating Profiles
# Via command line
java -jar app.jar --spring.profiles.active=prod
# Via environment variable
SPRING_PROFILES_ACTIVE=prod java -jar app.jar
# In application.yml
spring:
profiles:
active: dev
@ConfigurationProperties (Type-Safe Config)
@ConfigurationProperties(prefix = "app.jwt")
public class JwtProperties {
private String secret;
private long expiration;
private String issuer;
// Getters and Setters
}
@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class AppConfig {
}
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding Configuration Management is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Configuration Management in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Configuration Management implementation
// Key aspects: validation, error handling, logging, testing
public class ConfigurationManagement {
// Production-ready implementation
}Identify and handle edge cases for Configuration Management. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Configuration Management. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How do you activate a Spring profile?
2. What is the purpose of @ConfigurationProperties?
3. What is the primary purpose of Configuration Management?
4. What is a common mistake when implementing Configuration Management?
Flashcards
Question
What is Spring configuration externalization?
Click to reveal answer
Answer
Separating config from code so the same app runs in different environments
Question
How do you activate a profile?
Click to reveal answer
Answer
spring.profiles.active=dev via YAML, CLI args, or env var
Question
What is Configuration Management?
Click to reveal answer
Answer
Configuration Management is a key concept in backend development.
Question
When to use Configuration Management?
Click to reveal answer
Answer
Use Configuration Management when building production systems that require reliability, scalability, and maintainability.
Question
Configuration Management best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Externalize configuration from code to run the same app in different environments
- 2.Use application.yml for primary config and profile-specific files for overrides
- 3.Activate profiles via command line (--spring.profiles.active=prod), environment variables (SPRING_PROFILES_ACTIVE=prod), or application.yml
- 4.@ConfigurationProperties provides type-safe configuration binding to Java objects
- 5.Use ${VAR:default} syntax for environment variable fallbacks with defaults
Interview Tips
- •Know how to activate Spring profiles using multiple methods (CLI, env vars, YAML)
- •Explain the difference between application.yml and profile-specific configuration files
- •Discuss why externalizing configuration is important for production deployments
Cheat Sheet
Configuration Management
- Use application.yml as primary config
- Profile-specific: application-{profile}.yml
- Activate: --spring.profiles.active=prod, SPRRING_PROFILES_ACTIVE=prod, or spring.profiles.active in YAML
- @ConfigurationProperties: type-safe binding with prefix
- Environment variables: ${DB_USERNAME:postgres} with defaults
- @Profile annotation to conditionally activate beans