没有不值得去解决的问题,也没有不值得去学习的技术!

Troubleshooting a 7-Second GraphQL API: Can Windows PHP-FPM Handle Concurrent Requests?

A GraphQL API takes up to 7 seconds

1. A GraphQL API takes up to 7 seconds. See Figure 1.

A GraphQL API takes up to 7 seconds
Figure 1

2. Querying the time for this request in Laravel Telescope shows 1345 ms. See Figure 2.

Querying the time for this request in Laravel Telescope shows 1345 ms
Figure 2

3. Instead of requesting the GraphQL API from the page, making a standalone request using Altair GraphQL Client takes 1.87 seconds. This is roughly consistent with the time in Laravel Telescope. See Figure 3.

Instead of requesting the GraphQL API from the page, making a standalone request using Altair GraphQL Client takes 1.87 seconds. This is roughly consistent with the time in Laravel Telescope
Figure 3

4. The current runtime environment is Windows 10, Nginx, and PHP FastCGI. This environment has a limitation: it can only serve one HTTP request at a time. This causes the GraphQL API wait time to reach 7 seconds.

5. The current Nginx configuration is as follows:

Nginx

http {
	## PHP-FPM Servers ##
	upstream php-fpm {
		server 127.0.0.1:9000;
	}

	server {
		location ~ \.php$ {
			# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
			include fastcgi_params;
		}
	}
}

6. Reference: https://stackoverflow.com/questions/15819351/can-windows-php-fpm-serve-multiple-simultaneous-requests .

7. Adjust the Nginx configuration as follows:

Nginx

http {
	## PHP-FPM Servers ##
	upstream php-fpm {
		server 127.0.0.1:9000;
	}

	upstream php_farm {
        server 127.0.0.1:9003 weight=1;
        server 127.0.0.1:9004 weight=1;
        server 127.0.0.1:9005 weight=1;
		server 127.0.0.1:9006 weight=1;
    }

	server {
		location ~ \.php$ {
			# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
			# fastcgi_pass 127.0.0.1:9000;
			fastcgi_pass php_farm;
			fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
			include fastcgi_params;
		}
	}
}

8. Several php-cgi processes need to be started and bound to different ports: 9003, 9004, 9005, 9006. See Figure 4.

Several php-cgi processes need to be started and bound to different ports: 9003, 9004, 9005, 9006
Figure 4

9. After restarting Nginx. See Figure 5.

After restarting Nginx
Figure 5

10. Refresh the page in the browser again to check the GraphQL API request time. It has been reduced to about 2 seconds, as expected. See Figure 6.

Refresh the page in the browser again to check the GraphQL API request time. It has been reduced to about 2 seconds, as expected
Figure 6

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系