The Simplest Way to Make Haproxy and TimescaleDB Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

You pair haproxy and timescaledb by routing client traffic through HAProxy to active database nodes. This setup spreads read queries across replicas and directs write traffic to your primary instance. Time-series apps send huge bursts of metrics and event logs every second. A single database node can drop packets when too many apps open sockets at once. Putting a load balancer in front of your database cluster fixes this bottleneck. It checks node health and routes traffic away from failed inst

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.

You pair haproxy and timescaledb by routing client traffic through HAProxy to active database nodes. This setup spreads read queries across replicas and directs write traffic to your primary instance.

Time-series apps send huge bursts of metrics and event logs every second. A single database node can drop packets when too many apps open sockets at once. Putting a load balancer in front of your database cluster fixes this bottleneck. It checks node health and routes traffic away from failed instances right away.

How haproxy and timescaledb handle traffic and roles

HAProxy operates at the transport layer when it routes PostgreSQL wire traffic to TimescaleDB. It does not read the SQL queries inside the network packets. Instead, it forwards raw TCP streams from the client app to the right database server. You set up one listener port for writes and a second port for read queries.

Health checks keep this data flow safe during node failures or maintenance windows. HAProxy runs a lightweight check against each database node every few seconds. It asks the node if it is the primary writer or a read replica. If the primary node goes down, the cluster promotes a standby node to take over. HAProxy sees this change on the next check and shifts write traffic to the new leader.

Database roles and login rules still live inside TimescaleDB itself. Because the load balancer forwards raw TCP packets, the client must still log in directly. The app sends its username and password or TLS certificate through the open tunnel. The database checks those credentials against its own user table before allowing any queries. TLS termination can happen at the load balancer or pass straight through to the database.

Common failure modes and how to scope network paths

Teams often run into trouble when they use one shared database account for every app. When all traffic flows through HAProxy, the database sees the load balancer IP address. It does not see the original client IP unless you enable a specific protocol header. If five apps share one login and come from one IP, audit logs lose value. You cannot tell which app ran a bad delete query or locked a table.

Broad permissions make this logging gap much worse for security teams. Many teams give their main app role full write rights on all hypertables. If an attacker steals that shared key, they can read or drop all time-series data. Engineers who log in for debugging often use this same wide admin role too. Unlogged ad-hoc queries from staff laptops then mix with normal app traffic on the cluster.

End-to-End Encryption + HAProxy Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

You can fix this by splitting database roles by task and locking down network paths. Create one role that can only insert new rows into specific time-series tables. Create a second role for dashboards that can only read from continuous aggregates. Set up your firewall so TimescaleDB only accepts traffic from the HAProxy host subnets. Block direct user logins to the database nodes from office networks or developer machines.

Separating human queries from automated app traffic

Automated apps and human engineers need very different rules when they query production data. Apps run the same safe insert and select queries millions of times a day. They need fast TCP routing through HAProxy and stable connection pools. Humans run rare, complex queries to fix bugs or check system state.

Routing human engineers through the same static load balancer listener creates serious audit gaps. Static database passwords stored on developer laptops can leak or stay active after staff leave. You should separate machine traffic from human debugging sessions at the network level. Keep HAProxy dedicated to fast app traffic and health checks between your core services. Force human sessions through an identity workflow that logs every single command by user email. You can also apply data masking to rewrite sensitive fields like emails or phone numbers in query results.

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

Does HAProxy understand SQL queries sent to TimescaleDB?

No, HAProxy routes TimescaleDB traffic at the TCP layer without reading the SQL text. You use separate ports on the load balancer to split read and write traffic.

How does HAProxy know which TimescaleDB node is the primary writer?

HAProxy runs periodic health checks against an HTTP endpoint or a custom check script. The check returns a healthy status only when the target node is the active primary.

Why do database logs show the HAProxy IP instead of the client IP?

HAProxy opens a new TCP socket to the database on behalf of the client app. The database sees the load balancer address unless both tools use the Proxy Protocol header.

The query runs. The emails stay hidden.