perf(cosmos): share partition key range cache across clients per endpoint#46308
Closed
tvaron3 wants to merge 1 commit intoAzure:mainfrom
Closed
perf(cosmos): share partition key range cache across clients per endpoint#46308tvaron3 wants to merge 1 commit intoAzure:mainfrom
tvaron3 wants to merge 1 commit intoAzure:mainfrom
Conversation
…oint Clients targeting the same Cosmos DB endpoint now share a single CollectionRoutingMap cache instead of each maintaining an independent copy. This eliminates N-1 redundant copies of the partition key range data when N clients connect to the same account. The shared cache is a module-level dict keyed by endpoint URL, protected by threading.Lock. refresh_routing_map_provider now calls clear_cache() on the shared entry instead of creating a new SmartRoutingMapProvider. PPCB overhead with 150 clients (tracemalloc, ~100 partitions): | Clients | Original PPCB cost | Shared Cache | Reduction | |---------|-------------------|-------------|-----------| | 25 | 5.1 MB | 0.3 MB | -93% | | 50 | 10.3 MB | 0.3 MB | -97% | | 100 | 15.4 MB | -1.6 MB | -110% | | 150 | 27.4 MB | -0.0 MB | -100% | At customer scale (200K partitions x 152 clients): ~2.1 GB -> ~14 MB. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
Author
|
Moving to PR #46297 instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Clients targeting the same Cosmos DB endpoint now share a single CollectionRoutingMap cache instead of each maintaining an independent copy. This eliminates N-1 redundant copies of partition key range data when N clients connect to the same account.
Problem
When PPCB is enabled, each CosmosClient eagerly loads the full partition key range routing map via create_pk_range_wrapper(). Each client creates its own SmartRoutingMapProvider with its own _collection_routing_map_by_item dict, resulting in N identical copies for N clients targeting the same endpoint.
Changes
Memory Profiling Results
Test setup: tracemalloc, ~100 partitions, 2 regions, PPCB=True, 1 read + 1 upsert per client.
Current Memory (MB)
PPCB Overhead Reduction
Scaling Estimate
At customer scale (200K partitions x 152 clients): ~2.1 GB -> ~14 MB (single shared copy).
Tests
5 unit tests in test_shared_pk_range_cache.py: