Score:0

PHP 7.4 under nginx with php-fpm - single shared APCu cache - how?

cn flag
C C

I have a PHP 7.4 set of scripts running with APCu extension, which is used by the code as a general object store for all users who visit the single hosted website. Due to my own lack of understanding, I later found out that the APCu cache is established per PHP process. So what I am seeing is that in some cases user 'a' is getting a different cached value than user 'b' (presumably since PHP-FPM has forked a new process for user 'b').

But it's still not clear to me exactly how to set up php-fpm so that all incoming requests will be inside of child processes that are derived from the same parent process.

APCu guru krakjoe said this back in 2015 (my emphasis in bold):

Just to answer the original question ... as @fruitl00p said, you can configure your way out of the problem of not being able to share if you are using fpm.

There is a difference between using fpm and using fcgi, I should probably put in a PR to phptherightway to clear it up.

The rule is that only child processes can access what their parent created; In FCGI spawned processes are not necessarily a child of their parent, they may not be actual forks. If your process manager works like conventional FCGI/CGI then you will not be able to share, if it works like FPM, and initializes PHP in a parent and forks child interpreters then you will have no problem.

Further research led me to php-fpm www.conf file and the settings for:

pm = dynamic
pm.max_children = 50

So - is it as simple as changing this to:

pm = static
pm.max_children = 50

i.e. is this the way to force php-fpm to make sure all child processes point back to a single parent? Thus, assuring that there is only one global APCu cache in play? I realize that I need to do math to figure out the correct pm.max_children value.

Please note: I can't switch to memcached at this point, since I am relying on a regex iteration over the APCu keys via an APCUIterator object. I may get there eventually, but as of now I have to stick with APCu.

us flag
All PHP-FPM processes are forked by a single parent, and the APCu cache should be shared among them. Please share the full PHP and nginx configuration in your question.
cn flag
C C
@TeroKilkanen thank you for that insight - and now, based on some additional testing I did yesterday, it does seem like all php-fpm PIDs are pointing back to a common parent PID. I'm using an essentially 'plain vanilla' config for nginx and php. Also, the real cause of my problem ended up being I am deployed in OpenShift in three pods - so I have three different PHP/caches. Users hit the pods randomly. Which means I have to move this all to a separate micro-service specifically for caching.
us flag
Or you can route requests based on IP hash so the requests consistently reach the same server.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.