TASK
Implementation
Traffic splitting lets you roll out a new version gradually instead of switching all users at once. You can send a small percentage to the new version (canary), route specific users by request headers (A/B test), or split 50/50 (blue-green).
Implement a node that routes requests according to splitting rules:
// Weighted split: 80% to v1, 20% to v2
{ "type": "route", "msg_id": 1 }
{ traffic_split: {"v1": 80, "v2": 20} }
-> { "type": "routed", "in_reply_to": 1,
"version": "v1", "reason": "weighted_routing" }
// Header-based routing overrides weight
{ "type": "route", "msg_id": 2,
"headers": {"x-beta": "true"} }
-> { "type": "routed", "in_reply_to": 2,
"version": "v2", "reason": "header_match" }
// Update canary percentage (v1 + v2 = 100)
{ "type": "update_canary", "msg_id": 3,
"service": "api", "percentage": 50 }
-> { "type": "canary_updated", "in_reply_to": 3,
"service": "api", "v1": 50, "v2": 50 }
// Stable A/B assignment per user
{ "type": "assign_variant", "msg_id": 4, "user_id": "user-123" }
-> { "type": "variant_assigned", "in_reply_to": 4,
"user_id": "user-123", "variant": "A" }A/B variant assignment must be deterministic: the same user_id must always receive the same variant.
Sample Test Cases
Weighted traffic splitTimeout: 5000ms
Input
{
"src": "client",
"dest": "proxy",
"body": {
"type": "route",
"msg_id": 1
},
"traffic_split": {
"v1": 80,
"v2": 20
}
}Expected Output
{"type": "routed", "in_reply_to": 1, "version": "v1", "reason": "weighted_routing"}Header-based routingTimeout: 5000ms
Input
{
"src": "client",
"dest": "proxy",
"body": {
"type": "route",
"msg_id": 1,
"headers": {
"x-beta": "true"
}
}
}Expected Output
{"type": "routed", "in_reply_to": 1, "version": "v2", "reason": "header_match"}Hints
Hint 1▾
Weighted routing: pick a random number 0-99; route to v2 if < v2_percentage, else v1
Hint 2▾
Header-based routing: check request headers for a match before falling back to weighted
Hint 3▾
update_canary changes the v1/v2 split percentages; v1 + v2 must always sum to 100
Hint 4▾
A/B test assignment: hash(user_id) % 2 gives a stable deterministic variant per user
Hint 5▾
Header match takes priority over weighted split when both rules are active
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
canary deploymenttraffic splittingA/B testingblue-green deploymentweighted routing
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()