Concepts

When developing native applications using Microsoft Azure Cosmos DB, one important decision to make is choosing the connectivity mode between gateway and direct. Both modes have their advantages and considerations, and selecting the appropriate mode depends on your specific application requirements and use cases.

1. Gateway Mode

Gateway mode is the default connectivity mode and is recommended for most scenarios, including web and mobile applications. In this mode, the Azure Cosmos DB SDK communicates with the Azure Cosmos DB service via a gateway managed by Microsoft.

When using gateway mode, the SDK communicates over HTTPS (port 443) with the gateway. The gateway handles essential functionalities like authentication, load balancing, and routing requests to the appropriate data partitions.

To configure your application to use gateway mode, you need to provide the URI and key to connect to the gateway endpoint. Here’s an example of connecting to Azure Cosmos DB in gateway mode using the .NET SDK:

using Microsoft.Azure.Cosmos;

var cosmosClient = new CosmosClient("your-endpoint-uri", "your-authentication-key");

Gateway mode provides the following benefits:

  • Firewall-friendly: Since gateway communication happens over the standard HTTPS port, it simplifies firewall configuration.
  • Encryption: All traffic between the SDK and the gateway is encrypted using SSL/TLS protocols.
  • Client SDK version independence: You can upgrade the client SDK version without any changes to your application code.

2. Direct Mode

Direct mode provides direct connectivity to the Azure Cosmos DB service without requiring interaction through the gateway. It is designed for specific scenarios where you need more control over network connectivity, request routing, or advanced features of Azure Cosmos DB.

In direct mode, the SDK communicates directly with the Azure Cosmos DB service’s internal network endpoints over TCP/IP. This mode allows for low-latency and more efficient communication compared to gateway mode.

To utilize direct mode, you need to specify the TCP endpoint in addition to the authentication key. Here’s an example of connecting to Azure Cosmos DB in direct mode using the .NET SDK:

using Microsoft.Azure.Cosmos;

var connectionPolicy = new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
};

var cosmosClient = new CosmosClient("your-endpoint-uri", "your-authentication-key", connectionPolicy);

Direct mode offers the following advantages:

  • Low-latency: Direct mode provides a more direct and faster connection to the Azure Cosmos DB service.
  • Control over networking: With direct mode, you have control over network-level features like DNS resolution and proxy configuration.
  • Access to advanced features: Direct mode enables features like TCP-only transport and custom endpoint routing.

While direct mode offers certain benefits, it also introduces additional considerations:

  • Network configuration: Using direct mode requires careful network configuration, including allowing outbound access on specific ports and enabling connectivity to the necessary Azure Cosmos DB internal endpoints.
  • SDK compatibility: Direct mode requires SDK-specific support and version compatibility. Ensure that your chosen SDK supports direct mode and use the appropriate version.

In summary, when choosing a connectivity mode for your native applications using Azure Cosmos DB, consider your specific requirements in terms of performance, control, and features. Gateway mode is recommended for most scenarios, providing simplicity, security, and ease of use. Direct mode offers more control but requires additional setup and considerations. Finally, consult the official Azure Cosmos DB documentation and SDK documentation for detailed guidance on choosing and configuring the appropriate connectivity mode for your application.

Answer the Questions in Comment Section

What are the two connectivity modes available for accessing Azure Cosmos DB?

  • a) Gateway mode and Direct mode
  • b) Primary mode and Secondary mode
  • c) Local mode and Cloud mode
  • d) Offline mode and Online mode

Correct answer: a) Gateway mode and Direct mode

When should you choose Gateway mode for accessing Azure Cosmos DB?

  • a) When you want to connect to Cosmos DB from a client application running on-premises or in a virtual network.
  • b) When you want to connect to Cosmos DB from a client application running in a different Azure region.
  • c) When you want to connect to Cosmos DB from a client application running on a mobile device.
  • d) When you want to connect to Cosmos DB from a client application using a secure VPN connection.

Correct answer: a) When you want to connect to Cosmos DB from a client application running on-premises or in a virtual network.

True or False: In Gateway mode, client requests are routed through the Azure Cosmos DB gateway proxies before reaching the Cosmos DB service.

  • a) True
  • b) False

Correct answer: a) True

When should you choose Direct mode for accessing Azure Cosmos DB?

  • a) When you want to connect to Cosmos DB from a client application running on-premises or in a virtual network.
  • b) When you want to connect to Cosmos DB from a client application running in a different Azure region.
  • c) When you want to connect to Cosmos DB from a client application running on a mobile device.
  • d) When you want to connect to Cosmos DB from a client application using a secure VPN connection.

Correct answer: b) When you want to connect to Cosmos DB from a client application running in a different Azure region.

True or False: In Direct mode, client requests bypass the Azure Cosmos DB gateway proxies and connect directly to the Cosmos DB service.

  • a) True
  • b) False

Correct answer: a) True

What is the primary benefit of using Gateway mode for accessing Azure Cosmos DB?

  • a) Lower latency and higher throughput for client requests.
  • b) Ability to secure client connections using VPN.
  • c) Support for client applications running in different Azure regions.
  • d) Enhanced reliability and fault tolerance for client requests.

Correct answer: d) Enhanced reliability and fault tolerance for client requests.

What is the primary benefit of using Direct mode for accessing Azure Cosmos DB?

  • a) Lower latency and higher throughput for client requests.
  • b) Ability to secure client connections using VPN.
  • c) Support for client applications running in different Azure regions.
  • d) Enhanced reliability and fault tolerance for client requests.

Correct answer: a) Lower latency and higher throughput for client requests.

True or False: With Gateway mode, you can take advantage of Azure service endpoints to secure client connections.

  • a) True
  • b) False

Correct answer: a) True

True or False: Gateway mode is the recommended connectivity mode for most client applications accessing Azure Cosmos DB.

  • a) True
  • b) False

Correct answer: a) True

True or False: Direct mode is the recommended connectivity mode for client applications running in different Azure regions.

  • a) True
  • b) False

Correct answer: a) True

0 0 votes
Article Rating
Subscribe
Notify of
guest
24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rudra Uchil
1 year ago

Great post! I was really confused about whether to use gateway or direct mode for my Cosmos DB instance.

Viljami Keto
6 months ago

Thank you for this article, very informative.

Xavier Patel
1 year ago

Could someone explain the performance differences between gateway and direct mode?

Kahaan Rajesh
9 months ago

Does the choice of connectivity mode impact the security of data?

Lisa Olden
1 year ago

Very clear explanation, this will definitely help in my DP-420 exam preparation.

Barbara Fitzgerald
1 year ago

I prefer using direct mode for production environments due to its performance benefits.

Rasmus Madsen
1 year ago

Is there a significant cost difference between using gateway vs direct mode?

هستی یاسمی
7 months ago

I’m finding it difficult to configure direct mode. Any tips?

24
0
Would love your thoughts, please comment.x
()
x