Skip to Content

CHALLENGES AND SOLUTIONS FOR COSMOS DB CONNECTIVITY IN AZURE

Mar 5, 2025
Abdul Nasir Moonakal

When integrating Azure Cosmos DB with microservices, choosing the right connectivity mode is crucial for performance, security, and stability. However, many teams face challenges when configuring connectivity, particularly with firewall restrictions and port requirements. In this blog, we will explore the key issues encountered while connecting a Spring Boot microservice to Cosmos DB and discuss the resolution approach.

Architecture Overview

Below is a snapshot of the components deployed:

  • Azure API Management (APIM): A platform that enables publishing and securely orchestrating APIs to backends.
  • Azure Web App: Hosts the GraphQL APIs, deployed as a Spring Boot microservice.
  • Azure Cosmos DB: A fully managed NoSQL and relational database for modern application development. Various programming languages offer SDKs to establish connectivity with Cosmos DB.

Issues Observed

  1. The Spring Boot microservice deployed as a web app failed to establish connectivity with Cosmos DB.
  2. Direct mode connectivity uses random ports to establish a connection between the web app and Cosmos DB.
  3. According to Microsoft’s documentation, direct mode communication requires firewall access to ports 0-65535, posing a security risk.
  4. Given the wide range of ports required, the network team identified this as a critical security concern.

Microsoft Guidance on Connectivity Modes

Microsoft offers two modes of connectivity: Direct Mode and Gateway Mode. While direct mode is recommended for its lower latency, it requires opening a large range of ports, which may not be feasible in secure environments. Gateway mode introduces an additional hop but provides a more controlled and firewall-friendly approach.

ModePort RequirementHardware RequirementLatencyPricing
Direct0 through 65535NALowNo additional charge
Gateway (standard)443NA(hardware for gateway is shared)HighNo additional charge
Gateway (dedicated)443Additional hardware for gateway provisioningHighAdditional charge for hardware

Resolution Approach

Lower Environments:

  • Given that Gateway Mode (Standard) only requires port 443, it was selected for lower environments to ensure easier firewall whitelisting.
  • This approach avoids additional infrastructure costs while ensuring stable connectivity.

Higher Environments:

  • Gateway Mode (Dedicated) was chosen for production and higher environments since it provides dedicated hardware for traffic handling.
  • As port 443 is a standard port with a high probability of firewall approval, this ensures a secure and reliable connection.

Pricing and Infrastructure Provisioning

The dedicated gateway mode incurs additional costs based on the number of vCores provisioned. It is recommended to refer to the Azure Pricing Portal for the latest pricing details.

Terraform Provisioning for Dedicated Gateway

Terraform provides a resource to provision dedicated hardware for Cosmos DB. This can be integrated into the Cosmos account provisioning workflow to ensure seamless deployment.

Configuration Updates

With the dedicated gateway provisioned, the connection string must be updated to use the revised gateway connection keys instead of the primary key used in direct mode.

By implementing these changes, we successfully established a stable and secure connection to Cosmos DB while ensuring minimal impact on latency and performance.

Conclusion

Choosing the right connectivity mode for Cosmos DB is key to balancing security, performance, and cost. Direct Mode offers low latency but has extensive port requirements, while Gateway Mode (Dedicated) provides a more secure and manageable alternative for production. Organizations should assess their infrastructure and security needs to make an informed decision, keeping Microsoft’s guidance and pricing updates in mind.

About the author

Senior Manager | India
Abdul Nasir works as a DevOps consultant within Sogeti OneDeliver, his areas of expertise includes working with customers understanding their needs and helping them in their journey of achieving value through transformation. Abdul’s areas of interest are around DevOps tooling and cloud architecture.

Leave a Reply

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

Slide to submit