Report this

What is the reason for this report?

Problem with fastcgi buffering in App platform.

Posted on October 4, 2021

This issue bothered me for a couple of frustrating hours. I have a Laravel setup on the App platform, using my own container images, but all fairly standard and similar to this DO tutorial in terms of containers.

So I basically have an nginx container talking to a php-fpm container on port 9000. This all works fine in docker-compose, but not on App platform.

The problem was that for longer outputs, especially json, the response just ‘froze’ after about 1500bytes. This made me think it was a buffering issue.

While debugging I connected to the php-fpm server directly using this guide. This worked fine, for outputs of any size.

In the end I found out that there is a nginx directive to disable fastcgi buffering, which resolved the issue.

fastcgi_buffering off;

However I don’t understand why this problem started, and I don’t know whether disabling this buffering may have other (performance?) drawbacks.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Your discovery that setting fastcgi_buffering off; resolves the issue with responses ‘freezing’ after a certain byte size on the DigitalOcean App Platform is quite insightful.

This problem can indeed stem from how buffering is handled between Nginx and PHP-FPM, especially under the constraints or specific configurations of a managed platform like the DigitalOcean App Platform.

Understanding FastCGI Buffering

FastCGI buffering in Nginx controls whether the response to a request, which is served by a FastCGI server like PHP-FPM, should be buffered. When enabled (which is the default behavior), Nginx will buffer responses in memory or on disk before sending them to the client. This can improve performance for certain applications by freeing up FastCGI processes to handle other requests instead of waiting for clients to acknowledge receipt of data.

Why Disabling FastCGI Buffering Helped

Disabling FastCGI buffering (fastcgi_buffering off;) means that Nginx forwards the response from PHP-FPM to the client immediately as it receives it, without waiting for the entire response to be received from the backend. This approach can resolve issues in environments where buffering configurations are not optimal for the application’s response patterns, leading to problems like the one you encountered.

Potential Drawbacks

  • Performance Impact: Disabling buffering might increase the load on your PHP-FPM processes, as they now have to maintain connections longer. This can impact the application’s ability to handle high concurrency levels efficiently.
  • Increased Resource Usage: With buffering disabled, each active connection will hold onto PHP-FPM processes for a longer period, potentially increasing memory and CPU usage.
  • Throughput: The throughput of your application might decrease, especially under high load, as the system can no longer benefit from the efficiency gained by buffering responses.

The specific cause can be difficult to pinpoint without detailed knowledge of the DigitalOcean App Platform’s internal networking and configuration nuances. It could be related to the default buffer sizes and the way the platform handles network traffic between containers. The issue might not manifest in local Docker environments due to differences in network latency, available resources, or other environmental factors.

While disabling FastCGI buffering solved the immediate issue, it’s worth considering the following for a more robust solution:

  • Review Application Output: If possible, review how your application generates output. Large responses, especially JSON, might benefit from optimization or pagination.
  • Customize Buffer Sizes: Instead of disabling buffering entirely, adjusting buffer sizes with directives like fastcgi_buffers and fastcgi_buffer_size might provide a middle ground, allowing you to benefit from buffering while accommodating larger responses.
  • Monitor Performance: Keep an eye on your application’s performance and resource utilization. If disabling buffering leads to issues, reconsider the configuration or explore other optimizations.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.