Skip to content

feat: add router_rebuild_min_interval to reduce CPU spikes from frequent route changes#13072

Closed
membphis wants to merge 1 commit intoapache:masterfrom
membphis:feat-router-min-interval-v2
Closed

feat: add router_rebuild_min_interval to reduce CPU spikes from frequent route changes#13072
membphis wants to merge 1 commit intoapache:masterfrom
membphis:feat-router-min-interval-v2

Conversation

@membphis
Copy link
Member

@membphis membphis commented Mar 5, 2026

Description

When routes change frequently (e.g., 20-50 changes/sec with 5000+ total routes), the data plane rebuilds the radixtree router on every single change, causing significant CPU spikes (up to 80%) and temporary TPS drops.

This PR adds a configurable router_rebuild_min_interval option that sets a minimum time interval (in seconds) between HTTP router rebuilds. When the interval has not elapsed since the last rebuild, subsequent route changes are acknowledged but the router rebuild is deferred, dramatically reducing CPU usage during bulk route updates.

Configuration

apisix:
  router:
    http: radixtree_host_uri
    ssl: radixtree_sni
    router_rebuild_min_interval: 0  # default: 0 (rebuild immediately, current behavior)

Expected Impact

Scenario (30s duration) min_interval: 0 min_interval: 1 min_interval: 5
20 changes/sec 600 rebuilds ~31 rebuilds ~7 rebuilds
50 changes/sec 1500 rebuilds ~31 rebuilds ~7 rebuilds
1 change/3sec 10 rebuilds 10 rebuilds 10 rebuilds

Changes

  • conf/config.yaml.example: Added router_rebuild_min_interval config option
  • apisix/cli/config.lua: Default value 0
  • apisix/router.lua: Reads config and passes to HTTP router modules (+3 lines)
  • apisix/http/router/radixtree_uri.lua: Min interval check before rebuild
  • apisix/http/router/radixtree_host_uri.lua: Min interval check before rebuild
  • apisix/http/router/radixtree_uri_with_parameter.lua: Min interval check before rebuild
  • t/router/router-rebuild-min-interval.t: Test cases covering default behavior, skip behavior, and deferred rebuild

Backward Compatibility

Default value is 0, preserving the existing behavior of rebuilding immediately on every route change. No existing configurations are affected.

…ent route changes

When routes change frequently (e.g., 20-50 changes/sec), the HTTP router
is rebuilt on every change which causes CPU spikes due to full radixtree
recompilation with all routes.

This adds a configurable minimum interval between HTTP router rebuilds.
When set to a positive value (e.g., 1 or 5 seconds), rebuilds are
throttled so the router is not recompiled more frequently than the
configured interval, significantly reducing CPU usage during bulk
route updates.

Default value is 0 (rebuild immediately on every change), preserving
backward compatibility.
@membphis membphis closed this Mar 5, 2026
@membphis membphis deleted the feat-router-min-interval-v2 branch March 5, 2026 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant