|
| 1 | +--- |
| 2 | +name: render-background-workers |
| 3 | +description: >- |
| 4 | + Sets up and configures background workers on Render for queue-based job |
| 5 | + processing. Use when the user needs to process async jobs, consume from a |
| 6 | + queue, run Celery/Sidekiq/BullMQ/Asynq/Oban workers, handle graceful |
| 7 | + shutdown with SIGTERM, wire a worker to Key Value (Redis), or choose between |
| 8 | + workers and cron jobs for background work. |
| 9 | + Trigger terms: background worker, async jobs, queue consumer, Celery, |
| 10 | + Sidekiq, BullMQ, Asynq, Oban, job processing, SIGTERM, graceful shutdown. |
| 11 | +license: MIT |
| 12 | +compatibility: Render background worker services |
| 13 | +metadata: |
| 14 | + author: Render |
| 15 | + version: "1.0.0" |
| 16 | + category: compute |
| 17 | +--- |
| 18 | + |
| 19 | +# Render Background Workers |
| 20 | + |
| 21 | +This skill explains **worker** services on Render: processes that **consume jobs from a queue** instead of serving HTTP. Pair with **render-blueprints**, **render-env-vars**, and **render-networking** when wiring `render.yaml` and private connectivity. |
| 22 | + |
| 23 | +## When to Use |
| 24 | + |
| 25 | +- Designing or debugging **queue-backed workers** (Celery, Sidekiq, BullMQ, Asynq, etc.) |
| 26 | +- Choosing between a **worker**, **Cron Job**, or **Workflow** for background work |
| 27 | +- Configuring **Render Key Value** as a **broker** (not a cache) with correct **eviction policy** |
| 28 | +- Implementing **graceful shutdown** so in-flight jobs are not lost on deploy |
| 29 | + |
| 30 | +Per-framework setup and signal-handling detail: `references/queue-framework-setup.md`, `references/graceful-shutdown.md`. |
| 31 | + |
| 32 | +## How Workers Work |
| 33 | + |
| 34 | +- **Long-running services** with **no inbound (HTTP) traffic**. Render does not expose a public URL or internal hostname for workers the way it does for web or private services—**workers cannot receive private network traffic directed at them**. |
| 35 | +- The typical pattern is a **poll loop**: the process connects to a **queue backend** (often **Render Key Value**, Redis-compatible **Valkey 8**) and **pulls jobs**. |
| 36 | +- Workers **can initiate outbound connections** on the private network—to **PostgreSQL**, **Key Value**, **private services**, **web services** (internal URLs), and the public internet—subject to your plan and firewall rules. |
| 37 | + |
| 38 | +## Queue Framework Overview |
| 39 | + |
| 40 | +| Framework | Language | Queue backend | Notes | |
| 41 | +|-----------|----------|---------------|--------| |
| 42 | +| Celery | Python | Redis / Key Value | Most common Python task queue | |
| 43 | +| Sidekiq | Ruby | Redis / Key Value | Standard for Rails | |
| 44 | +| BullMQ | Node.js | Redis / Key Value | Modern Node queue (Redis-based) | |
| 45 | +| Asynq | Go | Redis / Key Value | Go async task processing | |
| 46 | +| Oban | Elixir | **Postgres** (not Redis) | Queue stored in the database | |
| 47 | + |
| 48 | +## Pairing with Key Value |
| 49 | + |
| 50 | +- Use **Render Key Value** as the **job broker** when your framework expects Redis. |
| 51 | +- Set **maxmemory policy** to **`noeviction`**. **`allkeys-lru`** and similar policies are for **caches**; evicting queue keys **drops jobs**. |
| 52 | +- Wire **`REDIS_URL`** (or your framework’s equivalent) via **`fromService`** with `type: keyvalue` and `property: connectionString` in the Blueprint. |
| 53 | +- **Blueprints require `ipAllowList`** on Key Value—include the CIDRs that should reach the instance (often `[]` for private-network-only access; see **render-blueprints** / Key Value field reference). |
| 54 | + |
| 55 | +See `references/queue-framework-setup.md` for minimal app + YAML examples. |
| 56 | + |
| 57 | +## Worker vs Cron vs Workflow |
| 58 | + |
| 59 | +| Need | Use | Why | |
| 60 | +|------|-----|-----| |
| 61 | +| Always-on queue consumer | **Background Worker** | Polls continuously; long-lived process | |
| 62 | +| Periodic scheduled task | **Cron Job** | Runs on a schedule, **exits**; **12h max** per run | |
| 63 | +| Distributed parallel compute | **Workflow** | Each run gets its own instance; fan-out patterns | |
| 64 | +| High-volume or bursty jobs | **Workflow** | Scales per run; **no idle instance cost** between runs | |
| 65 | + |
| 66 | +## Graceful Shutdown |
| 67 | + |
| 68 | +- Before stopping an instance, Render sends **`SIGTERM`**, then waits up to **`maxShutdownDelaySeconds`** (**1–300**, **default 30**) before **`SIGKILL`**. |
| 69 | +- Workers should: **(1)** stop accepting new jobs, **(2)** finish the current job or **checkpoint** progress, **(3)** close connections, **(4)** exit **0**. |
| 70 | +- Set **`maxShutdownDelaySeconds`** to at least your **longest safe job duration** (see Dashboard or Blueprint). |
| 71 | + |
| 72 | +Language- and framework-specific handlers: `references/graceful-shutdown.md`. |
| 73 | + |
| 74 | +## Blueprint Configuration |
| 75 | + |
| 76 | +Minimal pattern: **`type: worker`**, **`runtime`**, **`buildCommand`**, **`startCommand`**, and **`envVars`** wired from Key Value. |
| 77 | + |
| 78 | +```yaml |
| 79 | +services: |
| 80 | + - type: keyvalue |
| 81 | + name: jobs |
| 82 | + plan: starter |
| 83 | + region: oregon |
| 84 | + ipAllowList: [] |
| 85 | + |
| 86 | + - type: worker |
| 87 | + name: task-worker |
| 88 | + runtime: python |
| 89 | + region: oregon |
| 90 | + plan: starter |
| 91 | + buildCommand: pip install -r requirements.txt |
| 92 | + startCommand: celery -A tasks worker --loglevel=info |
| 93 | + envVars: |
| 94 | + - key: REDIS_URL |
| 95 | + fromService: |
| 96 | + name: jobs |
| 97 | + type: keyvalue |
| 98 | + property: connectionString |
| 99 | +``` |
| 100 | +
|
| 101 | +Optional: **`maxShutdownDelaySeconds`** on the worker service for longer draining jobs. |
| 102 | + |
| 103 | +## References |
| 104 | + |
| 105 | +| Topic | File | |
| 106 | +|--------|------| |
| 107 | +| Celery, Sidekiq, BullMQ, Asynq, Oban setup + YAML | `references/queue-framework-setup.md` | |
| 108 | +| SIGTERM, `maxShutdownDelaySeconds`, per-language patterns | `references/graceful-shutdown.md` | |
| 109 | + |
| 110 | +## Related Skills |
| 111 | + |
| 112 | +- **render-deploy** — First deploy, CLI, service creation |
| 113 | +- **render-blueprints** — Full `render.yaml` schema, `fromService`, projects |
| 114 | +- **render-networking** — Private URLs, what can call what |
| 115 | +- **render-scaling** — Worker plans, instance counts, limits |
0 commit comments