Search code examples
node.jsexpressmicroservicesexpress-gateway

Fallback to another service endpoint if the first is busy according to status code


How is possibile to fallback to another API service if the first try fails with status code of 429 or 500?

Consider this situation: The gateway should first try a microservice host, but if it timeouts or answer a non 2xx status code it should try a next one.


Solution

  • This is not supported in Express Gateway out of the box. If a request is failing, you'll get a failed request.

    Right now you can specify multiple urls for a serviceEndpoint that will be used, but in a round robin manner; therefore you'll still get an error first, before trying again with the second service.

    Express Gateway could — and maybe should — handle such case. In meantime I'd suggest you to look to other alternatives offered by your infrastructure. Consul could be the way to go.