5.2 Ambassador
Handle network communication on behalf of a serviceThis pattern is based on Ambassador defined by Burns [65, later 22, p. 21] and Microsoft [68], and Client Proxy by Buschmann et al. [54, p. 240]. It can also serve as a Channel Adapter by Hohpe et al. [4, p. 128, 69].
5.2.1 Context
This pattern handles the same problem described in § 5.3.1 – there is a need to offload commonly implemented functionality from services. Alternatively, a legacy service cannot be altered but may still need some functionality added to it.
5.2.2 Solution
Implement a small service that acts as an intermediary between the main service and the network (see fig. 7). This service can be deployed alongside the main service and enrich its networking capabilities without having to modify the existing service. It can also be reused across multiple services.
5.2.3 Potential issues
Compared to a shared library, using an ambassador will result in increased latency due to the extra network hop, added system complexity, and an additional point of failure. If an ambassador is shared across multiple services, it might become a bottleneck.
Consider the impact of offloaded features on the underlying service. For example, automatically employing Retry logic (see § 7.3) might cause problems if the service endpoint is not idempotent.
5.2.4 Example
ExampleEshop wants to try out a new version of its recommendation service with an updated model and evaluate its performance, but it is a risky operation, so it needs to be rolled out gradually. This is a temporary experiment, so they do not want to change the main service. However, as the recommendations are generated in real-time as users browse the site, it is critical for the experiment not to affect the latency of the application.
To achieve this, they deploy an ambassador service that intercepts requests to the recommendation service and forwards a percentage of them to the new version. Once the experiment is over, the ambassador service is removed without needing to change the main service. [22, p. 27]
5.2.5 Related patterns
An ambassador is commonly deployed as a Sidecar to improve performance
Patterns implementable in a gateway include Rate Limiting , Circuit Breaker , Retry , Partitioning and Health Monitoring
Compared to Offload to Gateway , this pattern has the added benefit of being able to be configured per service and be deployed as a sidecar for reduced latency. However, unlike it, it cannot be easily extended with aggregation
While an Anti-Corruption Layer lives in the same space as an ambassador, they differ in purpose. An ambassador can be reused across services; an ACL is, by definition, specific to a pair of services and exists to isolate domain-specific logic
The Gatekeeper pattern has a similar function but is deployed in a restricted environment for added security