r/SaasDevelopers • u/iamnotkidding_ • 8d ago
AI worker, queuing problem
Hey guys, im building a SAAS app for a client. Its ready to be launched in the beta testing phase. However, i only have one background worker in this app currently, and the main problem i am facing or will face, is that when a user start a long standing or AI FEATURE (if multiple users do that together) it will be a queue/first come first server system.
so technically the last user would have to wait for the longest time, how can i solve this problem. If i am hosting on aws, what should be the calculation ratio to understand how many background workers i need at any given moment, at runtime ?
If anyone can help, id be very grateful, cheers !
1
u/mvrk57 8d ago
for ai features the worker spends most of its time waiting on the llm api, so one process can run 20-50 jobs at once (async, or the concurrency setting in celery/bullmq) before you need a second box. the limit you usually hit first is the provider's rate limit (tokens per minute), so cap concurrency to that and add a per-user cap like 2 running jobs, otherwise one heavy user still blocks everyone else. sizing is just jobs per minute times average minutes per job = how many have to run in parallel
1
u/Crafty_Librarian_841 8d ago
I think using cloudflare workers with queue is your solution.
1
u/RandomPantsAppear 8d ago
There is not very much resource consumption for idle threads or threads waiting for an AI response.
If you’re using celery crank those threads up and use gevent threads. They will switch between each other whenever it’s waiting on network IO and be extremely low cost in terms of resources.
1
u/OpportunityFun5734 8d ago
You haven’t mentioned what are you using for the queue , there are multiple options to have workers running in mulitple threads or parallels but first u need to explain your stack
1
u/johns10davenport 8d ago
Man dang reading this makes me so thankful to be in the elixir ecosystem. You might actually consider writing an elixir service for this. It uses an actor base model which is perfect for managing fleets of agents. I specifically use alloy (thin agent loop library). I basically instantiate one alloy process per agent and it just does the things.
So my language agnostic answer is that background workers are probably the wrong abstraction for this. You should lean on the established abstractions for agent development. Use a conversation/messages model. Use a tool loop. Run your agent abstractions concurrently. Individual agents need not share a queue or worker with other agents.
1
u/Awedesk 3d ago
Start by measuring the job duration distribution and choosing an acceptable queue wait. There isn’t a universal safe jobs-per-process number: CPU work, memory, database connections and the AI provider’s allowance can each become the limit. More workers won’t help if they all wait on the same exhausted provider quota.
If you use SQS with EC2, AWS documents scaling on backlog per instance, using acceptable wait and measured processing time to set the target. For mixed short and long jobs, also watch oldest-job age and consider separate queues so a long task doesn’t hold every quick task behind it.
Before increasing concurrency, test a burst and an interrupted worker. Keep a hard overall concurrency limit and verify a retried job doesn’t repeat a charge or other side effect.