Skip to content

[pd-router] Add Configurable Retry Logic for reduce backend pressure#8744

Merged
slin1237 merged 2 commits intomainfrom
slin/http-client-retries
Aug 5, 2025
Merged

[pd-router] Add Configurable Retry Logic for reduce backend pressure#8744
slin1237 merged 2 commits intomainfrom
slin/http-client-retries

Conversation

@slin1237
Copy link
Copy Markdown
Collaborator

@slin1237 slin1237 commented Aug 4, 2025

Summary

This PR adds configurable retry logic to the PD router to handle backend KVTransferError failures under load. The solution ensures error handling without sacrificing the performance advantages of the Rust implementation. Additionally, a performance benchmark tool was developed during debugging and is included as it provides valuable testing capabilities.

Problem Investigation

Under load (1000+ concurrent requests), both the Rust PD router and Python mini load balancer experienced KVTransferError failures from backend servers:

[2025-08-02 16:45:38 DP0 TP3 EP3] Prefill bootstrap failed for request 
rank=3 req.rid='68a8a5936c314a92aadcb744cc32a3a9' req.bootstrap_room=5464336903252480568

Initial Hypothesis: Connection Pool Mismatch

We initially suspected a connection pooling issue:

Configuration Comparison:

  • Rust PD Router:
    • pool_idle_timeout: 50 seconds
    • pool_max_idle_per_host: 100 connections
    • Persistent connection pooling
  • Python Mini LB:
    • Creates new ClientSession per request
    • No connection pooling
  • Backend (uvicorn):
    • timeout_keep_alive: 5 seconds ⚠️

Failed Attempts to Match Python Behavior

We tried multiple approaches to make Rust behave like Python:

  1. ❌ Creating a new client for every request
  2. ❌ Adding static delays to slow down Rust
  3. ❌ Matching connection timeout settings
  4. ❌ Disabling connection pooling entirely

None of these approaches worked because Rust's inherent performance characteristics still made it faster than Python, even with artificial constraints.

Root Cause Discovery

The real issue wasn't connection pooling. The fundamental problem: both implementations hit capacity limits under high concurrent load

Solution: Configurable Retry Logic

Instead of trying to make Rust slower or ignore errors like mini-lb, we implemented a proper solution: configurable retry logic with exponential backoff.

Implementation Details

pub struct RetryConfig {
    pub max_retries: u32,           // Default: 3
    pub initial_backoff_ms: u64,    // Default: 100ms
    pub max_backoff_ms: u64,        // Default: 10000ms
    pub backoff_multiplier: f32,    // Default: 2.0
}

Key features:

  • Only retries server errors (5xx) and gateway errors (502, 504)
  • Exponential backoff with jitter prevents thundering herd
  • Configurable without recompilation
  • Maintains Rust's performance advantages while handling transient failures

Code Improvements

While implementing retry logic, we also refactored pd_router.rs:

  • Extracted helper methods to reduce code duplication
  • Improved error handling consistency
  • Better separation of concerns

Benchmark Tool

During debugging, we developed a Rust benchmark tool that proved invaluable for testing. We're including it in this PR as it provides ongoing value:

Usage:

# Test with extreme concurrency
./target/release/router-benchmark basic -r 10000 -c 10000

# Realistic workload with streaming
./target/release/router-benchmark realistic -r 5000 -c 5000 --stream

Results

With retry logic:

  • 100% success rate with 10,000 concurrent requests
  • Successfully handles transient failures
  • Maintains Rust's performance advantages
  • No need to artificially slow down the router

Files Changed

  • src/routers/pd_router.rs: Retry logic and refactoring
  • src/routers/router.rs: Retry configuration support
  • src/config/types.rs: RetryConfig struct
  • src/routers/factory.rs: Pass retry configuration
  • benches/router_benchmark.rs: New benchmark tool
  • benches/README.md: Benchmark documentation

Key Insights

  1. Performance isn't the problem - Rust being faster than Python is a feature, not a bug
  2. Proper error handling is the solution - Retry logic handles transient failures gracefully
  3. Don't compromise correctness - Instead of ignoring errors like Python, handle them properly
  4. Benchmarking is crucial - The new tool enables ongoing performance validation

Breaking Changes

None. Default retry configuration maintains existing behavior.

Accuracy Test

Benchmark & Profiling

Checklist

@slin1237 slin1237 requested a review from ByronHsu as a code owner August 4, 2025 02:50
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @slin1237, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness of the PD router by introducing a configurable retry mechanism to gracefully handle transient backend failures under heavy load. Alongside this critical reliability improvement, I've also integrated a powerful new benchmarking tool to facilitate ongoing performance validation and ensure the router maintains its high-performance characteristics. The changes also include general code refactoring to improve maintainability and error handling consistency.

Highlights

  • Configurable Retry Logic: I've implemented configurable retry logic with exponential backoff and jitter for the PD router. This solution specifically targets transient backend KVTransferError failures under high load, ensuring robust error handling without compromising Rust's performance advantages. It retries server errors (5xx, 502, 504) and is configurable via RetryConfig.
  • New Benchmark Tool: A new Rust-based benchmark tool (router-benchmark) has been added. This tool, developed during debugging, is capable of simulating various load scenarios (basic, realistic, progressive, sustained) and generating detailed performance reports, proving invaluable for testing and validation.
  • Code Refactoring and Error Handling: The pd_router.rs file has been refactored to improve code organization, extract common helper methods for error handling (e.g., server selection, bootstrap injection, serialization), and enhance consistency in error responses. This makes the codebase cleaner and more maintainable.
  • Configuration Updates: The RouterConfig now includes a retry field to hold the new RetryConfig parameters. Additionally, the default request_timeout_secs in RouterConfig has been increased from 600 seconds to 3600 seconds (1 hour) to better align with the Python mini load balancer's behavior.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces configurable retry logic to the PD router, refactors pd_router.rs for improved clarity, and adds a new benchmark tool. I've suggested improvements to the retry logic, benchmark tool accuracy, and consistency across router implementations.

Comment thread sgl-router/src/routers/pd_router.rs
Comment thread sgl-router/benches/router_benchmark.rs Outdated
Comment thread sgl-router/benches/router_benchmark.rs Outdated
Comment thread sgl-router/benches/router_benchmark.rs Outdated
Comment thread sgl-router/benches/router_benchmark.rs Outdated
Comment thread sgl-router/src/config/types.rs
Comment thread sgl-router/src/routers/pd_router.rs
@slin1237 slin1237 force-pushed the slin/http-client-retries branch from 0ab01ac to 5be904b Compare August 4, 2025 03:45
@slin1237 slin1237 force-pushed the slin/http-client-retries branch from 5d56337 to 0ea246b Compare August 4, 2025 21:52
@slin1237 slin1237 merged commit 354ac43 into main Aug 5, 2025
24 checks passed
@slin1237 slin1237 deleted the slin/http-client-retries branch August 5, 2025 03:42
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

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