Implementing a connectivity mode in Azure Cosmos DB is a key factor in the optimal design and implementation of native applications. Azure Cosmos DB supports two connectivity modes – Gateway mode and Direct mode that greatly influence the performance of your applications and how it interacts with the Azure Cosmos DB service.

Table of Contents

Gateway mode

In Gateway mode, client’s requests are first processed through the Azure Cosmos DB Gateway, a stateless intermediary that translates and forwards requests to the appropriate partition. Gateway architecture is easy to set and requires no additional client-side configuration. It uses TCP protocol for interconnections but TCP/IP restriction is applied only at the gateway level, simplifying the management of firewall rules.

Direct mode

Direct mode, on the other hand, provides a faster and more efficient connection by allowing the clients to directly communicate with the data partitions. Each client maintains a map of the partitions and routes requests directly to the nodes. This mode supports both TCP and HTTPS protocols for connections.

Whether you choose Gateway mode or Direct mode, it must be remembered that both have their own pros and cons and their usage depends greatly on the requirement of your application.

Gateway mode vs Direct mode

For instance, Gateway mode proofs to be a viable choice when starting up a project where simplicity and ease of setup is a priority. It’s also quite suitable for applications with infrequent data operations as it facilitates better management of firewall rules.

Direct mode, however, should be implemented when the demand for performance and efficiency is high. It minimizes the latency and improves read and write throughput, making it a preferred choice for applications dealing with large amounts of data or heavier workloads.

.NET Applications and connectivity mode

.NET applications make use of the Microsoft.Azure.Cosmos client SDK to connect to Azure Cosmos DB and the mode of connection is determined through the ConnectionMode enumeration under CosmosClientOptions. By default, the connection mode is set to Direct.

CosmosClient cosmosClient = new CosmosClient("connection-string", new CosmosClientOptions { ConnectionMode = ConnectionMode.Direct });

In the above code, Direct mode is being chosen for the application’s connectivity mode. If you want to switch to Gateway mode, simply replace Direct with Gateway.

CosmosClient cosmosClient = new CosmosClient("connection-string", new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway });

Conclusion

To conclude, the implementation of a connectivity mode largely depends on your application’s requirements, the complexity of your setup, and the type of workload. While Gateway can offer simplicity and easy set-up, Direct mode is recommended for better efficiency and performance especially when dealing with larger datasets or heavy workloads. Optimization of different connection modes according to your needs, therefore, is key to maximising efficiency and performance in Azure Cosmos DB.

Practice Test

True/False: Microsoft Azure Cosmos DB provides both Direct and Gateway connectivity modes.

  • True
  • False

Answer: True

Explanation: Azure Cosmos DB supports two types of connectivity modes: Direct (TCP protocols with protocol negotiation) and Gateway (HTTPS).

Which of the following protocols are supported by Direct connectivity mode in Azure Cosmos DB?

  • TCP
  • HTTP
  • UDP
  • ARP

Answer: TCP

Explanation: Direct mode in Azure Cosmos DB supports TCP protocol. This mode provides lower latency and higher throughput than Gateway mode.

True/False: In direct connectivity mode with the TCP protocol, the Azure Cosmos DB SDK maintains multiple physical connections.

  • True
  • False

Answer: True

Explanation: Direct mode uses the TCP protocol where the Azure Cosmos DB SDK maintains multiple physical connections to each Azure Cosmos DB replica for more efficient data transfer.

True/False: Changing the connectivity mode in Azure Cosmos DB requires application downtime.

  • True
  • False

Answer: False

Explanation: The connectivity mode can be changed at any time without application downtime. It’s just switching the connection policy on the client.

When implementing a connectivity mode, which of the following factors are important to consider?

  • Security of data transfer
  • Data transfer speed
  • Application downtime during switch
  • All of the above

Answer: All of the above

Explanation: Security of data transfer, data transfer speed, and no application downtime during compliance switch are key factors when implementing a connectivity mode in Cosmos DB.

Which connectivity mode uses the HTTPS protocol in Azure Cosmos DB?

  • Direct Mode
  • Gateway Mode
  • Connection Mode
  • Secure Mode

Answer: Gateway Mode

Explanation: Gateway mode in Azure Cosmos DB uses the HTTPS protocol.

Gateway mode is slower than Direct mode in Azure Cosmos DB because:

  • It requires multiple physical connections
  • It has additional hop between the client and the Azure Cosmos DB service
  • Direct mode uses a faster TCP protocol
  • Both 2 and 3

Answer: Both 2 and 3

Explanation: Gateway mode is slower than Direct mode because it involves an additional hop between the client and the Azure Cosmos DB service, and also Direct mode uses a faster TCP protocol.

True/False: Direct Mode in Azure Cosmos DB is only supported for .NET SDK.

  • True
  • False

Answer: False

Explanation: Direct Mode is supported for .NET, Java, Node.js and Python SDKs.

Which of the following Connectivity modes is recommended for applications with high request rates, or that require low latency?

  • Direct Mode
  • Gateway Mode

Answer: Direct Mode

Explanation: Since Direct Mode uses TCP protocol which enables fast and efficient data transfer, it is recommended for applications with high request rates or requiring low latency.

True/False: Gateway Mode uses more client-side resources than Direct Mode.

  • True
  • False

Answer: True

Explanation: Gateway Mode uses more client-side resources than Direct Mode because with Direct Mode, clients directly connect to data node, reducing client resource utilization.

What are the recommended connectivity modes for applications that aren’t CPU-bound or network-bound?

  • Direct Mode
  • Gateway Mode
  • Both Direct and Gateway Mode
  • None of the above

Answer: Gateway Mode

Explanation: For applications that aren’t CPU-bound or network-bound, Gateway mode is recommended since it provides an optimum performance without needing additional client-side resources.

True/False: With Direct Mode in Azure Cosmos DB, there is no need to open the firewall ports as required by Gateway mode.

  • True
  • False

Answer: False

Explanation: With Direct Mode, firewall ports ranging from 10250 to 10300 need to be opened for connectivity which may not be suitable for environments with strict firewall rules.

Which mode in Azure Cosmos DB places a premium on performance over resource consumption?

  • Direct Mode
  • Gateway Mode

Answer: Direct Mode

Explanation: Direct Mode places a premium on performance over resource consumption. It maintains multiple connections to each Azure Cosmos DB partition to ensure high availability, throughput, and low latency.

True/False: With Gateway mode in Azure Cosmos DB, application deployment becomes simpler as there is only one port to manage.

  • True
  • False

Answer: True

Explanation: With Gateway mode, application deployment is simplified as there is only one port (443) to manage.

Which of the following describes the best why firewall rules could steer the choice of connectivity mode in Azure Cosmos DB?

  • Firewalls may block the multiple ports used in Direct mode.
  • Gateway mode requires opening many firewall ports ranging from 10250 to
  • Firewalls do not affect the choice of connectivity mode.
  • Both 1 and

Answer: Firewalls may block the multiple ports used in Direct mode.

Explanation: Firewalls may block the multiple ports used in Direct mode and thus affecting the choice of connectivity mode. With Gateway mode, only one port (443) needs to be opened.

Interview Questions

What is Connectivity Mode in Azure Cosmos DB?

The connectivity mode in Azure Cosmos DB defines how the clients will interact with Azure Cosmos DB service. The connectivity modes are Gateway and Direct mode.

What is the primary difference between Direct and Gateway mode in Azure Cosmos DB?

Direct Mode allows clients to connect directly to the individual partitions in Cosmos DB, leading to lower latency and higher throughput, while Gateway Mode establishes connections through an intermediary gateway and is simpler to configure.

In which scenarios is the Gateway Mode recommended to use?

It is recommended to use Gateway mode if an application is running in development environments or in the cloud with a VPN, or firewall.

What is the benefit of using Direct Mode in Azure Cosmos DB?

Direct mode supports both TCP and HTTPS, providing a faster and more efficient connection by eliminating the need for request routing via Gateways, thus reducing latencies and improving throughput.

What is the role of Protocol in Azure Cosmos DB connectivity settings?

The Protocol defines whether the client will communicate using TCP (Transmission Control Protocol) or HTTPS (Hypertext Transfer Protocol Secure).

How do the Azure Cosmos DB SDKs decide which connectivity mode to use?

By default, Azure Cosmos DB’s SQL API SDKs use Gateway mode for connectivity, though users can setting it to use Direct mode.

How can you change the connectivity mode in Azure Cosmos DB?

You can change the connectivity mode using the ConnectionPolicy class in Azure Cosmos DB SDKs.

What is preferred locations feature in Azure Cosmos DB connectivity setting?

Preferred locations feature allows you to specify the Azure regions to which your application prefers to connect to in order to achieve lower latencies.

Can you return to Gateway mode in Azure Cosmos DB after switching to Direct mode?

Yes, you can switch back to Gateway mode by adjusting the connectivity setting in your Azure Cosmos DB connection policy.

Can Direct mode be used in Azure Cosmos DB if the application is behind a firewall?

Generally, it is recommended to use Gateway mode if the application is behind a firewall. However, if Direct mode is used, proper firewall configurations must be in place to open ports for DocumentDB.

Does Direct mode support automatic failover in Azure Cosmos DB?

Yes, Direct mode supports automatic and manual failover enabling applications to maintain high availability.

How does Direct connectivity mode provide lower latency in Azure Cosmos DB?

Direct mode creates a direct connection to Azure Cosmos DB replicas, eliminating the step of routing requests through a gateway, thus reducing latency.

Is Gateway mode available in all Azure CosmosDB APIs?

Yes, Gateway mode is supported across all Azure Cosmos DB APIs including SQL, MongoDB, Cassandra, Gremlin, and Table.

What is the benefit of using Gateway mode in Azure Cosmos DB?

Gateway mode simplifies connection management, reduces client resource consumption, and is generally recommended for smaller workloads or development/testing environments.

Can you use both Gateway and Direct mode simultaneously in Azure Cosmos DB?

No, you need to choose one or the other. It’s not possible to use both modes concurrently in a single client instance.

Leave a Reply

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