How to do akka-http request-level backpressure?
Asked Answered
D

0

4

In akka-http, you can:

  1. Set akka.http.server.max-connections, which will prevent more than that number of connections. Exceeding this limit means clients will get connection timeouts.
  2. Set akka.http.server.pipelining-limit, which prevents a given connection from having more than this number of requests outstanding at once. Exceeding this means clients will get socket timeouts.

These are both forms of backpressure from the http server to the client, but both are very low level, and only indirectly related to your server's performance.

What seems better would be to backpressure at the http level, based on request rate as seen by the server. Probably by returning 429 - Too Many Requests. Request rate is arguably an indirect measure of performance too, but it seems closer than number of connections.

This seems like a fairly reasonable thing, but I'm having trouble finding any existing patterns. This is the closest reference I can find: https://github.com/akka/akka-http/issues/411

From what I can tell, the best approach would be to grab the Flow you turn your Route into, and insert it into a graph that has a global measure of request rate (or maybe a single processing queue) and a short-circuit that bypasses the Route (by returning 429 or whatever) entirely.

Are there better ideas?

Deadlock answered 13/10, 2017 at 22:12 Comment(2)
Possible duplicate of How to limit an Akka Stream to execute and send down one message only once per second?Polycythemia
Not quite. Assuming manipulating the Flow is the best course, I don't want to limit the rate of the flow, because that would backpressure into the connections and the network. I want to complete some requests immediately using another mechanism, based on rate. Hm, perhaps that is something that could be baked into a Route/Directive.Deadlock

© 2022 - 2024 — McMap. All rights reserved.