2 months ago
Hi,
I would like to setup a queue + worker services setup in my existing services on Railway. My traffic is very spiky so it means during the night I need zero worker, while during the day I might need 10 workers (32 vCPU). I've researched a lot online but didn't find a good way to handle this situation using railway's worker service. It looks like worker service is meant to be always-on services, so I would have 10 worker running but idle in the night, charging for processing nothing. So I'm wondering if there are some recommendations on this.
Thanks!
3 Replies
2 months ago
We don't currently have built-in autoscaling based on queue depth, but you can achieve this using our Public API. The serviceInstanceUpdate mutation accepts a numReplicas field, so you can build a lightweight autoscaler that monitors your queue length and adjusts replicas (including down to 0) programmatically - see the API Cookbook for the exact mutation. The Serverless feature won't help here since it wakes services on inbound traffic, which doesn't apply to workers that pull from a queue.
Status changed to Awaiting User Response Railway • about 2 months ago
2 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open Railway • about 2 months ago
2 months ago
Railway workers are meant to be long running services, so your concern is valid. A worker service is not the same as a cron style job that starts, finishes, and exits. Railway’s own guide describes background workers as always-on services, while cron jobs are the scheduled pattern that only run when triggered.
For your use case, the best pattern is usually:
- Put jobs into a real queue Use Redis plus BullMQ, Celery, Sidekiq, or similar. Railway explicitly recommends Redis-backed queues for this setup. Your API enqueues jobs and your workers consume them.
- Keep your worker tier separate from the API Railway supports scaling workers independently because they are separate services. Multiple worker replicas can all consume from the same Redis queue.
- Do not expect native queue based scale-to-zero from Railway workers Railway supports vertical scaling automatically, but horizontal replicas are something you increase manually in service settings. Their docs describe replica scaling as manual, not queue-latency driven autoscaling.
So the practical answer is:
- If you need true scale-to-zero overnight, use a scheduled pattern, not a permanently running worker fleet. Railway cron jobs are the native option for tasks that can run on a schedule and exit.
- If you need event-driven queue consumers that wake up exactly when jobs arrive and drop back to zero automatically, Railway does not appear to provide that natively for worker replicas today.
What I would recommend in practice:
- Keep 1 small worker if you need low-latency daytime and nighttime processing.
- Add more replicas manually during expected busy windows.
- If your spikes are predictable, use scheduled scale-ups before peak hours and scale-downs after peak hours.
- If you want real queue-length autoscaling, use an external autoscaler. Judoscale is one example that explicitly supports Railway and background worker autoscaling from queue latency.
So the short answer is: Railway can do queue + workers well, but native worker services are not really designed as zero-at-night, ten-during-the-day queue consumers. For that, either use cron for scheduled batch work, or add an external autoscaling layer if you want dynamic worker counts.
2 months ago
Hello
worker services are always-on by default and will consume resources even when idle, the only native feature that can help is serverless mode , it automatically sleeps your service after 10 minutes of no outbound traffic. however the catch is that if your worker keeps an active connection to redis/queue broker it will likely prevent it from sleeping, so it may not work cleanly for your use case
and i think railway does not currently have built-in autoscaling based on queue depth. horizontal scaling is manual only (you set replica count yourself in the settings)
so for my information there's no out-of-the-box solution for this on railway right now. your best bet is to either try serverless mode and test if your worker actually sleeps when the queue is empty
Hope this help you :)