Situation: A microservices architecture with:
- Microservices are written in Java with Spring Boot and Hibernate
- An options-service which provides information about Options via a REST interface
- Option data which could be modelled as follows e.g. via an enum (representative)
- Has some attributes like
enabled
status associated with it. It is unlikely that more attributes belonging to Option will be added in future which have to be directly tied to it. - New Options and OptionTypes have to be seldom added. They will never be deleted (just disabled if need be).
- An Option should have a unique identifier which can be referenced
- There should be no UNKOWN option if possible
- Has some attributes like
enum OptionType {
TYPE_A,
TYPE_B,
TYPE_C;
}
enum Option {
TYPE_A_X1(TYPE_A),
TYPE_A_X2(TYPE_A),
TYPE_B_Z1(TYPE_B, false),
TYPE_B_Z2(TYPE_B);
TYPE_C_U1(TYPE_C);
TYPE_C_U2(TYPE_C);
TYPE_C_U3(TYPE_C);
private final OptionType type;
private final boolean enabled;
Option(OptionType type){
this.type = type;
this.enabled = true;
}
Option(OptionType type, boolean enabled){
this.type = type;
this.enabled = enabled;
}
}
- Other microservices (currently 3) need to be able to access Option data. They need to know which Options exist and somehow reference an Option e.g. via its name or identifier
- One of those services (example-service) needs to provide the Option data type as filter settings in its own REST interface to the outside world. The filter object in JSON would look something like this:
{
"typeA": "TYPE_A_X1",
"typeB": "TYPE_B_Z2",
"typeC": [ "TYPE_C_U1", "TYPE_C_U2"]
// more filter settings
}
Different approaches of storing and sharing this Option data between microservices as I see it:
options-service stores Option data in its own database. If I read my data from database into my Hibernate entities Option is only a
String
everywhere from there on.- Pro:
- Easy to rename, add and remove Options
- Con:
- No type safety when working with Option in code e.g. when deserialising a response containing Option
- example-service cannot easily offer Option data in its OpenAPI doc (just
String
s) - Microservices need to query and cache Option data from options-service
- Pro:
Option data only lives in source code in an
enum
as e.g. modelled above and is shared between different services via a lib.- Pro:
- Type safety everywhere where Options are needed. Really useful when deserializing reponses containing Options data but also for generating OpenAPI doc
- Microservices can still reference an Option in its database via its name since it is unique
- Con:
- Editing the name of an Option is difficult
- Removing an Option not possible
- If a new Option/OptionType is added the order in which the services relying on that lib update their lib version matters. Since we cannot deserialize responses into an UNKNOWN Option type.
- Pro:
There is also the possibility of a mixed database and enum solution which comes with the big drawback that one has to maintain both sources of truth.
What is the best way to store and share the Option data between microservices? What is best practice?