Leaving Deno Deploy for Cloudflare Workers: the port that compiled and lost 46 of 50 clicks

By Esteban Trillo8 min readDenoCloudflareTypeScript

Piwi helps carry the Deno app out of Deploy before it closes. The KV crate on top gets a red tag on the way to Cloudflare: not the same thing.
Piwi helps carry the Deno app out of Deploy before it closes. The KV crate on top gets a red tag on the way to Cloudflare: not the same thing.

Deno Deploy has about four months left.

On October 9, Deno announced it’s joining Cloudflare, and the line that matters if you host anything there is this one: Deploy “will continue operating for six months before shutting down.” There’s no calendar date yet. Six months from October 9 is early April 2027.

So I took a small Deno app, the kind people actually put on Deploy, and moved it to Cloudflare Workers. The routing took ten minutes. Then I ran a load test against the port:

bun race.ts
claims: 1 of 20 accepted
clicks: 4 of 50 counted

Fifty clicks went in and four were counted. There was no error and no warning, and the types were fine. The other 46 just weren’t there.

This post is about that bug. The Deno.serve part of a migration is easy. The hard part is that Deno KV was quietly giving you a guarantee, and the obvious replacement doesn’t.

What actually changed

Three different things are ending on three different schedules, so it’s worth getting them straight:

  • Deno Deploy shuts down about six months after October 9. Paying customers get migration help to Workers. Everyone else gets the blog post.
  • The Deno runtime goes into maintenance. The pinned issue says 2.9.x gets a monthly LTS release with bug and security fixes “until October 2027”, and feature PRs will be closed. It stays open source, and the team says they’re working on moving it into a foundation.
  • JSR keeps running. Its infrastructure moves to Cloudflare.

The thing nobody mentions is Deno KV. It isn’t in Deno’s post, Cloudflare’s post or the pinned issue. Hosted KV lives inside Deploy, so when Deploy goes, it goes too. There’s no export tool and no recommended replacement. That’s the part of your app with a real deadline, and it’s what most of this post is about.

The app

It’s a URL shortener, about 50 lines of code. POST /links claims a slug, GET /:slug redirects and counts a click, and GET /:slug/stats shows the count.

gh
$ gh repo view devtrillo/deno-deploy-to-workers --branch workers▋
devtrillo/deno-deploy-to-workersworkers

The shortener before and after: the deno tag is the Deno Deploy version, the workers tag runs on Cloudflare Workers with a Durable Object per slug. PR #1 has both port commits, including the broken one.

$ git clone -b workers https://github.com/devtrillo/deno-deploy-to-workers.git
View this repository on GitHub: https://github.com/devtrillo/deno-deploy-to-workers/tree/workers↗

The two interesting parts are the two writes. Creating a link has to fail if the slug is taken, even when two people ask for it at the same moment:

main.ts (Deno)
const res = await kv
.atomic()
.check({ key: ["links", slug], versionstamp: null })
.set(["links", slug], url)
.commit();
if (!res.ok) return Response.json({ error: "slug taken" }, { status: 409 });

versionstamp: null means “this key doesn’t exist yet.” If the check fails, nothing is written and ok comes back false. That’s an atomic insert-if-absent in four lines.

Counting a click is one line:

main.ts (Deno)
await kv.atomic().sum(["hits", slug], 1n).commit();

sum() adds to a Deno.KvU64 on the server. You never read the old value, so two clicks at the same moment can’t overwrite each other.

To check both, I wrote race.ts. It fires 20 requests that claim the same slug at once, then 50 clicks at once, and exits with an error unless exactly one claim wins and all 50 clicks count. Against deno task dev:

deno task race
claims: 1 of 20 accepted
clicks: 50 of 50 counted

That’s the baseline.

The port that looked right

Deploy and Workers have the same idea of a request handler, which makes the outside of the app nearly free. Deno.serve(handler) becomes a default export:

src/index.ts
export default {
fetch(req, env) {
// the same routing as before
},
} satisfies ExportedHandler<Env>;

The jsr: import needed one command. Wrangler bundles with npm-style resolution, and nothing in Cloudflare’s docs says it understands jsr: specifiers. JSR packages are also on npm, though:

Terminal window
npx jsr add @std/encoding

That writes an .npmrc for the @jsr scope and adds "@std/encoding": "npm:@jsr/std__encoding" to package.json. The import line in the code doesn’t change at all.

Then the storage. Cloudflare has a product literally called KV, it takes string keys, and the API is get and put. So I did the obvious thing:

src/index.ts (Workers KV)
// Workers KV has no atomic check. This is a read, then a write, with a gap.
if (await env.LINKS.get(`links:${slug}`)) {
return Response.json({ error: "slug taken" }, { status: 409 });
}
await env.LINKS.put(`links:${slug}`, url);
src/index.ts (Workers KV)
// No sum() either: read, add one, write back.
const hits = Number((await env.LINKS.get(`hits:${slug}`)) ?? 0);
await env.LINKS.put(`hits:${slug}`, String(hits + 1));

It typechecked. wrangler dev started. A single click redirected and the stats said 1. Then I ran the race:

bun race.ts, against wrangler dev
claims: 1 of 20 accepted
clicks: 4 of 50 counted

Why the clicks disappeared

Fifty requests each read hits. Most of them read the same number, because none of the writes had landed yet. Each one added one and wrote it back, so 50 writes of “4” still leave you with 4. Deno’s sum() never had this problem, because the addition happened inside the database.

Workers KV can’t do that addition for you. The docs say it directly: “KV is not ideal for applications where you need support for atomic operations.” There’s no check, no versionstamp and no increment. It also allows one write per second to the same key, which a click counter blows through on its first busy minute.

Now look at the claims line again. It says 1 of 20, which looks correct, and I don’t trust it. Locally, wrangler dev runs KV in one process, and a write is visible right away. In production, Workers KV is eventually consistent, and a write “may take up to 60 seconds or more to be visible in other global network locations.” Two people in two cities claiming the same slug within that window would both read “free” and both get a 201. The last write wins, and one of them has a short link that sends people to someone else’s URL.

That’s the worst kind of bug. The local test passes, and you only find it in production.

A Durable Object per slug

What atomic() gave me was one place where a slug’s reads and writes happen one at a time. On Cloudflare, that’s a Durable Object: “a single-threaded, globally-unique instance with its own persistent storage.” Give each slug its own object and every request about that slug goes through the same one.

src/index.ts
import { DurableObject } from "cloudflare:workers";
// One Link object per slug. Calls into it run one at a time, and the storage
// API is synchronous, so check-then-set has no gap for another request.
export class Link extends DurableObject {
create(url: string) {
if (this.ctx.storage.kv.get("url")) return false;
this.ctx.storage.kv.put("url", url);
return true;
}
hit() {
const url = this.ctx.storage.kv.get<string>("url");
if (url)
this.ctx.storage.kv.put(
"hits",
(this.ctx.storage.kv.get<number>("hits") ?? 0) + 1,
);
return url;
}
stats() {
const url = this.ctx.storage.kv.get<string>("url");
return url
? { url, hits: this.ctx.storage.kv.get<number>("hits") ?? 0 }
: undefined;
}
}

hit() is the same read-add-write as the broken version. The difference is that nothing else can run in the middle of it. ctx.storage.kv is the synchronous storage API on SQLite-backed objects, so there isn’t a single await between the read and the write. An object’s input gate blocks new requests “while synchronous JavaScript execution is in progress.”

I picked the sync API on purpose. Cloudflare’s rules page contradicts itself on whether awaiting the async ctx.storage.get() reopens the gate. One line says it does, and the page’s own counter example says it’s safe. With no await in the method, I don’t need to know which is right.

The Worker calls the object like a function. Its public methods are RPC calls:

src/index.ts
if (!(await env.LINK.getByName(slug).create(url))) {
return Response.json({ error: "slug taken" }, { status: 409 });
}

The config declares the binding and the class. Current docs use exports for this. If you’ve seen migrations with new_sqlite_classes in older tutorials, that’s the legacy form, and a Worker can use one or the other but not both:

wrangler.jsonc
{
"name": "deno-deploy-to-workers",
"main": "src/index.ts",
"compatibility_date": "2026-11-03",
"durable_objects": {
"bindings": [{ "name": "LINK", "class_name": "Link" }],
},
"exports": {
"Link": { "type": "durable-object", "storage": "sqlite" },
},
}

That works on wrangler 4.157.0. The same race against wrangler dev, run twice:

bun race.ts
claims: 1 of 20 accepted
clicks: 50 of 50 counted

I chose one object per slug on purpose. The docs warn that “a single Durable Object handling all traffic becomes a bottleneck”, so a global Links object would recreate the problem one level up. SQLite-backed objects are also the only kind on the Free plan, with 100,000 requests and 100,000 rows written a day. That’s plenty for a side project’s short links.

What I gave up: Deno KV let me list() every link with a prefix scan. With one object per slug, there’s no single place to ask “what links exist?” If you need that, add a D1 table as an index, write to it after create() succeeds, and keep the object as the source of truth for each slug.

Get your data out first

Deploy shutting down takes your KV data with it, and there’s no official export. What does exist is the KV URL connector. Deno’s docs show opening your production database from a local script with Deno.openKv("https://api.deno.com/v2/databases/<id>/connect") and a DENO_KV_ACCESS_TOKEN. Point this at it:

export.ts
const kv = await Deno.openKv(Deno.args[0]);
const rows = [];
for await (const { key, value } of kv.list({ prefix: [] })) {
rows.push({
key,
value: value instanceof Deno.KvU64 ? Number(value.value) : value,
});
}
await Deno.writeTextFile("kv-export.json", JSON.stringify(rows, null, 2));
console.log(`exported ${rows.length} entries`);

The KvU64 line matters, because JSON.stringify throws on a bigint. I tested this against a local KV file. I don’t have a real Deploy database left to try it on, so run it on a copy before you trust it. Importing into the Durable Objects is then a one-off script that calls create() and seeds hits for each slug.

If you’d rather not be on Cloudflare

Node and Bun take the same fetch(req) handler. On Bun it’s a default export, just like Workers:

server.ts (Bun)
export default { port: 8000, fetch: handler };

On Node, @hono/node-server wraps it. You don’t need the rest of Hono:

server.ts (Node)
import { serve } from "@hono/node-server";
serve({ fetch: handler, port: 8000 });

I ran both against a tiny handler on Bun 1.3 and Node 24. The routing moves over for free. The catch is the same one as on Workers: there’s no Deno KV on either, and plain SQLite needs the counter written as UPDATE ... SET hits = hits + 1 and the claim written as an INSERT that fails on a duplicate key. The database has to do the arithmetic. That’s the whole lesson, whatever runtime you choose.

What to do this week

  1. Export your KV data now with the URL connector, while Deploy still serves it.
  2. Switch your entry point to export default { fetch } and keep running on Deno. It’s a free step toward any target.
  3. Find every kv.atomic() in your code. Each check() and each sum() is a guarantee that Workers KV won’t give you. Those are the lines that need a Durable Object or a real database, not a get and a put.
  4. Run a concurrency test before you switch DNS. A single request will pass, and so will the claim race on wrangler dev. Fifty clicks at once is what showed the bug here.
  5. No hurry on the runtime itself. Deno 2.9 gets security fixes until October 2027. Deploy is what has the deadline.

The full port is one PR, and the broken Workers KV commit is in there on purpose so you can run the race against it yourself.

The routing was the easy part. What I nearly shipped broken was a guarantee I never had to think about while I was on Deploy.

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.