Urgent.News

What's breaking now, across thousands of outlets.

Tech

Cloudflare Workers caching: store values, not in-flight promises

A module-level cache can reuse completed data across requests in a Cloudflare Worker. Sharing a pending operation is a different decision: the work behind that promise may belong to the request that started it. Our rule for Teasa's isolate caches is to share completed values and let concurrent misses perform their own reads. This article explains that tradeoff, provides a small JavaScript…

Cloudflare Workers caching offers a method to store completed data values, rather than pending in-flight promises. The rationale behind this approach is to avoid sharing a pending operation, as the work behind that promise may be tied to the specific request that initiated it. The recommended strategy is to share completed values and let concurrent requests perform their own reads.

The article provides a JavaScript implementation of this technique, accompanied by tests that demonstrate its effectiveness in avoiding issues where a request reads data that hasn't completed, while another request still waits for an answer. Deduplicating concurrent work can be useful in many JavaScript applications, but it's crucial to understand that sharing a promise can lead to unintended consequences.

Cloudflare explicitly warns against the practice of performing I/O (Input/Output) on behalf of a different request, emphasizing that a completed JSON object is fundamentally different from a live response body. The key distinction lies in whether a later invocation needs the work or resources from an earlier one.

The solution involves separating the read from the reusable value, following these three steps:

1. Return an unexpired value if one exists.

2. On a miss, let this invocation perform and finish its own read.

3. Store the resulting data after that read succeeds.

This approach leads to two simultaneous misses, resulting in two independent reads, which is the intended behavior. By using this pattern, concurrent misses read independently, with neither caller waiting for the in-flight operation of the other.

The provided code example demonstrates a small standalone cache utility, suitable for public data with a TTL (Time To Live) and a limit on the number of entries. The example shows how a Worker can read a configuration file from its static assets binding, parse it, and cache the resulting data for subsequent requests.

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

When the user is busy, don't drop the UI action: a small replay queue in Node.js

Disclosure: I'm the developer of the tool described here, and this post was drafted and published by an AI agent from the project's source code.

  • Relaymote tool in Node.js implements a small replay queue for UI actions.
  • Queue enforces rules to prevent dropping actions and ensure safety.
  • Queue provides bounded retries and audit log for failed actions.

Turn a Lighthouse export into tickets with traceable threshold evidence

A performance backlog becomes difficult to maintain when a ticket says only that a page is slow. The developer needs the measured value, the device strategy, the source capture time and the rule that…

  • Turn audit batch into reports for each URL and strategy pair
  • Preserve snapshot source capture time for specific saved example
  • Apply threshold models to mobile and desktop records

One UDP port, several Windows users: LAN discovery that works on a shared PC

Disclosure: I'm the developer of Bolo, the app this code comes from, and this post is published from my account. It was drafted and published by an AI agent working from the project's source code.

  • Two discovery methods: mDNS/DNS-SD and UDP probe
  • UDP probe sends 9-byte string every 2 seconds
  • Shared PC solution uses shared folder and heartbeat files

More from Saturday 24 October →