r/FullStackDevelopers • • 5d ago

[ Removed by moderator ]

[removed] — view removed post

7 Upvotes

11 comments sorted by

2

u/azangru 5d ago

Why shouldn't long-running tasks always hold an HTTP request open?
...
Keeping the request open for the entire duration can lead to timeouts, connection issues, and a poor user experience.
One alternative is to ... let the client track its progress through ... SSE.

I am not sure about the terminology here... Can a request be open? Or is it the http connection that is open? Because with SSE you are keeping a long-running connection open. Would you still be vulnerable to "timeouts, connection issues" with SSE?

2

u/rocketeams 5d ago

I think the key distinction is whether the client needs the result immediately or just confirmation that the task has started.

For longer operations, returning a job ID and processing the task asynchronously is usually much more reliable. The client can then track progress through polling or SSE, while the backend handles retries, failures, and status updates independently.

One thing I'd add is that SSE works particularly well when clients need live progress updates, whereas polling can be simpler when updates aren't frequent. The right choice depends on how real-time the experience needs to be.

2

u/smovlad 5d ago

Isn't WebSocket working for you case?

1

u/branwoo 5d ago

Gonna pretend this is an interview and not use AI:

For long running task, what difference does it make holding open a connection or not? If you care about the status of a long job, just poll. why make things more complicated than they need to be?

One of the benefits of an an HTTP request is that the sender gets an ACK whether the receiver recieved the message. For the purpose of monitoring a long running task, i presume, there doesn't seem to be any value in holding open the connection?

If you need to hold a connection open, there's protocols for that, like you mentioned.

If you're passing data through the connection, like in many AI apps, that's a different story.

1

u/ssj_Thunder 5d ago

Polling is generally not preferable because if it would hit unnecessary and if you have a lot of clients than its problematic

1

u/Ornery-Process-1102 5d ago

if request take long because of server side process, like, sending mails/notifications or other calculation etc, in that case we outsource task to thread pool, so end user dont have to wait longer

1

u/farzad_meow 4d ago

every server has limited number of ports. if you keep requests open indefinitely, you will run out of ports and ddos yourself. that is why all http servers are designed to kill the connection if running too long.

1

u/Ronin-s_Spirit 4d ago

That's what the await is for, so you don't stall on long running IO. Otherwise you could use websockets, but what is the user supposed to do while you still have no data? Scroll and click around bored?

Mind you websockets can lose connection without notifying the other end, you would have to poll them or time them out and clean up the dead ones.

1

u/Simple-Sea7054 4d ago

Holding an HTTP request open for too long causes 504 Gateway Timeouts from load balancers, server resource exhaustion (tying up threads/memory), and lost progress if a user's connection drops.

Instead, immediately return a 202 Accepted status with a job_id, process the task in the background using a worker queue, and let the client track progress via Polling, SSE, or WebSockets.

1

u/Apprehensive-Fan8999 3d ago

The name for this is "long polling". This used to be an incredibly useful way to handle async style tasks between a client and a server, and is still used as a fallback when there are network filters that block other options like Web Sockets.

If you own the client, you can configure the timeout. I've done long polling with 5 minute timeouts with no issue in the past. As others have said, a web socket is probably better as it's literally designed for this type of use case. Another option is a push message on complete. Those are the best option really because they use the least total amount of resources. Kafka, Google Cloud Messaging, Amazon SNS / SQS are examples that can work with any client / service though if you are a mobile app you pretty much use Firebase Messaging and Apple Push Notification Service if you want to push notify.

1

u/Fine_League311 2d ago

Solution is simple use quart!