r/django • u/NebulaKaiz • 9d ago
Hosting and deployment when does celery actually need its own vm?
I'm trying to figure out when celery actually needs its own vm. Right now django postgres redis and two celery workers are on one 4 vcpu 8 gB box. Usually its fine but then someone uploads a csv with a million rows and the whole app slows down until celery finishes. First time it happened i almost resized the server but ten minutes later everything was normal again. I could move celery to another vps. Serverspace hetzner and others make that easy enough but then i have another server to maintain for a problem that happens a few times a week. Idk if i should wait until this becomes a daily problem or split it now before someone uploads an even worse file?
32
u/Free-Good9203 9d ago
Honestly, don't spin up a second VM yet for something that only happens a few times a week. Usually when a 1M-row CSV tanks a single box like that, it's either CPU starvation or Postgres I/O getting hammered by the worker, not that you've actually outgrown 4 vCPUs.
Before paying for and maintaining another server, I'd check three quick things first:
Once the CSV task is throttled and uses `COPY`, you'll probably be fine on one box until heavy jobs start overlapping daily.
4
u/scraping_solutions 9d ago
I'd hold off on the extra VM for now. Before splitting, check what actually saturates during an import (htop and iotop while it runs). If it's postgres taking a million inserts, moving celery to another VPS won't help much since the db stays on this box. Either way, put the CSV import on its own queue with a dedicated worker at --concurrency=1, so a huge file can only ever take one core, and keep your normal tasks on the other worker. Starting that worker with nice -n 10 also lets the web process win when they compete. And if the task inserts row by row, reading the file in chunks and using bulk_create with a batch_size of a few thousand cuts the load on postgres a lot. If imports still drag the app down after that, that's when a separate box starts to pay off.
1
u/Jejerm 9d ago
Is celery taking longer to finish a problem? If not, you could try limiting the the ram or cpu usage by process so celery doesnt slow down the entire server.
1
u/Colby-Jenkins 8d ago
If it's just a few times a week, maybe set a time window for heavy imports and scale down the resource allocation during that. You can also use `celery` task prioritization to manage how those tasks run, so they don’t hog everything.
1
u/FewWallaby2092 4d ago
for a spike that only hits a few times a week and clears up in about 10 mins, i wouldn't spin up another vps yet unless you actually want another box to maintain. usually a 1M row csv slows everything down becuase it's hogging ram or cpu, streaming it in small chunks( i think everyone does this) instead of loading all of it at once can solve things right there. You can also cap worker concurrency, so workers can recycle memory, and put resource limits on celery so it doesn't choke postgres or the web app. move celery to it's own vm once background work is constant, needs differnt hardware, or still hurts traffic after tuning. talking from experience most of the times the issue is with the code, processing and stuffs. I worked in the hotel industry before where i handled things like that managing and maintaining inventory about syncing about 5-7Million hotel and activities records and updating them.
1
u/quiet_stacktrace 2d ago
one more small thing that helped me: set worker_prefetch_multiplier to 1 on the worker that handles the big imports. otherwise it grabs a bunch of queued tasks ahead of time and they sit behind the long csv job even though another worker is idle. cheap to try before paying for another box.
1
u/Forward-Outside-9911 9d ago
Lmao the comments here just prove dead internet theory.
Whats even the point of trying human-human communication anymore