Axum SSE and backpressure

I'm using Axum SSE to send out events and was wondering what, if any, built-in mechanisms there are to handle back-pressure. For example, when sending out events, if the consumer (web browser) can't process them fast enough or suspends for some reason, then I'm guessing that program's task associated with sending the events will also suspend.

In an ideal scenario, I'd like to drop the SSE consumer's connection instead of suspending my server's tasks.

Thanks for confirming what Axum or even Hyper should do in this scenario.

I haven't worked with this implementation specifically, but usually if some async send is involved, it will make you .await if buffers for sending are full.
There's a lot of bufferbloat in every part of the stack and across the Internet, so you may end up sending 8KB or more into the void before anything starts pushing back. If you need messages to be low latency and not pile up, then pacing of sending is a tricky topic and needs special care (most likely careful timing and active feedback from the recipient).

If you just want to avoid needless waiting on your server, then the simplest solution would be to add a timeout on send, and close the connection on timeout (you don't know how much has been sent so far, so closing is safest).

To skip messages while the connection is obviously busy, you can wrap it in an async Mutex, and only use try_lock when locking it for sending. This way while it's busy sending, other attempts to lock it will fail.

1 Like

Thanks for the reply. To provide some more information, the way Axum works is to return an item to a Stream - there's no "send" to speak of and thus nothing to timeout on there. However, I just noticed a timeout stage for futures-timeout that might help me...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.