Urgent.News

What's breaking now, across thousands of outlets.

Tech

Polling versus events for Node background work

[ EXECUTIVE TEARDOWN // TL;DR ] setInterval schedules by start time, so a job slower than its interval overlaps itself and piles up; chain setTimeout instead. A poll that finds nothing still costs a query, a round trip and a wake-up, multiplied by every instance you run. Events remove the idle cost but add delivery guarantees you now own; a missed notification is a job that never runs. The…

SetInterval schedules background work by start time, which can cause jobs to overlap and pile up when a task takes longer than its interval. This leads to a growing overlap until the pool of resources is exhausted. To address this issue, use setTimeout instead, creating a gap between runs instead of a strict schedule. However, polling still has downsides: each idle check costs a query, round trip, and wake-up, which can add up quickly, especially on metered databases or battery-powered systems.

Events, on the other hand, remove the idle cost but add the responsibility of handling delivery guarantees. Missed notifications result in jobs that never run, creating a reputation for occasional failures. The recommended approach is to use both methods, leveraging events for latency and a slow poll as a safety net. Each background worker should start with setInterval to check for tasks, but also include a slow poll for reliability.

Implement safe concurrent execution by using a draining flag to prevent overlapping tasks. This layered approach provides the benefits of both methods, ensuring low latency, minimal idle cost, and reliable job execution without the risk of silent failures.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

OpenSpec Rejected Proposals: A Decision Memory Convention

An agent that proposed and shipped "move persistence into a shared library" six months ago will happily propose it again next quarter unless something durable tells it the idea was already…

  • OpenSpec proposals lack decision memory, making it unclear if ideas were previously declined.
  • /opsx:archive feature does not provide a rejection counterpart to track declined ideas.
  • Decision.md pattern and schema proposed to address decision memory gap without core support.

From Madrid to Zagreb, Europe's autonomous vehicles are multiplying

From Tesla's FSD rollout to driverless lorries connecting Lidl's warehouses, a wave of approvals across the EU shows regulators embracing autonomous mobility faster than expected — and the pace shows…

  • Multiple EU nations approved Tesla's Full Self-Driving mode
  • Madrid grants Uber, WeRide, and AVOMO permission for autonomous testing
  • Verne tests autonomous rides in Zagreb using Nvidia technology

More from Friday 18 September →