Microsoft Certified: Azure Developer Associate (AZ-204)Develop for Azure storageHard

A global e-commerce platform uses Azure Cosmos DB for its product catalog. The platform experiences high read traffic from users worldwide. To minimize latency for read operations, the company has configured Cosmos DB with multiple read regions. How can the application code ensure that read requests are consistently directed to the closest available replica?

  1. AConfigure the Cosmos DB client with `ConnectionMode.Direct` and let the SDK handle geo-spatial routing.
  2. BSet the `PreferredLocations` property in the Cosmos DB client configuration to an ordered list of desired regions.
  3. CManually specify the preferred read region in the Cosmos DB client configuration for each user's request.
  4. DUse `ConnectionMode.Gateway` and rely on Azure Traffic Manager for automatic routing to the nearest replica.
Show answer & explanation

Correct answer: B. Set the `PreferredLocations` property in the Cosmos DB client configuration to an ordered list of desired regions.

The `PreferredLocations` property in the Cosmos DB client configuration allows specifying an ordered list of regions. The SDK automatically detects the user's geographical location and attempts to connect to the closest preferred region for read operations, falling back to others if necessary.

Why the other options are wrong

  • A. ConnectionMode.Direct optimizes connection to a specific replica but doesn't handle geo-spatial routing to the *closest* replica across multiple regions automatically without PreferredLocations.
  • C. Manually specifying the region for each request is cumbersome and doesn't dynamically adapt to the closest replica for every user.
  • D. ConnectionMode.Gateway uses a single endpoint and relies on the gateway for routing, which might not be as optimal for read latency as `PreferredLocations` with direct mode. Traffic Manager is typically for routing traffic to application endpoints, not directly to Cosmos DB replicas.

Cosmos DB Global Distribution (Reads)

Cosmos DB supports global distribution, allowing data to be replicated across multiple Azure regions. Read requests can be routed to the closest replica to minimize latency.

  • Multi-master or single-master replication.
  • Read requests can be served from any replica.
  • SDK's `PreferredLocations` for closest replica routing.
  • Enhances global application performance.

Memory trick: Preferred Locations is like telling Cosmos DB, 'Here are my favorite spots, pick the closest one for my reads!'

More Develop for Azure storage questions