[Cosmos DB] Robust usage of PreferredLocations / equivalent of ApplicationRegion
See original GitHub issue- Package Name: azure-cosmos
- Package Version: 4.2.0
- Operating System: Linux
- Python Version: 3.8
Describe the bug This is probably as much as feature request as a bug report:
For different app environments (dev,test,prod) we are using different Cosmos DB accounts. Prod and test are geo-replicated (multiple read-regions, only one write region). Dev, however, is not replicated at all.
Now we would like our app to connect automatically to the closest available region. In the .NET SDK this seems to be available with the ApplicationRegion parameter. I didn’t find this in the python SDK?! So instead we tried to use PreferredLocations. However, that crashes if you specify a region in there, which is not in the replicated regions for this account (even if another region in that list does exist).
To Reproduce
self.cosmos_client = CosmosClient(
url=DB_URL,
credential=credentials,
preferred_locations=["UK West", "UK South"]
Whereas the Cosmos DB does exist in UK south but was not replicated to UK West.
Traceback (most recent call last):
[...]
File "./app/database/database.py", line 60, in __init__
self.cosmos_client = CosmosClient(
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/cosmos_client.py", line 167, in __init__
self.client_connection = CosmosClientConnection(
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_cosmos_client_connection.py", line 208, in __init__
self._global_endpoint_manager.force_refresh(database_account)
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 89, in force_refresh
self.refresh_endpoint_list(database_account)
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 99, in refresh_endpoint_list
raise e
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 97, in refresh_endpoint_list
self._refresh_endpoint_list_private(database_account, **kwargs)
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 107, in _refresh_endpoint_list_private
self.location_cache.should_refresh_endpoints()
File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_location_cache.py", line 164, in should_refresh_endpoints
most_preferred_read_endpoint = self.available_read_endpoint_by_locations[most_preferred_location]
KeyError: 'UK West'
Expected behavior The next region in the list should be automatically tried without the entire connection failing.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (4 by maintainers)

Top Related StackOverflow Question
basically, yes 😃 For now we worked around this, but it would have felt more natural if the SDK will automatically retry if one of the regions is invalid. Ideally, even if none of the preferred_locations, is valid, it should just fall back to the main/default region. After all, it’s called “preferred” not “ever-only-one-of-these” locations 😃
ok, fair enough. thanks!