Skip to content

feat: add nearest DC detection with TCP race#763

Open
r142f wants to merge 19 commits intoydb-platform:mainfrom
r142f:local-dc-detection
Open

feat: add nearest DC detection with TCP race#763
r142f wants to merge 19 commits intoydb-platform:mainfrom
r142f:local-dc-detection

Conversation

@r142f
Copy link
Copy Markdown

@r142f r142f commented Feb 4, 2026

Add automatic nearest datacenter detection for connection pooling

Pull request type

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

The SDK uses round-robin load balancing across all discovered endpoints without considering network latency, which can lead to suboptimal performance in multi-datacenter deployments.

Fixes: #446
Fixes: #592

What is the new behavior?

Adds opt-in nearest datacenter detection (enabled via DriverConfig(detect_local_dc=True)) that uses TCP connection racing to identify and prioritize the fastest-responding datacenter, reducing request latency in multi-DC deployments.

Other information

  • Fully backward compatible (disabled by default)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds an opt-in “nearest datacenter” selection mechanism for endpoint discovery, using a TCP connection race to prefer endpoints in the lowest-latency location when DriverConfig(detect_local_dc=True) is enabled.

Changes:

  • Add sync + async nearest_dc modules implementing TCP-race-based location detection.
  • Wire DC detection into sync and async discovery pools to mark endpoints in the detected location as preferred.
  • Add detect_local_dc flag to DriverConfig and introduce unit tests for both sync and async implementations.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
ydb/pool.py Uses detected local DC (when enabled) to set endpoint preference during discovery.
ydb/nearest_dc.py Implements synchronous TCP race DC detection helpers.
ydb/driver.py Adds detect_local_dc configuration flag to DriverConfig.
ydb/aio/pool.py Async discovery now prefers endpoints in the detected local DC.
ydb/aio/nearest_dc.py Implements asynchronous TCP race DC detection helpers.
tests/test_nearest_dc.py Adds unit tests for sync nearest DC detection behavior.
tests/aio/test_nearest_dc.py Adds unit tests for async nearest DC detection behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@r142f r142f force-pushed the local-dc-detection branch from 75c84c6 to 33826e7 Compare March 26, 2026 03:44
@vgvoleg
Copy link
Copy Markdown
Collaborator

vgvoleg commented Mar 26, 2026

Hi! Awesome PR! one little suggestion: can you move nearest_dc contents to _utilities modules? I don't think we need to add another file for this

@r142f r142f marked this pull request as draft March 29, 2026 16:16

if ssl_filtered_endpoints:
try:
detected_location = _utilities.detect_local_dc(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we don't have any cache here - the number of endpoints could be huge

@r142f r142f marked this pull request as ready for review March 30, 2026 10:40
@vgvoleg vgvoleg added the SLO label Mar 31, 2026
@github-actions
Copy link
Copy Markdown

🌋 SLO Test Results

Status: 🟢 2 workloads tested • All passed

Workload Metrics Regressions Improvements Links
🟢 ydb-python-sync-query 8 0 0 ReportCheck
🟢 ydb-python-sync-table 8 0 0 ReportCheck

Generated by ydb-slo-action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Automatic detection of the nearest DC/availability zone by TCP pings Python Detect and select nearest DB by tcp-pings

3 participants