DEV Community

Cover image for Short Polling vs Long Polling
Dev on Remote
Dev on Remote

Posted on • Originally published at devonremote.com

Short Polling vs Long Polling

Short polling and long polling are two techniques used in web development to continuously check for updates on a server without the user having to manually refresh the page. These strategies enable web applications to deliver updates to users in what feels like real-time.

Short Polling

Short polling involves the client sending requests to the server at set intervals to inquire about new data or updates.

Example:

  1. Client sends a request
    Server responds immediately with a handle (request id, job id, etc)

  2. Server processes the request (may take some time)

  3. Client uses the handle to check for status of the request, may disconnect since the handle was saved.

  4. Client sends multiple short requests with the handle checking the status of the processed request

Pros:

  • Straightforward to implement.

  • Enables the client to safely disconnect and reconnect, using the saved handle to resume where it left off.

  • Suitable for long-running asynchronous backend requests.

Cons:

  • Scaling issues due to increased demand on resources as more clients perform checks.

  • Increased network traffic, quickly leading to congestion.

  • Inefficient resource use

Long Polling

The client makes a request to the server, but the server holds the response until there’s new data available to send back. Once the response is sent, the client immediately makes another request.

Basically the difference between short and long poll is that here server doesn't respond to you until response is ready. If it isn't ready, no status is send to the client.

Example:

  1. Client sends a request

  2. Server responds immediately with a handle (request id, job id, etc)

  3. Server processes the request (may take some time)

  4. Client checks the status of the request, but server doesn't respond until it has the response ready

  5. Sometimes there is a timeout, so we don't poll forever

Pros:

  • More scalable than short polling as it reduces the frequency of requests.

  • Reduces network congestion by waiting to send data until there's something new.

  • Allows clients to disconnect and later reconnect using the saved handle, ideal for asynchronous tasks.

  • This method is beneficial for long-running tasks, offering an alternative when traditional request/response models or short polling are less efficient.

Cons:

  • It's not a real-time solution - delays can occur between the completion of data processing and the next client request, potentially missing rapid sequential updates.

  • If the server processes multiple updates between client requests, it may only return the latest update, omitting intermediate ones.

Note: The challenges of not being truly real-time and the potential of missing updates also apply to short polling. However, they were primarily highlighted in the long polling section due to its comparative advantages in other areas.

When to Prefer Long Polling?

  • For operations that require extended processing time, such as file uploads.

  • When the application needs to receive backend notifications or updates efficiently.


Cheers! Hope it clarifies this topic a little bit πŸš€

Top comments (0)