Before making the decision between Gateway and Direct connectivity, it is crucial to understand these terms in the Azure Cosmos DB context.

  • Gateway Mode: The Gateway mode significantly abstracts the service topology for applications, creating a clear separation between user network and Azure Cosmos DB service. The Gateway server is responsible for routing all the traffic, providing a single HTTP/HTTPS endpoint, which takes advantage of automatic indexing.
  • Direct Mode: The Direct Mode, on the other hand, operates in two different ways— TCP and HTTPS. It delivers low-latency and high-throughput capabilities, by directly connecting to the Azure Cosmos DB service. TCP provides higher performance, while HTTPS offers software compatibility.

Table of Contents

Choosing Between Gateway and Direct Modes

The decision to use Gateway or Direct mode for your Azure Cosmos DB connectivity largely depends on your application requirements and your optimization of performance, cost, or network bandwidth. Here are some key considerations:

  • Performance: The Direct mode offers better performance for read-heavy workloads with its direct TCP/HTTPS connections. The Gateway mode, while offering lower performance than Direct mode, delivers decent performance suitable for most general-purpose applications.
  • Network Bandwidth: The Direct mode, with its direct connectivity, consumes more network bandwidth than the Gateway mode.
  • Software Compatibility: If the software’s framework does not have native support for .NET SDK v2, or if it uses an older version, the Gateway mode is a better choice due to its compatibility with HTTPS.

Example: How to Change Connectivity Mode in SDK

To facilitate an informed choice based on the above considerations, here is an example demonstrating how to change from Gateway to Direct mode, and vice versa, through the SDK:

// Creating new CosmosClient with Gateway Mode
CosmosClient client = new CosmosClient(
"",
new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway });

// Creating new CosmosClient with Direct Mode
CosmosClient client = new CosmosClient(
"",
new CosmosClientOptions() { ConnectionMode = ConnectionMode.Direct });

In conclusion, choosing the right connectivity mode for your Azure Cosmos DB is crucial. Whether you choose Gateway or Direct connectivity mode depends on your unique application’s requirements for performance, cost, or network bandwidth. Understanding these modes and making the right selection can significantly improve your application’s performance and minimize latency.

Practice Test

True or False: Gateway mode provides a higher performance and is ideal for applications with high request rates.

  • True
  • False

Answer: False

Explanation: Direct mode provides higher performance and is ideal to use for applications with high request rates.

The Gateway mode is for scenarios where network connectivity to Azure is not possible. True or False?

  • True
  • False

Answer: True

Explanation: Gateway mode is useful in scenarios where network connectivity to Azure is not feasible.

Which mode in Azure Cosmos DB does not maintain a persistent connection to Azure Cosmos DB and is ideal for high-density low-cost apps?

  • A) Direct Mode
  • B) Gateway Mode

Answer: B) Gateway Mode

Explanation: Gateway mode does not maintain a persistent connection to Azure Cosmos DB and is ideal for high-density low-cost apps with lower rate of requests.

The default connectivity mode in the Azure Cosmos DB .NET SDK is ____________.

  • A) Direct Mode
  • B) Gateway Mode

Answer: A) Direct Mode

Explanation: The default connectivity mode in the Azure Cosmos DB .NET SDK is Direct Mode.

In Azure, the Direct mode uses more resources available on the client machine compared to the Gateway mode. True or False?

  • True
  • False

Answer: True

Explanation: This is because the Direct mode maintains a persistent connection with every single partition in the Cosmos DB.

True or False: The connectivity mode cannot be changed once chosen.

  • True
  • False

Answer: False

Explanation: The connectivity mode is configurable and can be changed based on the application’s needs.

Which mode provides the benefits of both Gateway and Direct modes in Azure Cosmos DB?

  • A) Hybrid Mode
  • B) Combined Mode
  • C) There is no such mode

Answer: C) There is no such mode

Explanation: Currently, Azure Cosmos DB only supports either Gateway mode or Direct mode.

Multiple Select: In which of the following scenarios would the Gateway mode be ideal?

  • A) Applications with a high request rate
  • B) Applications with lower request density
  • C) When network connectivity to Azure is not feasible
  • D) When client resources are low

Answer: B) Applications with lower request density, C) When network connectivity to Azure is not feasible, D) When client resources are low

Explanation: Gateway mode is ideal for all these situations as it doesn’t maintain a persistent connection to Azure Cosmos DB, uses lesser client resources, and is feasible even when network connectivity to Azure is not possible.

In Direct mode, each client keeps a persistent connection with each partition. True or False?

  • True
  • False

Answer: True

Explanation: This is true, and as a result, Direct mode uses more resources on the client machine, but also provides a higher performance.

The Gateway mode in Azure Cosmos DB uses a stateless protocol. True or False?

  • True
  • False

Answer: True

Explanation: Gateway mode uses HTTP/HTTPS, which are stateless protocols, meaning they don’t maintain a persistent connection with Azure Cosmos DB.

Which mode is ideal for applications with lower request density?

  • A) Direct Mode
  • B) Gateway Mode

Answer: B) Gateway Mode

Explanation: Gateway mode is ideal for scenarios with lower request density as it does not maintain a persistent connection to Azure Cosmos DB.

The Gateway mode in Azure Cosmos DB uses which protocol(s)?

  • A) HTTP or HTTPS
  • B) TCP
  • C) UDP

Answer: A) HTTP or HTTPS

Explanation: Gateway mode uses a stateless protocol, HTTP/HTTPS, which doesn’t maintain a persistent connection with Azure Cosmos DB.

The Direct Mode provides lower latency and higher throughput compared to Gateway Mode. True or False?

  • True
  • False

Answer: True

Explanation: Due to the persistent connection with every single partition in the Cosmos DB, the Direct Mode provides lower latency and higher throughput.

The Direct mode is ideal for scenarios where network connectivity to Azure is not feasible. True or False?

  • True
  • False

Answer: False

Explanation: Gateway mode is ideal for scenarios where network connectivity to Azure is not feasible.

With Azure Cosmos DB .NET SDK version 0 and later, which connectivity mode is used by default?

  • A) Direct Mode
  • B) Gateway Mode

Answer: A) Direct Mode

Explanation: With the Azure Cosmos DB .NET SDK version 0 and later, Direct mode is used by default as it has lower latency and higher throughput.

Interview Questions

What is the primary difference between gateway mode and direct mode in Cosmos DB?

Gateway mode uses the Azure Cosmos DB Gateway to route all data operations, while Direct mode establishes a direct connection to the partition where the data resides.

Can you switch between gateway mode and direct mode after creating a Cosmos DB account?

Yes, you can switch between gateway and direct mode at any point even after the Cosmos DB account is created.

Where is the setting to switch between gateway mode and direct mode found?

The setting to switch between gateway mode and direct mode is found in the Connection policy tab of Azure Cosmos DB’s SQL API SDK.

Which connectivity mode provides lower latency and higher throughput: gateway or direct?

Direct mode typically provides lower latency and higher throughput as it doesn’t route requests through the Azure Cosmos DB gateway.

What is a major benefit of using gateway mode in Azure Cosmos DB?

A major advantage of using gateway mode is that it reduces client-side configuration. Clients do not need to run Azure Cosmos DB ports or manage the address cache.

Which mode is recommended to use when firewall and network configurations hamper applications from reaching out to Azure Cosmos DB’s port 443 and 10255?

When firewall and network configurations prevent reaching out to Azure Cosmos DB’s port 443 and 10255, the gateway mode is recommended.

What advantage does direct mode offer when considering partition key range caching?

Direct mode has the advantage of automatic partition key range caching. It updates the cache automatically when partitions split or merge, without needing an application restart.

Which mode simplifies client-side setup when connecting to Cosmos DB: gateway or direct?

Gateway mode simplifies client-side setup when connecting to Cosmos DB, as it does not require any specific Azure Cosmos DB firewall and network configuration.

What is the fallback to gateway mode when using Azure Cosmos DB’s SQL API .NET SDK?

While using Azure Cosmos DB’s SQL API .NET SDK, if the application cannot connect in direct mode because it’s behind a firewall, it falls back to gateway mode.

Which connectivity mode is better for multiplayer gaming applications in terms of latency?

Direct mode is better for multiplayer gaming applications as it offers lower latency, thus providing faster data operations and better gameplay experience.

What does connectivity mode mean in Azure Cosmos DB?

Connectivity mode in Azure Cosmos DB refers to the method used to route data operations from the client to the Azure Cosmos DB backend. The two modes are gateway and direct mode.

When would you use direct mode over gateway mode?

Direct mode is beneficial when performance is a high priority. It gives lower latency due to direct connections with Cosmos DB’s nodes.

Which connectivity mode provides automatic and transparent failover handling?

Both connection modes provide automatic and transparent failover handling. However, the direct mode performs all the required re-computations related to failovers in the background without impacting client operations.

Does direct mode support all Cosmos DB APIs?

As of now, direct mode supports SQL (Core) API, MongoDB API, and Cassandra API, but doesn’t support Gremlin (Graph) API and Table API.

Is throttling handled differently in gateway mode compared to direct mode?

No, throttling is handled the same way in both modes. It reflects the offered throughput assigned to the containers in the Cosmos DB account.

Leave a Reply

Your email address will not be published. Required fields are marked *