4.1 Gateway Routing
Abstract service locations from the clientsThis pattern is based on Gateway Routing by [45], Virtual Endpoint by [13, p. 64], and the API routing patterns by [46]. Hohpe et al. define this pattern more generically with the Message Router and Content-Based Router in the context of a Pipes and Filters architecture [4, pp. 91, 211, 47, 48].
This pattern is sometimes considered a part of the API Gateway pattern. For notes on why this work breaks it down, see § 4.1.7.
When creating a client-facing distributed system, one of the first apparent problems is how a client will interact with it since there is no longer a single endpoint. The easiest solution is to create a common routing service – a Gateway Router.
4.1.1 Context
Apply this pattern if there is a need to decouple service configuration from clients and at least one of the following conditions holds.
The client needs to communicate with multiple backend services, and their configurations change frequently, or it is not feasible to update the clients
There is one backend service, but it has multiple running instances or multiple versions, perhaps for progressive rollout or A/B testing
Traffic should be redirected to other working instances in case of failure or overload
4.1.2 Solution
Introduce a singular gateway component (see fig. 4). Clients interact with the system only through this component, containing all the necessary rules to route the requests. This can be based on several criteria, such as the hostname, URL, port or header. The gateway then forwards the request to the appropriate service. This way, the clients never need to know the actual service locations.
4.1.3 Potential issues
The biggest downside of this pattern is that it introduces a single point of failure – see chapter 7 on common techniques to reduce resiliency in similar situations. Another important aspect is the increase in latency due to an added jump in the requests and the introduction of a possible bottleneck to the system.
From a higher point of view, introducing this pattern makes it a very appealing point to add more features. This can accentuate the problems outlined above. [3, p. 162]
4.1.4 Example
ExampleEshop uses different APIs for its different frontend. In order to avoid having to update all its apps after changes in deployments, it uses a single entry point that routes the requests to the correct backend based on the path. Thanks to the amount of control this gives them, they can also use it to test new versions of the backend services, run experiments or re-route traffic in case of failures.
4.1.5 Related patterns
From another point of view, this pattern can be seen as an application of Partitioning
Instead of having a universal gateway, which might introduce a risk of handling too much, consider breaking it down into multiple Backends for Frontends to better split the concerns
Aggregating Gateway builds upon the router and consolidates multiple services calls into one to increase client performance
Use Offload to Gateway to offload common tasks to the gateway to simplify services
Introduce load balancing into the gateway with Competing Consumers
4.1.6 Further reading
- This functionality is often implemented in a Reverse Proxy. Nygard [12, p. 179] discusses their capabilities in the context of load-balancing.
4.1.7 API Gateway
Gateway routing is sometimes considered part of an API Gateway pattern. However, that pattern, by definition, is a set of multiple functionalities, and that coupling can lead to it being sometimes considered almost an anti-pattern [3, p. 162]. However, these discrete functionalities do not need to be implemented in one system. They can even be necessary in some use cases, so this work adopts the naming scheme propagated by [17], which splits the API Gateway into three discrete patterns – Gateway Routing, Aggregating Gateway (8.3) and Offload to Gateway (5.3) [45, 49, 50].
The following sources are still very useful for insight into the individual functionalities of an API Gateway, although they focus more on them being deployed as a whole.
Richardson [20, p. 260, 51] has a mostly positive take, arguing that they are an essential part of most systems and can be very useful
[3, p. 162], on the other hand, is much more sceptical of its use in, especially third-party solutions, but he admits it can have a use under the right conditions, i.e. when the team is small enough, and it does not handle aggregation and filtering
The API management Gateway component on Wikipedia [52]