APISIX Traffic Splitting Guide

Introduction

Traffic splitting in APISIX enables gradual rollouts and A/B testing of your services. This guide covers implementation strategies and best practices.

Prerequisites:
  • APISIX installed and configured
  • Basic understanding of APISIX routes and upstream concepts
  • Two versions of your service running

Basic Traffic Splitting

Weight-based Routing

{
    "uri": "/api/v1/*",
    "upstream": {
        "nodes": {
            "service-v1:8080": 90,
            "service-v2:8080": 10
        },
        "type": "roundrobin"
    }
}

Implementing Canary Releases

Canary Deployment Configuration

Canary releases allow you to test new versions with a subset of users before full deployment.

{
    "uri": "/api/v1/*",
    "plugins": {
        "traffic-split": {
            "rules": [
                {
                    "match": [
                        {
                            "vars": [
                                ["http_user", "==", "beta-user"]
                            ]
                        }
                    ],
                    "weighted_upstreams": [
                        {
                            "upstream": {
                                "name": "new-version",
                                "type": "roundrobin",
                                "nodes": {
                                    "service-v2:8080": 1
                                }
                            },
                            "weight": 100
                        }
                    ]
                }
            ]
        }
    }
}
Pro Tip: Start with a small percentage (1-5%) of traffic and gradually increase based on monitoring results.

A/B Testing Strategies

Configuring A/B Tests

Test different versions of your API to measure performance and user engagement.

{
    "uri": "/api/v1/*",
    "plugins": {
        "traffic-split": {
            "rules": [
                {
                    "match": [
                        {
                            "vars": [
                                ["arg_version", "==", "B"]
                            ]
                        }
                    ],
                    "weighted_upstreams": [
                        {
                            "upstream": {
                                "name": "version-b",
                                "type": "roundrobin",
                                "nodes": {
                                    "service-b:8080": 1
                                }
                            },
                            "weight": 100
                        }
                    ]
                }
            ]
        }
    }
}

Advanced Routing Rules

Complex Traffic Splitting

Implement sophisticated routing based on multiple conditions:

  • Header-based routing
  • Cookie-based splitting
  • Geographic distribution
  • Device type targeting
{
    "uri": "/api/v1/*",
    "plugins": {
        "traffic-split": {
            "rules": [
                {
                    "match": [
                        {
                            "vars": [
                                ["http_device", "==", "mobile"],
                                ["http_region", "==", "us-west"]
                            ]
                        }
                    ],
                    "weighted_upstreams": [
                        {
                            "upstream": {
                                "name": "mobile-optimized",
                                "type": "roundrobin",
                                "nodes": {
                                    "mobile-service:8080": 1
                                }
                            },
                            "weight": 100
                        }
                    ]
                }
            ]
        }
    }
}

Monitoring and Analytics

Tracking Split Traffic

Essential metrics to monitor:

  • Response time per version
  • Error rates comparison
  • Traffic distribution accuracy
  • User engagement metrics
  • Resource utilization
{
    "plugins": {
        "prometheus": {},
        "traffic-split": {
            "rules": [...],
            "metrics": {
                "enable": true,
                "prefix": "apisix_traffic_split"
            }
        }
    }
}

Best Practices and Guidelines

Implementation Guidelines

  • Gradual Rollout: Start with a small percentage and increase gradually
  • Monitoring: Set up comprehensive monitoring before splitting traffic
  • Fallback Strategy: Always have a rollback plan ready
  • Documentation: Maintain clear documentation of split configurations
  • Testing: Thoroughly test configurations in staging environment

Common Pitfalls

  • Insufficient monitoring setup
  • Lack of proper error handling
  • Missing fallback configurations
  • Incomplete testing scenarios
  • Poor documentation of split rules

Troubleshooting Guide

Common Issues and Solutions

Issue Possible Cause Solution
Uneven Traffic Distribution Incorrect weight configuration Verify and adjust weights in configuration
Rules Not Matching Syntax errors in match conditions Double-check rule syntax and test with curl
High Latency Network issues or service overload Monitor upstream service health and adjust capacity

Read Next

Performance Tuning

Optimize your APISIX gateway performance

Read More

Monitoring

Monitor your traffic splitting setup

Read More