Bref: Add documentation about PHP memory limit

Created on 19 Aug 2019  路  11Comments  路  Source: brefphp/bref

When configure Lambda's memory I naively thought that is the amount of memory PHP will use. I forgot about memory_limit in php.ini.

Correct me if Im wrong. But PHP-FPM will be running on a "warm" Lambda. Say that I've configured it with 1792Mb of memory. For every new request I get 1792Mb. Right, So why not specify memory_limit: 1792M?

Same with using console commands. Right?

documentation

Most helpful comment

The php.ini does support reading environment variables so this works:

memory_limit=${AWS_LAMBDA_FUNCTION_MEMORY_SIZE}M

Although this would need to be tested in the docker images.

All 11 comments

Yes that's correct.

The hard thing here is that we should set PHP's memory limit to the lambda's memory limit. And it varies based on the configuration of the user.

Can that be injected during serverless deploy? Or would setting PHP's memory_limit directive to -1 in Bref's base php.ini do the trick?

Can that be injected during serverless deploy?

Maybe, I haven't looked into that so I have no idea if it would be easy to do.

Setting -1 as the limit might be risky: PHP will never run the "garbage collector" and you might end up with aborted lambdas (because they hit the limit) even though there was no reason to. I'm not sure if what I say makes sense, and honestly I'm just speculating.

To inject the value during deploy is more "magic". It is also hard to figure out if a default value was used or if the user intentionally set that value.

I think this is a feature to the documentation.

Bah, I think both cases have pitfalls. You're right about it being _too magic_ and should be exposed to the user, but on the other hand as a user I think I'd want to set the value once in serverless.yaml for everything and not have to worry about remembering to set memory limits in other places.

If it is actually possible to inject the value, maybe we could have an auto mode where Bref figures out what it thinks should be the limit based on your Lamda's memory, and you can choose to override it if you want to?

Can we we have it both ways?
Can it be set once in serverless.yaml and then override it during execution (if needed)

Actually garbage collection should still kick in even with the memory_limit=-1 as it's only relying on the number of objects which have been fallen out of scope reach a treshold.

See also https://www.php.net/manual/de/features.gc.performance-considerations.php#features.gc.performance-considerations.slowdowns

The php.ini does support reading environment variables so this works:

memory_limit=${AWS_LAMBDA_FUNCTION_MEMORY_SIZE}M

Although this would need to be tested in the docker images.

Oh this is awesome!

Does that memory_limit setting include the memory for fpm etc? If not, we might still be out of luck because we only have our max memory minus the overhead (fpm, etc) available.

@georgeboot you are correct, there needs to be a margin for those other processes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnapoli picture mnapoli  路  6Comments

tlfbrito picture tlfbrito  路  8Comments

f3l1x picture f3l1x  路  6Comments

nealio82 picture nealio82  路  6Comments

tlfbrito picture tlfbrito  路  4Comments