haproxy icon indicating copy to clipboard operation
haproxy copied to clipboard

Stick counters per transaction

Open tonireed opened this issue 2 years ago • 1 comments

Your Feature Request

It would be helpful, if HAProxy had stick counters per transaction.

What are you trying to do?

The scope of variables can be a session (sess) or transaction (txn). For sticky counters this is not the case. The http-request track-sc and http-response track-sc actions associate an entry in a stick table with a session. However, it is not possible associate an entry with a transaction.

An important use of sticky counters is rate limiting. This is, for example, explicitly mentioned as feature of HAProxy Enterprise. At the moment, sticky counters can only be used for rate limiting with keys that do not change during a session, for example, as per the documentation, source or destination address and port or information derived thereof, for example, ASN or country, or TLS session ID. Once a stick table entry is associated with a session, it remains associated for the lifetime of the session.

Unfortunately, rate limiting per source address, information derived thereof or TLS session ID is coarse, imprecise and difficult. In times of carrier-grade NAT, company proxy servers, IPv6, VPNs and proxy server services that offer large amounts of source addresses, a source address cannot be taken for granted to represent a single person, household or server. In some cases, IP addresses or TLS session IDs also might not be available. Therefore, precise rate-limiting without a large number of false-positive decisions requires other identifiers, for example, (web application) session IDs, usernames or account numbers. However, it is not possible to use sticky counters with such keys, as either (forward) proxy servers that are used by companies or other organisations or reverse proxy servers might combine HTTP requests of multiple clients in one connection (in accordance with the semantics of HTTP). Consider the following example:

http-request track-sc0 req.cook(app-session-id) table session-id-table
http-request inc-gpc0
http-request deny deny_status 429 if { sc0_gpc0_rate gt 10 }

Once an entry is associated with the session, subsequent requests are counted for the value from the cookie "app-session-id" that was first encountered. If a proxy server of a company combines the requests of multiple employees, who are assigned different session IDs by the web application, the requests are counted for the first session ID that was encountered in the connection and the rate limits do not work as intended.

It would be possible to implement rate limiting in the web application but this is often orders of magnitude slower: most web applications are often written in interpreted languages and maintain their state in databases on separate database servers. HAProxy is much more efficient and can already synchronise stick tables efficiently. Therefore, it would be helpful if HAProxy could also be used in this case.

Assuming that HAProxy processes all transactions of a session in order, the simplest way to implement sticky counters per transaction would be to have an http-response untrack-sc action. However, HTTP/1.1 already introduced some parallelism and HTTP/2 is inherently parallel. So a separate action for per transaction sticky counters that saves the sticky counter entries in the http_txn data structure would be required. Unfortunately, I could not come up with names for these actions that are not confusing but perhaps track-sc-txn exemplifies the intended semantics.

I could provide a patch but it seemed best to see whether the project's maintainers and architects would accept a patch for this feature and how they would prefer it to be implemented.

Output of haproxy -vv

This feature request was written when HAProxy 2.8 was the latest LTS version.

tonireed avatar Jan 25 '24 14:01 tonireed

No, if you perform track-sc during an http-request, it's only for the current transaction and it ends once the response is delivered. Only tcp-request connection and tcp-request session span over a session.

wtarreau avatar Jan 26 '24 08:01 wtarreau