In a previous post, I talked about why immutability is so important. After writing that post, I came to see immutability in different forms at many places. One such place is Configuration and Immutable Infrastructure.
Immutable Configuration means, your configuration will not change once deployed. It’ll remain the same. It’s a combination of two things:
- Your application does not maintain a state and neither maintains a database.
- If you need to make changes, you don’t make changes to the existing deployment. You’ll instead create a new deployment with the changes.
- The configuration is usually via JSON.
This has a few advantages:
- Your deployments are clear: You know the if you have deployed something, they are exact. For example, if you are deploying with new changes and it fails. You still have the previous perfectly running deployment state.
- Immutable means this is fast. Immutable infrastructures don’t have to provision for changes.
One of the most common places to use immutable infrastucture is Gateways. You set up the gateway once by providing config in form of
yaml. Once that config is provided, there is no way to make changes to it.
Considerations when building your own immutable infra
Finding Good Use case
If you have a functionality where:
- You have a set of configurations that are less likely to change.
- The functionality does not need to maintain state.
You can choose to use immutable configuration.
For example, you have a list of items to be displayed that do not change regularly. You can save these as JSON configuration instead of DB. This way when you want to read that data, you don’t have to make a network call to the DB. This makes your config fast.
There is always a tradeoff associated.
If you are using Spring Boot, you can use
yaml files to load up configuration at run time. For
You can also create separate
json files for your configuration.
Try to make these configurations modular.
Validate your configuration
Run a script when the application starts to make sure all your configurations are loaded properly. The application should fail to start if the configuration provided isn’t provided well.
If you are using Spring Boot
@Validated would work.
json, you can create JSON Schema annotations.