brefphp / bref

Serverless PHP on AWS Lambda
https://bref.sh
MIT License
3.09k stars 367 forks source link

Automatically gzip large responses #1661

Open jaulz opened 10 months ago

jaulz commented 10 months ago

I wonder if Bref could automatically try to gzip the response if it's bigger than the 6MB limit. I gave it a try and at least this approach works for me (removed the Vapor header): https://bannister.me/blog/gzip-compression-on-laravel-vapor/

Since it's already possible via (Laravel) middleware, it's not urgent but maybe a nice addition.

mnapoli commented 10 months ago

Hey @jaulz, that is interesting! Do you have any numbers on bigger payloads that you are able to return that way?

jaulz commented 10 months ago

Yeah, for the one critical response I was dealing with it's a factor of 24 (from 8MB of text to 300KB): image

I used this code:

      $content = $response->getContent();
      $compressedContent = gzencode($content, 9);

      $response->setContent($compressedContent);
      $response->headers->add([
        'Content-Encoding' => 'gzip',
        'X-Original-Size' => mb_strlen($content, '8bit'),
        'X-Compressed-Size' => mb_strlen($compressedContent, '8bit'),
      ]);
kevincerro commented 10 months ago

Now AWS supports response streaming to bypass 6MB limit

Its supported on NodeJS official runtime but AWS allows to support on custom runtimes too.

Maybe this will be interesting also in combination with gzip encoding, because GZIP works great on text responses but on binary responses maybe not

https://aws.amazon.com/es/blogs/compute/introducing-aws-lambda-response-streaming/ https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html

kevincerro commented 10 months ago

In my case from 87,6kB to 5,8kB on a simple SF project using GZIP

Captura de pantalla 2023-10-24 a las 14 11 49

mnapoli commented 10 months ago

@kevincerro FYI https://github.com/brefphp/bref/issues/1503