I'm working on a service that's going to be accessed through a client library, and we have multiple instances of this service. Some instances are shared among various customers while others are dedicated to specific customers. In our database, we have a mapping of customer IDs to their respective instance IP addresses. I'm trying to figure out the best way to route requests to these specific instances. It's important to note that we already have an authentication process that will reject any requests made to the wrong instance, so my focus here is purely on how to handle the routing correctly.
One idea I had was to send all requests through a single load balancer or API gateway that uses a header containing the customer ID to route the requests to the appropriate instance. However, I'm looking into using managed load balancers from GCP or AWS, and I haven't found a way to set up detailed routing rules for individual instances; their routing conditions seem more suited for different APIs rather than directing requests to specific instances of the same API.
Another thought was to have the client library first connect to a shared service that knows the customer ID to instance IP mapping, retrieving the correct instance IP and then sending requests straight to that service, which would already have a load balancer. While this plan could work, it feels somewhat clunky and could introduce edge cases that we'd need to manage in the client library.
I'd love to hear how others might approach handling this request routing!
5 Answers
Just a side note, make sure to think about high availability and how you’d handle node outages. Having a resilient system is key, especially to avoid single points of failure.
Using an Nginx or Envoy proxy can be a great way to handle routing using headers. You set it up to parse incoming requests and route them appropriately based on the header values.
We actually do something similar in Kubernetes. Our setup has customers authenticate, and then they receive a header with a JWT. We route requests to the correct Kubernetes service based on that header. This approach is secure since any tampering with the header would result in invalid authentication. We use a centralized auth system (like FusionAuth) and an API gateway, and I think similar setups with Google Apigee or AWS API Gateway could work too.
There are a few strategies you could consider for routing:
1. **Host-based routing**: Assign a unique hostname for each tenant that points to their API.
2. **URL-based routing**: Use specific URI segments to redirect requests to the right host based on the tenant.
3. **Parameter-based routing**: Parse query parameters to identify the tenant and route accordingly.
I've found that the first method works best. The second is effective if you can guarantee your URLs will always be compliant, but the third method can be pretty fragile since developers might forget to include tenant parameters, leading to issues.
You might also want to consider header-based routing alongside the parameter-based approach. Just a heads up, though, whichever method you choose could potentially expose your service to targeted attacks, like DDoS, unless you incorporate some cryptographic elements to ensure secure routing. This could complicate things and break some levels of isolation, so having dedicated infrastructure to treat each tenant as a separate environment might be the way to go.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically