API7 Enterprise's Canary Traffic Shifting Strategy for Precise Traffic Control
Tags: traffic, programming, tutorial, devops
Published: 2024-01-29 07:48:14

In a modern microservices architecture, the API gateway serves as the traffic entry of systems, handling crucial tasks such as request routing, identity authentication, rate limiting, and circuit breaker. [API7 Enterprise](https://api7.ai/enterprise), as a robust API management platform built on the APISIX, offers users rich and flexible functionalities, with [canary traffic shifting](https://docs.api7.ai/enterprise/api-full-lifecycle-management/api-publishing/canary-traffic-shifting) playing a pivotal role in scenarios like seamless service upgrades and validation of new features.

## Overview of Features

### Creating Upstream Based on Upstream Services

Upon a service being deployed to the gateway groups of API7 Enterprise, administrators can effortlessly derive a new upstream for canary traffic shifting from services of the existing baseline upstream. The upstream can inherit all configurations from the baseline upstream or selectively inherit the tested historical configurations. This design significantly simplifies the configuration process, reducing repetitive work and potential configuration errors. Additionally, the canary traffic shifting upstream supports configuring new nodes or integrating service discovery mechanisms. This means that without affecting existing business logic, a portion of the traffic can be redirected to new servers or environments for performance testing, feature validation, or other types of evaluation.

### Flexible Shifting Conditions and Weights

When initiating canary traffic shifting strategies, administrators can finely define the conditions and weights for shifting traffic:

**Shifting Conditions**: These determine which traffic should be directed to the canary traffic shifting upstream. Administrators can combine multiple conditions using logical operators (such as "AND" and "OR") to ensure that only requests meeting specific conditions enter the canary traffic shifting environment.

Properties that can be used for judgment include:

- **Header**: Specific fields in the request header, such as `User-Agent`.
- **Cookie**: Cookie information sent by the client, commonly used for user identification or session state recognition.
- **Query Parameters**: Query string parameters in the URL.
- **Variables**: System or custom variables, such as geographic location or user roles.

  Matching logic includes equal to (==), not equal to (~=), regular matching (~~), among other methods, ensuring the flexibility and precision of condition judgment.

  For example, administrators can set the following shifting condition: "When the `User-Agent` field in the request header contains 'iPhone' and the `user_group` field in the Cookie equals 'test_group', redirect the request to the traffic shifting upstream."

**Shifting Weights**: These determine what percentage of traffic should be directed to the canary traffic shifting upstream. It's a percentage value that allows administrators to gradually increase or decrease the traffic entering the canary traffic-shifting environment.

Initially, the weight may be set to a low value (e.g., 10%) to ensure that new features are tested within a small range. As testing progresses and results are collected, the weight can gradually increase until eventually, 100% of the traffic switches to the new version.

By flexibly adjusting shifting conditions and weights, administrators can effectively control the scope and impact of canary traffic shifting testing, ensuring the smooth rollout of new features and system stability.

![Traffic Shifting of API7 Enterprise](https://static.apiseven.com/uploads/2024/01/29/Mm2x59Vk_canary-release.png)

## Practical Application Scenarios

1. **New Feature Validation**: When a development team completes a new feature and prepares to release it, canary traffic shifting can be used to initially roll out the new feature to a small subset of users or specific user groups. This allows for collecting user feedback in a real environment while ensuring that if any issues arise, the scope of impact is manageable.

2. **Performance Testing**: During significant events or high-concurrency scenarios, administrators can use canary traffic shifting to redirect a portion of the traffic to newly configured, higher-capacity nodes to test the system's performance limits and stability.

3. **Gradual Migration**: During service upgrades or migrations, canary traffic shifting can help administrators gradually transition traffic from the old environment to the new environment, ensuring a smooth migration process.

## Best Practices

1. **Reasonably Setting Shifting Conditions**: Choosing appropriate shifting conditions based on actual needs is crucial. For example, for tests targeting specific user groups, precise shifting can be achieved through user identifiers in Cookies or Headers.

2. **Dynamic Adjustment of Weights**: During testing, dynamically adjusting shifting weights based on test results and user feedback can expedite the collection of valid data or narrow down the scope of issues.

3. **Monitoring and Alerting**: Strengthening monitoring of relevant metrics during traffic shifting and setting appropriate alert mechanisms enables timely detection and resolution of issues.

## Conclusion

Through the canary traffic shifting of [API7 Enterprise](https://docs.api7.ai/enterprise/introduction), enterprises can not only ensure service stability but also flexibly conduct new feature testing, performance optimization, and other operations, providing robust support for rapid business growth.