php-fpm pm.max_children: is anyone actually calculating this or just copying blog posts?

maria_lopez

New Member
Messages
66
Reaction score
0
Location
Miami, FL

Spent most of yesterday tuning php-fpm on a box that kept throwing 502s under load. Turns out someone had set pm.max_children = 40 "because that's what the tutorial said". Box has 8GB RAM and each worker was sitting at ~85MB RSS after a few hours of opcache warmup. Do the math and 40 workers eats 3.4GB just in PHP, before mysql, redis, nginx, and the actual app buffers. No wonder it was OOM-killing nginx on the regular.

So I dropped it to 22, set pm = dynamic, start_servers 6, min_spare 4, max_spare 10, max_requests 500. Load average went from 14 to 3.5. Same traffic. Nobody wrote a single line of app code. The whole "fix" was just not oversubscribing the box.

What bugs me is every guide online gives you the formula (total RAM / worker memory) and then everyone ignores it and hardcodes 40 or 50. I've seen this in three different jobs now. Half the time the RSS number people feed into the formula is measured right after startup when it's still 30MB, not after the app has been running for 8 hours with all the autoloaded classes in memory. Measure it at steady state, not at minute one.

Anyway what are you all actually running? pm = static or dynamic? And does anyone here bother with pm.max_requests anymore or do you just let workers bleed memory until deploy? I've been running 500 forever but I keep hearing arguments both ways.

greg_ohio

New Member
Messages
71
Reaction score
0
Location
Columbus, OH

bhai this is almost exactly what happened to my Contabo VPS lol. I run a small points tracker for my rummy group and it kept 502ing during peak hours (9-11pm when everyone is playing). Never touched pm.max_children, it was sitting at 25 on a 4GB box because I copy pasted some guide. Dropped it to 10 and the errors just stopped. Same traffic, didn't change any code. Wild.

Question though, how do you actually measure the steady state RSS? I've only ever stared at htop and the number jumps around every refresh. Is there a command that gives the real figure or do you just watch it for hours?

And pm.max_requests 500, I've been too scared to touch that one. Read somewhere that setting it low tanks performance. Myth or real?

rk_designs

New Member
Messages
68
Reaction score
0
Location
Vancouver, BC

greg_ohio the htop jumping is normal, RSS wobbles every refresh because workers allocate/free constantly. The trick is average across all workers and check hours into uptime, not right after a restart. This one-liner has bailed me out more times than I can count (half my clients are offshore books and crypto casinos on cheap Contabo-tier boxes, I get OOM pages at

jt_loves_tacos

New Member
Messages
75
Reaction score
0
Location
Phoenix, AZ

greg_ohio for the RSS question, stop staring at htop. Enable the fpm status page (pm.status_path = /status in your pool conf, lock it to localhost in nginx) and curl it. It hands you the average RSS per worker across the whole pool instead of whatever your terminal happened to catch mid-refresh. Eyeballing htop for an hour is a waste of an afternoon.

max_requests 500 is not tanking anything. The "performance hit" people repeat is just the cost of respawning a worker, a few ms of opcache warmup, once per 500 requests per worker. That's noise. What actually wrecks you is leaving it at 0 and letting one leaked worker sit at 300MB for a month. Been on 500 forever, never seen a real benchmark where it mattered.

That said I'll push back on the "just do the math" framing. The formula is a starting point, not gospel. I've got boxes where the math says 30 workers but 18 runs cleaner because the traffic is bursty and the extra workers just fight the scheduler for CPU. Worker count is only half of it, you still gotta watch what's actually queueing.

And if half your requests are basically static, you're tuning the wrong knob. Spend an afternoon on a CDN instead, there's a whole section for that here

https://websiteforums.info/forums/cdn-and-performance.4/

tommy2k

New Member
Messages
69
Reaction score
0
Location
Mexico City, MX

the shared memory bit is what nobody mentions when they repeat the formula. opcache sits in a shared segment, so summing RSS across 22 workers counts that same chunk 22 times. so "total RAM / worker RSS" overshoots PHP's real footprint. but it doesn't save you, because the same folks measured at minute one when it was 30MB. one error up, one error down, and you still land at 40 workers somehow.

max_requests, jt nailed it, 500 costs you nothing worth measuring. the real question isn't perf though, it's whether you trust the code. clean app that frees its refs, run 0 forever. legacy thing holding a global cache that only grows, 500 is your seatbelt. I've run both. the seatbelt saved me at 3am once, and the 0 config has been up two years without a hiccup.

greg if you want the steady state number just enable pm.status_path and check it after a few hours uptime, not after a restart. htop lies.

LisaW_

New Member
Messages
53
Reaction score
0
Location
Edinburgh, UK

ok this thread is making me realize how much I've been winging it. I've got a little WP site on a 2GB droplet and I set pm.max_children to 25 because some blog said so, never once thought about what each worker actually weighs after a day of running.

The shared memory thing tommy2k brought up broke my brain a little. So opcache sits in one shared segment, meaning summing RSS across all workers double counts it? Does that mean the formula is overcounting AND undercounting depending on when you measure? I can't tell if that makes me safer or more doomed lol.

Also dumb question, can I enable pm.status_path without restarting php-fpm on a live site? Kinda scared to touch the pool conf while people are actually on it.

jen_93

Member
Messages
62
Reaction score
0
Location
Cape Town, ZA

LisaW_ a reload won't drop anything. systemctl reload php-fpm (or kill -USR2 to the master) makes the master re-read the pool confs and gracefully respawn workers as they finish in-flight requests. No dropped connections, nobody on the site notices. A restart is what you avoid. Reload is fine. So enable the status path, reload, curl it after a few hours uptime. That's the whole trick.

On the overcount/undercount thing, yeah, both, and that's the part that drives me nuts. Shared opcache gets counted in every worker's RSS, so summing across 22 workers counts that segment 22 times. Overcount. But measuring at minute one when the worker is 30MB instead of 85MB at steady state is an undercount. They sort of cancel out, which is exactly why the formula "works" for people who did everything wrong. Lucky, not correct. tommy2k's right that it doesn't save you, because the two errors are unrelated in size and you've got no idea which way they lean on your box.

I'll push back on the "trust the code, run max_requests 0" idea though. I got bitten by that. App was clean, we'd audited it, held no globals, all the good intentions. Then a third-party SDK we pulled in for payments started caching responses in a static array keyed by something that never repeated. Leaked about 4MB an hour per worker. Took three weeks to notice because the box had headroom. max_requests 500 is a few lines of insurance that would've hidden it completely. I'd rather respawn a worker every 500 requests than wake up to an OOM page at 2am. The "performance hit" is a rounding error, jt already covered it.

FWIW I run dynamic on everything now, max_children sized off steady state RSS after subtracting mysql/redis/nginx, and 500 on max_requests unless I've got a real reason not to. Half the horror stories in this thread come from people squeezing a cheap box that never had the RAM to begin with, which is its own conversation. There's some decent stuff in hosting discussions about picking boxes that actually have room to spare.

FrostyBear

New Member
Messages
67
Reaction score
0
Location
Brisbane, AU

jen_93 that SDK thing is exactly what scares me about the "our code is clean" argument. You audit your own stuff, feel great, then some payment library you added for one feature is quietly hoarding cache and you never see it until the box falls over.

Did you ever find out how fast it was growing? Like was it megabytes a day or a slow bleed you only caught after weeks? Curious because I'd have no idea where to even start looking.

Also LisaW_ don't feel bad, I set mine to 25 on a 2GB box too because a blog told me to. Found this thread and now I'm scared to look at what my workers actually weigh lol.

wafflesRgood

New Member
Messages
57
Reaction score
0
Location
Berlin, DE

I run a tiny forum for fish keepers (like 300 users, mostly people panicking about ich treatments) and I've been on pm = dynamic with max_requests 0 for like two years because I read one blog post and never touched it again. Reading this thread is making me sweat ngl. tommy2k saying "the real question is whether you trust the code" and then jen_93 and FrostyBear immediately going "yeah well a third party library will betray you" is basically my whole fear in one exchange.

My app is WordPress with maybe 9 plugins and I have absolutely no idea what any of them are holding in memory. So I'm now sitting here wondering if one of them has been slowly eating RAM for a year and I just haven't noticed because the box is oversized. Feels like the server equivalent of a fish looking fine until it's suddenly floating sideways.

Question for the max_requests 500 crowd though. If a worker respawns every 500 requests, does that mean the opcache in that worker is cold for a bit and the next few requests are slower? jt_loves_tacos said it's a few ms and noise, but is it noise when the traffic is bursty like mine? Like if 3 people hit the site at once right after a respawn, do they eat that warmup hit or does opcache stay warm across the pool somehow? I genuinely don't understand if opcache is per worker or shared.

Also FrostyBear I feel you on being scared to look. I've decided I'm not checking mine until Monday.

greg_ohio

New Member
Messages
71
Reaction score
0
Location
Columbus, OH

Thanks for the pm.status_path tip jt, finally set it up yesterday on my Contabo box and curled it after like 6 hours uptime. Average worker RSS was 71MB, not the 30MB I was assuming from staring at htop. So my 10 workers are eating ~700MB just in PHP on a 4GB box. No wonder the 25 that was there before was dying lol. tommy2k the "htop lies" line is now permanently in my head.

wafflesRgood your fish tank comparison killed me, floating sideways is exactly what it feels like 😅 but honestly if 3 people hit your site right after a respawn they won't feel anything. That warmup is milliseconds. You'll spend more time worrying about it than your users ever spend waiting.

Since the WordPress army is showing up in this thread, what's everyone running for pm.max_requests on WP specifically? My rummy tracker is a tiny custom thing so 500 feels safe, but wafflesRgood got me thinking about 9 plugins and what they're hoarding. Anyone here run 0 on a plugin-heavy site and lived to tell it?

mike_t_2019

Member
Messages
60
Reaction score
0
Location
Leeds, UK

rk_designs nailed it with the offshore thing, half my boxes are exactly that. Contabo-tier VPS running Crash and Plinko front-ends for a couple of sites that shall not be named. The regulated Ontario stuff can't even offer half these games so nobody's tuning those boxes, they're just charging you 40% margin for a slot machine with a license sticker on it. Anyway.

On greg's WP question: run 0 on a 9-plugin WordPress and you're not trusting your code, you're trusting nine strangers who last updated their plugin in 2021. I watched a single payment gateway SDK hold onto session data and climb about 4MB an hour. Nobody notices for weeks because the box has headroom, then a promo hits, everyone's hammering the site at once, and you're OOM-killing nginx at 2am wondering what changed. Nothing changed. It was always bleeding. 500 is fine, I actually run 300 on the casino boxes because webhook handlers leak worse and I'd rather eat the respawn than babysit a graph at 3am.

Something nobody's mentioned yet though, and it cost me a bad weekend: max_requests only saves you if the leak is per-worker. If it's sitting in shared memory or a persistent redis pool, respawning workers does nothing and you're just rotating deck chairs on the Titanic. You'll see RSS flatline across the whole pool while the box still fills up. That's the tell.

Anyone here running opcache.file_cache to survive the warmup after a respawn? Been meaning to try it but I keep putting it off.

nightowl87

New Member
Messages
60
Reaction score
0
Location
Portland, OR

greg_ohio's update is what got me, 71MB at steady state vs the 30MB I'd been assuming. I set up pm.status_path on my little Laravel box yesterday and let it sit overnight before curling it. Average worker was 68MB. I'd been sizing everything off "eh, like 40MB each" because that's what it showed five minutes after a deploy.

So basically I've been guessing wrong in the same direction as everyone else in this thread lol.

mike_t_2019 the "only saves you if the leak is per-worker" bit is what's bugging me now. How do you actually tell the difference? Like if the box keeps climbing even after workers cycle, is that the shared/persistent pool thing, or is it just mysql buffers and I'm chasing the wrong process entirely?

You must log in or register to reply here.