The Simplest Way to Make Azure Cosmosdb and Haproxy Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair azure cosmosdb and haproxy to route database traffic over private network paths. This setup lets internal apps reach cloud data endpoints through fixed entry points. Engineering teams often run apps across hybrid networks or strict cloud subnets. Direct traffic from every worker node to managed cloud databases creates firewall sprawl. Routing requests through HAProxy gives network teams one place to manage outbound connections. Still, putting a load balancer in permissions of a cloud

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.

Teams pair azure cosmosdb and haproxy to route database traffic over private network paths. This setup lets internal apps reach cloud data endpoints through fixed entry points.

Engineering teams often run apps across hybrid networks or strict cloud subnets. Direct traffic from every worker node to managed cloud databases creates firewall sprawl. Routing requests through HAProxy gives network teams one place to manage outbound connections. Still, putting a load balancer in permissions of a cloud database brings unique challenges.

How traffic and keys flow between azure cosmosdb and haproxy

Clients talk to Azure CosmosDB using HTTPS APIs or wire protocols like MongoDB. When you put HAProxy in the middle, it acts as a traffic forwarder. You can set up HAProxy in TCP mode to pass encrypted packets straight through. You can also terminate TLS at HAProxy and re-encrypt traffic to the cloud backend.

In both modes, HAProxy does not handle database login on its own. Your app or user still holds the master key, resource token, or identity token. The client sends this secret inside the request payload or header through HAProxy. Azure CosmosDB checks the key when the packet arrives and then runs the query.

Database SDKs also try to discover partition addresses on the backend. When clients use direct mode, the SDK asks for backend node IP addresses. If those addresses do not route through HAProxy, client requests will fail. Teams must set the client SDK to gateway mode so traffic stays on the main endpoint.

Common failure modes and how to scope network paths

The biggest risk in this setup is sharing broad master keys across apps. Because HAProxy forwards traffic, any person on that network path can reach the database. If teams share one primary read-write key, you cannot tell who ran a bad query. Network logs in HAProxy only show IP addresses and byte counts, not database queries.

Shared service accounts also make key rotation hard for platform teams. When an engineer leaves the team, rotating a shared master key can break apps. Instead, teams should use Azure role-based rules and separate keys for each app. Give read-only roles to reporting tools and scope write roles to single containers.

Azure RBAC + CosmosDB RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Certificate errors also cause outages when pairing these two tools in production. Cloud database endpoints expect the client request to match the exact cloud hostname. If HAProxy forwards traffic without setting the right host header, the TLS handshake fails. You must set the server name rule in HAProxy to match your cloud account URI.

Health checks need care so they do not waste request units or hit rate limits. Simple TCP checks on the database port confirm that the cloud endpoint is reachable. Avoid running heavy database queries from the load balancer just to check node health. Keep network ACLs tight so Azure CosmosDB only accepts traffic from your HAProxy nodes.

Data privacy is another gap when developers query production containers for debugging. Network load balancers pass full query results back to the client without checking the payload. If a container holds emails, phone numbers, card numbers, or national IDs, raw data flows out. Data masking rewrites matched fields in results before they reach the client tool.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Should I run HAProxy in TCP mode or HTTP mode for Azure CosmosDB?

Use TCP mode if you use wire protocols like MongoDB or Cassandra APIs. Use HTTP mode only for SQL API traffic when you need header rules.

Why do my Azure CosmosDB SDK clients bypass HAProxy after the first request?

The SDK defaults to direct mode and fetches backend node addresses after login. Switch the SDK to gateway mode so all requests stay on your HAProxy address.

Does HAProxy log the actual queries sent to Azure CosmosDB?

No, HAProxy only logs network details like client IPs, status codes, and timing. It does not parse database wire protocols or record specific document reads and writes.

The query runs. The emails stay hidden.