The Simplest Way to Make Cosmosdb and Haproxy Work Like They Should
Running cosmosdb and haproxy together lets your apps route database traffic through fixed network endpoints. This setup manages open TCP pools, checks backend health, and handles region failovers for your data. Cloud databases often change their underlying IP addresses during routine maintenance or regional shifts. Load balancers give your internal services a single, stable address to call every time. When you pair these two systems, your apps avoid connection drops and timeout errors. How tr
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Running cosmosdb and haproxy together lets your apps route database traffic through fixed network endpoints. This setup manages open TCP pools, checks backend health, and handles region failovers for your data.
Cloud databases often change their underlying IP addresses during routine maintenance or regional shifts. Load balancers give your internal services a single, stable address to call every time. When you pair these two systems, your apps avoid connection drops and timeout errors.
How traffic and keys flow between cosmosdb and haproxy
Your app starts a request by calling the local load balancer address on a set port. The load balancer checks which cloud database region is healthy and open for traffic. It then forwards the encrypted request straight to the active cloud database endpoint.
In most setups, the load balancer runs in TCP mode or uses TLS passthrough. The client app holds the cloud identity token or master key for the database. The load balancer does not read the database payload or hold the database secret. Instead, it passes the signed request through to the cloud endpoint for checking.
Once the cloud service checks the key or role, it runs the query. The response travels back through the same network path to the waiting app. This flow keeps latency low while giving network teams one place to watch traffic volume.
Common failure modes and how to scope network paths
Many teams make the mistake of sharing one master database key across all apps. When every service routes through one load balancer with the same key, audit logs break. You cannot tell which app or user ran a slow or destructive query.
Broad network access is another common risk in this setup. If any internal host can reach the load balancer port, any host can try database calls. A leaked read-write token then gives an attacker a straight path to your production data.
CosmosDB RBAC + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Unlogged queries also create blind spots for security and compliance teams. A basic TCP load balancer only records bytes sent, source IPs, and connection times. It does not record which user ran a query or what records came back.
To fix this, give each app its own scoped role in your cloud directory. Grant read-only roles to reporting tools and write roles only to core backend services. Lock down your network firewall so only approved app subnets can call the load balancer. Finally, set the cloud database firewall to accept traffic only from your load balancer IPs.
Keeping connections stable during region failovers
Cloud databases replicate data across regions to stay online during big outages. Your load balancer needs active health checks to spot when a primary region goes down. If a check fails, the load balancer shifts new sessions to the backup region right away.
DNS caching can also cause trouble for long-running load balancer processes. If the cloud provider changes an endpoint IP, an old DNS cache breaks routing. Set your load balancer to refresh DNS records on a short timer using trusted resolvers. This simple rule keeps traffic flowing when cloud endpoints shift behind the scenes.
Add runtime guardrails and masking beside your databases and APIs
Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.
Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Should HAProxy terminate TLS for CosmosDB traffic?
Most teams use TLS passthrough so the app validates the official cloud certificate directly. If you terminate TLS at the load balancer, you must manage internal certificates and re-encrypt traffic.
Why do database connections drop through the load balancer?
Idle timeouts often mismatch between the client SDK, the load balancer, and the cloud firewall. Set your load balancer idle timeout lower than the cloud firewall limit to close dead sockets cleanly.
How do we restrict which apps can reach the database?
Use network security groups to limit which app hosts can reach the load balancer port. Pair this with role-based permissions so each app logs in with its own scoped identity.
Related guides
In this post