Why Your Node.js App Feels Laggy: Time to Rethink Polling

Kishan KareliyaKishan Kareliya
4 min read

Ever wondered how chat apps, live dashboards, or notification systems keep the UI updated with fresh data? Behind the scenes, it often comes down to one of two techniques: short polling or long polling.

In this guide, I’ll walk you through both — without buzzwords or confusion. Whether you're building your first real-time feature or optimizing an existing app, understanding these strategies is key.


What is Polling, Anyway?

Imagine a customer repeatedly asking a shopkeeper:
“Hey, did my package arrive yet?”

That’s polling in a nutshell. The client (browser) keeps asking the server if there's anything new.

  • If there's data — return it.

  • If not — either return nothing (short polling), or wait until you have something (long polling).

Now, let’s unpack both types.


Short Polling: The “Check Frequently” Approach

In short polling, the client sends a request at fixed intervals — say every 2 seconds — no matter what.

Example:
A weather app checking every 30 seconds for new temperature data, even if nothing changed.


Pros of Short Polling

  • Dead Simple to Implement
    Just use setInterval() and make a GET request.

  • No Need to Hold Connections
    Server replies immediately. Good for basic HTTP servers.

  • Works with All Infrastructures
    No problems with firewalls, CDNs, or proxies.


Cons of Short Polling

  • Wastes Resources
    Most of those frequent requests return nothing — still cost CPU, memory, bandwidth.

  • Can Miss Fresh Events
    If something happens right after a poll, user waits till the next one.

  • Doesn’t Scale Well
    Multiply 1 user’s polling by 1000 users — that's a lot of traffic doing nothing.


When Should You Use It?

  • Internal dashboards that refresh data every few seconds

  • MVPs or quick demos where real-time accuracy isn’t critical

  • Environments where advanced techniques like WebSockets aren’t an option


Short Polling in Node.js

// Short Polling Server (server.js)
app.get('/poll', (req, res) => {
  res.json({ message: latestMessage || null });
});

// Client: poll every 2 seconds
setInterval(async () => {
  const res = await fetch("/poll");
  const data = await res.json();
  if (data.message) console.log("New message:", data.message);
}, 2000);

Node.js handles these frequent requests well if your I/O is asynchronous. Thanks to the event loop, the server stays non-blocking.


Long Polling: The “Wait Until Something Happens” Strategy

Instead of constantly checking, the client says:

“Here’s a request. Keep it open until you have something.”

If something happens immediately — great. If not, the server waits, maybe up to 30 seconds, then responds.

After getting a response, the client sends another request — and the cycle continues.


Pros of Long Polling

  • Real-Time Feel Without WebSockets
    Server responds the moment something happens.

  • Fewer Useless Requests
    Unlike short polling, you’re not spamming the server.

  • Better User Experience
    Especially for apps like chat, live notifications, etc.


Cons of Long Polling

  • Held Connections Cost Resources
    Each open request ties up memory, file descriptors, etc.

  • More Logic Required
    You need to manage timeouts, dropped connections, retries.

  • Doesn’t Work Everywhere Perfectly
    Some CDNs, proxies may cut off long-held requests.


When to Use Long Polling?

  • Real-time apps without WebSocket support

  • Chat apps, comment systems, live collaboration tools

  • Notification systems that need instant delivery


Long Polling in Node.js

// Long Polling Server (server.js)
app.get('/poll', (req, res) => {
  if (latestMessage) {
    res.json({ message: latestMessage });
    latestMessage = null;
  } else {
    clients.push(res);
    setTimeout(() => {
      clients = clients.filter(c => c !== res);
      res.json({ message: null });
    }, 30000); // 30-second timeout
  }
});

// When new data arrives
app.post('/send', (req, res) => {
  latestMessage = req.body.message;
  clients.forEach(clientRes => clientRes.json({ message: latestMessage }));
  clients = [];
  res.send("Message sent");
});

This works well in Node.js thanks to its non-blocking nature. Even while waiting on clients, the event loop stays free to handle other users.


Short Polling vs Long Polling: Quick Comparison

FeatureShort PollingLong Polling
Request FrequencyFixed intervalOne at a time, re-poll on response
Server ResponseImmediateDelayed until data or timeout
Network UsageHigherLower
Data FreshnessDelayedNear real-time
ComplexityEasierSlightly complex
Best ForInfrequent updatesReal-time apps

Final Thoughts

Polling is one of those “boring but essential” concepts in real-time systems. It may not be flashy like WebSockets, but when used right — it just works.

  • Use short polling when you want quick setup and can tolerate delays.

  • Use long polling when freshness matters but WebSockets aren’t an option.

Node.js makes both easy to implement, thanks to its async architecture. As you grow as a backend developer, knowing when and how to apply these patterns gives you a solid edge in building real-time experiences that scale.


If this helped, consider bookmarking it for future reference or sharing it with a fellow developer. Polling may be old school — but it’s far from obsolete.

Happy coding! 🚀

0
Subscribe to my newsletter

Read articles from Kishan Kareliya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kishan Kareliya
Kishan Kareliya