DEV Community

Dominic Rockson
Dominic Rockson

Posted on

Background jobs without a queue server: Yatta JS jobs in one Bun process

Every solo project hits the same wall sooner or later: something needs to happen after the response is sent.

A welcome email. A thumbnail to generate. A webhook to retry. A nightly cleanup. The standard answer is a second system — Redis plus a queue library, or a hosted worker service, plus the dashboard, plus the bill. For a side project, that second system is often what kills the project. Not the code. The setup.

Yatta JS ships a job queue inside the same Bun process as your API. No Redis, no worker service, no extra deploy. One process, one SQLite file, and your jobs survive restarts.

This is yatta.js@1.0.6 — install it with npm i yatta.js. Docs at https://yatta.js.org. Code at https://github.com/psrockstar098/yatta.js.

Define a job, handle it, enqueue it

A job is a typed name and a handler:

import { Jobs } from "yatta.js/jobs";

// Tell TypeScript what each job carries.
declare module "yatta.js/jobs" {
  interface JobRegister {
    "send-email": { to: string; subject: string; body: string };
  }
}

Jobs.handle("send-email", async (ctx) => {
  const { to, subject, body } = ctx.data;
  await sendMail({ to, subject, body });
  ctx.log(`sent to ${to}`);
});

// Start a worker on the default queue.
Jobs.worker();
Enter fullscreen mode Exit fullscreen mode

Enqueue it from a route, right where the signup happens:

api.post("/signup", async (ctx) => {
  const user = await db.users.create(await ctx.json(signupSchema));
  await Jobs.enqueue("send-email", {
    to: user.email,
    subject: "Welcome to the app",
    body: `Hi ${user.name}, you're in.`,
  });
  return API.json(user, { status: 201 });
});
Enter fullscreen mode Exit fullscreen mode

The response goes back immediately. The email goes out in the background, in the same process.

Delayed, retried, and deduplicated

The fluent builder covers the cases that usually mean reaching for a bigger queue:

await Jobs.job("send-email")
  .with({ to: user.email, subject: "Your receipt", body: "..." })
  .delay("5m")            // run it in five minutes, not now
  .retry(3)               // three attempts, backoff with jitter
  .unique(`receipt:${orderId}`) // same order, one receipt, ever
  .onQueue("emails")      // its own queue, its own worker
  .save();
Enter fullscreen mode Exit fullscreen mode

If a handler throws, the job is retried with backoff and jitter. If it exhausts its retries, it lands in the dead-letter queue instead of vanishing — you can see it, inspect it, and requeue it. Workers take jobs under an atomic lease, so a crashed worker's job gets reclaimed instead of stuck. And because the queue is SQLite-backed, a restart doesn't eat the jobs that were waiting.

Worker behaviour is boring in the good way: the default worker runs 5 jobs at once, polls every second when idle, and holds a 60-second lease on each job. You can tune all of that per worker if you need to.

Cron, without a cron service

Recurring work is the same story. The scheduler speaks standard five-field cron, with timezones:

import { createCron } from "yatta.js/jobs";

const cron = createCron();

cron.schedule("daily-cleanup", "0 0 * * *", async () => {
  await db.sessions.deleteExpired();
}, { timezone: "Africa/Monrovia" });

cron.every("15m", async () => {
  await refreshLeaderboardCache();
});
Enter fullscreen mode Exit fullscreen mode

No cron service, no scheduled-lambda workaround. The schedule lives in your code, next to the job it runs.

What it is not

Honest limits, because they matter more than the features:

  • Jobs run in your process, on your machine. If your app needs workers spread across nodes, this is not that system — yet.
  • A long job holds a worker slot. The defaults (5 concurrent) are sized for background chores, not for a render farm.
  • The queue is durable, not magic. A job that always throws will always land in the dead-letter queue, where you have to look at it.

That tradeoff is the point. For the side project, the small SaaS, the internal tool — the thing where a second service is the difference between shipping and stalling — the queue that lives in your process is the queue you actually get.

The pattern, once more

Most backends I have built ended up as a constellation: API server, queue, worker dyno, scheduler, cache. Each one needed configuring, monitoring, and paying for. Yatta JS collapses the constellation back into one process: the API, the database, auth, mail, storage, realtime, and now the jobs and the cron that drive them.

npm i yatta.js — the jobs engine is yatta.js/jobs, and it works on a fresh project with nothing configured. What background work is your side project still doing inline in the request handler?

Top comments (0)