There are two ways to connect to the Azure Cosmos DB. You need to specify the way you want to connect to Azure Cosmos DB in SDK. The way that you pick for connectivity mode can make a big difference for your application's performance.
First connectivity mode is Gateway Mode. This is the default way to connect Azure Cosmos DB in earlier version of SDK platforms. This method is mostly for applications that stays behind corporate firewall or It is for environments that have a limited number of socket connections. If your company have strict firewall restrictions, Gateway mode is the way to go for sure. Gateway mode uses standard HTTPS port and single endpoint. Here is the list of port numbers for different APIs using Gateway Mode.
SQL API 443MongoDB API10250 (No Global Distribution)10255(With Global Distribution)10256(With Global Distribution)Table API 443Cassandra API 10350GrapAPI 443
If you use Gateway Mode, your application does not connect to Azure Cosmos DB backend replicas directly. Gateway endpoint stays in the middle of your application and Azure Cosmos DB which causes latency go higher. Gateway Mode work like a cashier in a grocery store, you need visit cashier before you leave the store.
Second connectivity mode is Direct Mode. This is the default way to connect Azure Cosmos DB in .NET v3 SDK. Direct Mode is supported only on .NET and Java SDK platforms. This method lets your application connect to backend replicas directly. Because of that, Direct method delivers better latency between your application and Cosmos DB. You need to be sure that the port range between 10000 and 20000 is open. If you are getting HTTP 503 Service Unavailable error, you may need to check your open ports. Also, be sure that you create client instances are singleton since you don't want to create a new connection rather than using current one.
Direct mode works like self checkout of grocery store, it is more direct and latency is lower.
If your application is on Azure too, then it makes sense to keep Cosmos DB and your application in the same region to get the best latency. If your application lives in the same region with your Cosmos DB, latency will be within 1ms to 2ms. If your application lives in East region and your database lives in West region, latency will be more than 50 ms. To get the best results, keep your application and database in the same region and use Direct Mode.
No comments:
Post a Comment