By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Some work is too slow to do inside an HTTP request: sending a webhook, exporting a report, reprocessing a batch, rebuilding a cache. Queue-backed work decouples request latency from job duration, lets you retry failures without retrying the user, and gives backpressure a place to live. The request becomes 'accepted, job id X' instead of a long, fragile, timeout-prone handler.
The moment a queue exists, delivery semantics matter. Most queues are at-least-once: a job can be delivered twice if a worker crashes after doing the work but before acknowledging it. Make handlers idempotent — keyed by job ID, with a durable 'done' record — and the duplicate becomes a harmless no-op. At-most-once is cheaper but loses work; exactly-once is a property you build from idempotent consumers, not a button you enable.
On shutdown, stop accepting new jobs, let in-flight jobs finish, and only then exit. Relay's in-memory queue closes its job channel and waits for workers, which is the small version of the same lifecycle a broker consumer implements with visibility timeouts and acknowledgement. The failure mode to avoid is a process that exits mid-job and a queue that thinks the job is still running.
package main
import (
"fmt"
"slices"
"sync"
)
type job struct {
ID int
}
func main() {
jobs := make(chan job, 4)
results := make(chan string, 4)
var workers sync.WaitGroup
for worker := 0; worker < 2; worker++ {
workers.Add(1)
go func() {
defer workers.Done()
for j := range jobs {
results <- fmt.Sprintf("job %d done", j.ID)
}
}()
}
producerDone := make(chan struct{})
go func() {
defer close(producerDone)
for id := 1; id <= 4; id++ {
jobs <- job{ID: id}
}
close(jobs)
}()
go func() {
workers.Wait()
close(results)
}()
<-producerDone
collected := make([]string, 0, 4)
for line := range results {
collected = append(collected, line)
}
slices.Sort(collected)
for _, line := range collected {
fmt.Println(line)
}
fmt.Println("queue drained")
}| Queue choice | Good at | Watch out for |
|---|---|---|
| In-memory channel | Single process, simple work | Jobs are lost on restart |
| Database table | Durability without new infrastructure | Polling load and locking |
| Message broker | Scale, retries, dead letters | Operational overhead and semantics |
| Outbox table | Exactly-once side effects across services | A relay process to publish rows |
| Cron + queue | Periodic batches | Overlapping runs need a lock |
Key takeaways
Move slow or retryable work off the request path into a queue.
At-least-once delivery is the norm; make consumers idempotent by job ID.
Drain in-flight jobs during shutdown instead of exiting mid-job.
Choose in-memory, database-backed or broker queues by durability and scale needs.
Acknowledge only after the side effect is durable.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.