r/SpringBoot • • 11h 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.

3 Upvotes

5 comments sorted by

•

u/EirikurErnir 9h ago

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

•

u/bikesched 7h 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.

•

u/wimdeblauwe 7h ago

Seems more like Jobrunr to me.

•

u/bikesched 5h 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.

•

u/nian2326076 7h ago

bikesched addresses specific needs for background jobs in Spring Boot, especially those requiring durability and webhook handling. To gather feedback from production users, share your project on relevant developer forums or communities where experienced Spring Boot developers can offer insights. Additionally, you can use PracHub to practice interview questions that relate to Spring Boot and backend development, which can help enhance your skills as you work on bikesched.