A bounded queue with four jobs between producer and consumer nodes

Overview

Queue Relay is a fictional service for background work. A client submits a task with a stable request key, polls its status, and can inspect the reason for a terminal failure. The case study focuses on what can happen between accepting a job and acknowledging it.

Problem and objectives

Running a task inside a request ties application latency to the duration of the task. Moving work to a queue changes that latency, but a crash between a side effect and an acknowledgement can cause the queue to deliver the same task twice.

The design needs to acknowledge acceptance only after durable storage, bound the number of retries, and make terminal failures inspectable.

Architecture

A Spring Boot API owns validation and job creation. A PostgreSQL jobs table stores the payload, state, attempt count, and time of the next attempt. Workers claim eligible rows in a short transaction and process outside that transaction. A claim lease permits recovery from worker shutdowns.

Client → HTTP API → durable job table → worker
                         ↑               |
                         └──── status ────┘

Implementation and trade-offs

A unique constraint on the request key makes repeated requests converge on the same job. The worker uses a lease owner and a lease version when it records completion, so a late worker cannot overwrite a newer claim. Retries use bounded backoff with jitter. Invalid payloads move straight to a terminal state.

The lease protects the job record. Work that crosses a service boundary also needs an idempotency key that the destination recognizes. A destination with neither idempotency nor a way to query the outcome leaves an ambiguous failure that requires reconciliation.

Demonstration outcomes

The example defines checks for concurrent enqueue, worker shutdown after a claim, temporary downstream failures, and duplicate completion. In this design, error state and retry history are part of the client contract.

Lessons learned

Specify delivery and side-effect guarantees separately. A durable queue can support retryable delivery, while the destination determines whether the effect itself can be safely repeated.

Back to projects