0

I have a Java Spring boot microservice in production which supports multi-tenancy and the tenants are isolated at database level. This microservice exposes REST APIs for communication. During a request, the tenant ID is passed as a header which is read by the filter and is put in ThreadLocal as a Tenant Context.

One of the API requires an internal call to another service say System A. One of the tenant requires to call System B rather than System A which is the default system for all other tenants.

enter image description here

To implement this diversion, I am thinking of a router strategy where depending on the context(which contains the tenant), it routes the request to one or the other spring service component. The strategy will read the data from ThreadLocal, determines the tenant and calls the appropriate spring service component. The code may roughly look like:

Map<String, SomeClass> tenantMap; //contains tenant ID to implementation/bean mapping from DB or config

public handle(String data){
    String currentTenantId = TenantContext.getTenantId();

    tenantMap.get(currentTenantId).handleFurther(data);
}

It does not seem convincing to me however as this needs to be implemented at all points of diversion. Which could lead to a need of huge configuration storage and lookup. Any suggestion if this is a good idea? Or is there exists any better idea?

3
  • What about API Gateways? An Internal one for this purpose.
    – Laiv
    Jun 20, 2023 at 9:02
  • @Laiv: Could you elaborate? Just to be more clear, the diversion logic needs to be contained in "MyService" and the interface of the System A and B could be different. So i have to make a choice internally, for example, user REST Consumer for Sys A or SOAP Consumer for Sys B
    – ashish.g
    Jun 20, 2023 at 9:55
  • 2
    I have to make a choice internally, for example, user REST Consumer for Sys A or SOAP Consumer for Sys Now think in big. Instead of 2 tenants, you have 100 each of which calls a different System N... Is not MyService already a gateway/router? If yes, why not move all this logic (which is unrelated to MyService business) to another system? One you can scale, replicate for HA, configure in runtime, and all other services can use?
    – Laiv
    Jun 20, 2023 at 11:31

1 Answer 1

2

"We can solve any problem by introducing an extra level of indirection."

Adding a configurable mapping of tenants to specialized handlers is such a level of indirection. It would solve the problem, but comes at a cost. Simpler solutions (such as an if-then-else statement to choose another route for tenant X) have less impact but also less flexibility. You need to weigh the cost against the possible gains to make a decision.

One thing to consider before embarking on a fully configurable router system, though, is YAGNI. Will the extra effort for a full solution actually be needed? If you're not really sure, think twice before investing the time and money. It may be sufficient to implement a tenant-specific route with simple configurability (like your example code) just where you need it now, and use that as a template when you later find that you need it in another place, too.

1
  • I can foresee the use of router at many places. Do you think tenant specific deployment would be helpful in that case? And use an API Gateway to determine which instance of "MyService" to call depending on tenant header? I am not very experienced in microservice domain. Is this how "they" do it?
    – ashish.g
    Jun 20, 2023 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.