|
| 1 | +--- |
| 2 | +title: Asynq |
| 3 | +keywords: [asynq, redis, queue, worker, background jobs, tasks] |
| 4 | +description: Enqueue background jobs from Fiber and process them with an Asynq worker. |
| 5 | +--- |
| 6 | + |
| 7 | +# Fiber and Asynq example |
| 8 | + |
| 9 | +[](https://github.com/gofiber/recipes/tree/master/asynq) [](https://stackblitz.com/github/gofiber/recipes/tree/master/asynq) |
| 10 | + |
| 11 | +## Description |
| 12 | + |
| 13 | +This example shows how to run background jobs with [Asynq](https://github.com/hibiken/asynq) (a Redis-backed task queue) from a [Fiber](https://github.com/gofiber/fiber) HTTP server. The API enqueues a job and returns immediately; a separate worker process consumes the queue and does the slow work, with retries and priorities handled by Asynq. |
| 14 | + |
| 15 | +## How it works |
| 16 | + |
| 17 | +- The Fiber API exposes `POST /enqueue` with a JSON body: `{"user_id": "...", "email": "..."}`. |
| 18 | +- Each request enqueues a `email:welcome` task onto Redis and returns the task id — it never does the work inline. |
| 19 | +- A separate **worker** process pulls tasks from Redis and runs them, retrying on failure with backoff. |
| 20 | +- The task type and payload live in a shared `task` package, so the API and the worker can't drift apart. |
| 21 | + |
| 22 | +The worker uses weighted queues (`critical` drained ~6x as often as `low`), which is the usual way to keep a noisy low-priority job from starving important ones. |
| 23 | + |
| 24 | +## Requirements |
| 25 | + |
| 26 | +- [Go](https://golang.org/dl/) 1.25 or higher |
| 27 | +- A running Redis instance (or use the provided `docker-compose.yml`) |
| 28 | + |
| 29 | +## Running the example |
| 30 | + |
| 31 | +### With Docker Compose |
| 32 | + |
| 33 | +```bash |
| 34 | +docker compose up --build |
| 35 | +``` |
| 36 | + |
| 37 | +This starts Redis, the API, and the worker together. |
| 38 | + |
| 39 | +### Manually |
| 40 | + |
| 41 | +Start Redis, then in two terminals: |
| 42 | + |
| 43 | +```bash |
| 44 | +# terminal 1 — API |
| 45 | +make run-api |
| 46 | + |
| 47 | +# terminal 2 — worker |
| 48 | +make run-worker |
| 49 | +``` |
| 50 | + |
| 51 | +Set `REDIS_ADDR` if Redis isn't on `localhost:6379`. |
| 52 | + |
| 53 | +## Trying it out |
| 54 | + |
| 55 | +```bash |
| 56 | +curl -X POST http://localhost:3000/enqueue \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d '{"user_id":"42","email":"jane@example.com"}' |
| 59 | +# {"enqueued":true,"task_id":"...","queue":"default"} |
| 60 | +``` |
| 61 | + |
| 62 | +The worker terminal logs: |
| 63 | + |
| 64 | +```text |
| 65 | +sending welcome email for user 42 |
| 66 | +``` |
| 67 | + |
| 68 | +A request with a missing or invalid body returns `400`. |
| 69 | + |
| 70 | +## Notes |
| 71 | + |
| 72 | +- The handler returns an error to signal a retry; returning `asynq.SkipRetry` (as it does for a malformed payload) tells Asynq not to bother retrying something that will never succeed. |
| 73 | +- Enqueue options like `asynq.MaxRetry` and `asynq.Queue` are set per call, so different endpoints can enqueue onto different queues with different retry policies. |
0 commit comments