r/SpringBoot • • 5d ago

News I built a Spring Boot 4 starter for durable, PostgreSQL-backed background jobs (Apache 2.0, feedback welcome)

I needed background jobs in a Spring Boot app that survive restarts, retry with backoff and can wait for things like a payment webhook, without adding a broker or a separate service. I didn't find a library that fit, so I built bikesched. It's an early 0.x release and I'd like feedback from people who run Spring Boot in production.

With the starter and a DataSource, there's nothing to wire: no @Enable…, no builder code.

@Component
class OrderJobs {

  @JobHandler(name = "fulfil-order")
  public void fulfil(Job job, Order order) {
    // runs once; a retried or resumed job gets the stored id back
    String paymentId = job.step("create-payment", String.class,
        step -> payments.create(order).id());

    // the job suspends here without holding a thread until the webhook publishes the event
    Payment payment = job.waitForEvent("await-payment", "payment-received", paymentId,
        Payment.class).receive();

    job.step("ship", () -> shipping.ship(order, payment));
  }

  @JobHandler(name = "purge-expired-sessions", schedule = "0 0 */6 * * *")
  public void purgeExpiredSessions() { … }
}

What the starter does:

  • Finds @JobHandler methods on your beans, including @Transactional proxies, and calls them through the proxy.
  • schedule takes Spring's six-field cron syntax or an ISO-8601 duration, so it can replace @Scheduled and runs once across all instances instead of on every one.
  • Schedules jobs inside your @Transactional method, so the job only exists if your transaction commits (outbox style).
  • Starts polling with the context and on shutdown releases unfinished jobs before the DataSource closes, so another instance takes over.
  • Uses Boot's Jackson ObjectMapper for payloads and records Micrometer metrics when those modules are on the classpath. Every option is a bikesched.* property.

Under the hood:

  • Jobs are rows in plain PostgreSQL tables. Instances claim them with SKIP LOCKED and wake each other with LISTEN/NOTIFY.
  • Durable steps whose results survive retries, compensation for sagas, timeouts, idempotency keys, child jobs and sleeping for days without holding a thread.
  • Crash recovery: jobs of a dead instance are picked up by another one.
  • Java 21+, virtual threads. The core depends only on the PostgreSQL JDBC driver and SLF4J. There's also an optional admin UI and a test module that can skip time.

It's Apache 2.0, with no open-core or paid tier.

What I'd most like to hear: does the @JobHandler model feel natural next to @Scheduled and @Transactional, and what would stop you from using it?

Full disclosure: I'm the author, and it's also an experiment in building a library heavily with AI coding agents.

11 Upvotes

7 comments sorted by

2

u/EirikurErnir 5d ago

This makes me think of ShedLock, what problems does this solve that that does not? :)

1

u/bikesched 5d ago

Good question! ShedLock just makes sure a \@Scheduled`` task runs on one instance at a time. That's it, and it's great at that.

bikesched can do that too, but it's more of a job queue in Postgres. You can kick off jobs from your code ("send this email in 10 min"), they get retried if they fail, and if an instance dies another one picks them up.

On top of that it does durable execution, kind of like a lightweight Temporal inside your app. A job can have multiple steps, like "charge the card, then ship, then send the confirmation". Each step's result is saved, so after a crash or retry the job picks up where it left off instead of charging the card twice. Jobs can also wait for events, like "wait until the payment webhook for order 123 comes in", even for days, without blocking a thread.

So if you only need cron tasks to not run twice, ShedLock is the simpler choice. If you need background jobs or workflows that survive crashes, bikesched is for that.

3

u/wimdeblauwe 5d ago

Seems more like Jobrunr to me.

1

u/bikesched 5d ago

Fair, it's much closer to JobRunr than to ShedLock. The main differences: bikesched is Postgres only and fully Apache 2.0. Transactional scheduling, instant job start, timeouts and waiting for events are all free, while in JobRunr most of those are Pro features. It also has compensation to undo earlier steps when a later one fails.

1

u/bikesched 16h ago

0.2.0 adds job batches: a job starts many child jobs as one durable step and waits for all of them. The items are regular jobs, so every instance of your app picks them up, not just the one that started the batch.

@Component
class NewsletterJobs {
 
  @JobHandler(name = "send-mail")
  public void send(Mail mail) {
    mailClient.send(mail);
  }
 
  @JobHandler(name = "newsletter")
  public void newsletter(Job job, String subject) {
    // one child job per reader, picked up by any instance, at most 10 at a time
    JobBatch<Object> mails = job.newJobBatch("mails", "send-mail")
        .addAll(subscribers.emails(), email -> email, email -> new Mail(email, subject))
        .parallelism(10)
        .schedule();
 
    // waits without holding a thread; throws if a mail failed for good
    mails.await().orElseThrow();
  }
}

If the newsletter job is retried, it gets the stored batch back instead of starting the mails again. failFast() cancels the rest at the first failure, and items can return typed results.

Feedback wanted: still 0.x, so the API can change easily. If something feels off, tell me.

2

u/sereikis 4d ago

add a crash test immediately after payments.create succeeds but before job.step stores its result. replay that step with a stable payment-provider idempotency key derived from the job and step; a stored return value alone cannot prevent a second charge across that gap.