I am trying to build a social networking website that would have a large amount of users logged in at the same time, with an upper limit of around 5000 at a time.
I plan to use the LAMP stack. Apache can have 2 mpms (multi processing modules) - preforker(forks a new process per request) and the worker model (which uses threads per request).
Considering that my server side code is in PHP, and I want to be able to scale up the website on demand, which one would be more preferable. Some third party PHP modules we are using are not thread safe, so ideally preforker is recommended.
Is there any way by which delay between swapping processes can be decreased in preforking
Is there any way by which I can use the worker module, with php as well ?
Social networking sites running on the LAMP stack, normally use which mode?
If you plan to server push services, apache is not a good choice. Take a look at cherokee and nginx, both work much better with php-fpm than mod_php with apache2, since you do not need CGI support (only FastCGI is supported with cherokee and nginx) only PHP.
A few weeks ago I migrated my WordPress network from apache2-mpm-itk with mod_php to nginx+php5-fpm. As a result system load drastically dropped, memory load became predictable and the user experience seems to be a lot faster. I also added some free test from loadimpact.com, the serving times were better with the new config also.
Apache isn't preferred with so much requests. You'd be better off with nginx for example. http://nginx.net/
"10000+ concurrent connections per server" sounds good enough ;-)
You could use Apache in worker mode if you do not use PHP with mod_php. Use PHP in Fastcgi mod, php-fpm is a must-try for that.
With current apache stable version (2.2) you will have problems if you want to use it in chrooted mode. But you may try apache 2.3 or wait for the 2.4 and use mod_proxy_fastcgi instead of mod_fastcgi or others. You could also test the new event basef mpm with this version of Apache. Or of course you could try the other web servers as answered by #petermolnar
Related
I am not pretty familiar with PHP threads, as long as I am searching for options for using threads in PHP, the most suitable tool I can find is pthreads. Though it is very convenient to use, it requires ZTS and it is clearly mentioned in the documents that this tool cannot be used in a web server environment.
Warning The pthreads extension cannot be used in a web server
environment. Threading in PHP is therefore restricted to CLI-based
applications only.
So I was wondering what is the best way to use threads or multi threads in an web server environment in PHP.
Yaba daba don't.
Why not? Because other languages do threads better.
What you want to do can be done with a worker that consumes events from a queue. This is how we do things in the PHP world.
Essentially you run another PHP process somewhere else (from a cron job, for example) that does your background processing. The web-related (fpm) workers should be as light weight as possible and only submit these tasks or events to the queue.
Multi threading (not actually) with PHP is achieved by a http server sending multiple requests to a php-fpm daemon (or mod_php if you are so inclined) and by running schedulers or workers in the background as separate, independent processes.
I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer
How does server manages multiple connections does it forks a child process for each request or does it handle using threads or does it handles using a thread pool?
The linked answer says a process is forked and then the author in comment says threads or process, which makes it confusing, if requests are served using child-processes, threads or thread pool?
As I know, every webserver has it's own kind of handling multpile simultanous request.
Usually Apache2 schould fork a child process for each new request. But you can somehow configure this behaviour as mentioned in your linked StackOverflow answer.
Nginx for example gets every request in one thread (processes new connections asyncronously like Node.js does) or sometimes uses caching (as configured; Nginx could also be used as a load balancer or HTTP proxy). It's a thing of choosing the right webserver for your application.
Apache2 could be a very good webserver but you need more loadbalancing when you want to use it in production. But it also has good power when having multiply short lasting connections or even documents which don't change at all (or using caching).
Nginx is very good if you expect many long lasting connections with somehow long processing time. You don't need that much loadbalancing then.
I hope, I was able to help you out with this ;)
Sources:
https://httpd.apache.org/docs/2.4/mod/worker.html
https://anturis.com/blog/nginx-vs-apache/
I recommend you to also look at: What is thread safe or non-thread safe in PHP?
I think the answer depends on how the web server and the cgi deploy.
In my company, we use Nginx as the web server and php-fpm as cgi, so the concurrent request is handled as process by php-fpm, not thread.
We configure the max number of process, and each request is handled by a single php process, if more requests(larger than the max number of process) come , they wait.
So, I believe PHP itself can support all of them, but how to use it, that depends.
After doing some research I ended up with below conclusions.
It is important to consider how PHP servers are set to be able to get insights into it.For setting up the server and PHP on your own, there could be three possibilities:
1) Using PHP as module (For many servers PHP has a direct module interface (also called SAPI))
2) CGI
3) FastCGI
Considering Case#1 PHP as module, in this case the module is integrated with the web server itself and now it puts the ball entirely on web server how it handles requests in terms of forking process, using threads, thread pools, etc.
For module, Apache mod_php appears to be very commonly used, and the Apache itself handles the requests using processes and threads in two models as mentioned in this answer
Prefork MPM uses multiple child processes with one thread each and
each process handles one connection at a time.
Worker MPM uses
multiple child processes with many threads each. Each thread handles
one connection at a time.
Obviously, other servers may take other approaches but, I am not aware of same.
For #2 and #3, web server and PHP part are handled in different processes, and how a web server handles the request and how it is further processed by application(PHP part) varies. For e.g.: NGINX may handle the request using asynchronous non-blocking I/O and Apache may handle requests using threads, but, how the request would be processed by FastCGI or CGI application is a different aspect as described below. Both the aspects i.e. how web server handles requests and how PHP part is processed would be important for PHP servers performance.
Considering #2, CGI protocol has makes web server and application (PHP) independent of each other and CGI Protocol requires application and web server to be handled using different process and the protocol does not promote reuse of the same process, which in turn means a new process is required to handle each request.
Considering#3, FastCGI protocol overcomes the limitation of CGI by allowing process re-use. If you check IIS FastCGI link FastCGI addresses the performance issues that are inherent in CGI by providing a mechanism to reuse a single process over and over again for many requests.
FastCGI maintains compatibility with non-thread-safe libraries by
providing a pool of reusable processes and ensuring that each process
handles only one request at a time.
That said, in case of FastCGI it appears that the server maintains a process pool and it uses the process pool to handle incoming client requests and since, the process pool does not require thread safe check, it provides a good performance.
PHP does not handle requests. The web server does.
For Apache HTTP Server, the most popular is "mod_php". This module is actually PHP itself, but compiled as a module for the web server, and so it gets loaded right inside it.
Since with mod_php, PHP gets loaded right into Apache, if Apache is going to handle concurrency using its Worker MPM (that is, using Threads)
For nginx PHP is totally outside of the web server with multiple PHP processes
It gives you choice sometimes to use non-thread safe or thread safe PHP.
But setlocale() function (when supported) is actually modifies the operation system process status and it is not thread safe.
You should remember it when you are not sure of how legacy code works.
I am running a Debian server 7, after preforming load testing using Jmeter on my website. I noticed that MySQL was dying after 50 users, PHP was dying after 100+ users and Apache 2 dying after 200+ users. Now my question is what is the best way to restart these services if they are terminated or froze up?
Restarting a service means killing all its current processes and start a new one. In the meantime you have lost/dropped all the requests from some legitimate users that will eventually see an http error or a timeout when connections are dropped.
I would ask myself, are you happy with 200+ users? Is mysql your bottleneck? Etc..
Use some sort of monitoring service like new relic and as a workaround just restart those services when alerts start coming in, either manually or automatically.
But if you want to improve your site performance deploy your service on a better infrastructure so it can scale up to bigger numbers or improve the code/app architecture used on your site, i.e. put some extra caching between mysql and your application.
Also It would be interesting to know how you have managed to test apache, mysql and php separately especially httpd vs php processes with a tool like JMeter that in my experience sure can test apache separately and mysql separately. But your php and apache scripts are really tightly bound together.
We have a fairly intensive Drupal installation running on a large EC2 server instance. The system takes registrations for campaigns that we host, and when we send out an invitation email the numbers of responses spike to around 1,000 per minute for the first ten or twenty minutes.
The system is running on a fairly standard LAMP installation, using the latest version of Drupal 7. I guess I have three questions:
1.) Should this amount of load be maxing out this size server? The install has the Organic Groups, Tokens, and Webforms modules running.
2.) Are there Mysql/Apache server tweaks that will minimize the amount of load per connection—shortening 'keep alive' time, etc.?
3.) Are there Drupal tweaks that we should implement to do something similar—maybe consolidating SQL calls?
I know this is a lot. Any ideas, suggestions, or critisisms will help.
Thanks!
For high server load with Drupal, combination of NGINX+PHP-FPM+APC+MySQL+VARNISH will be the best option.
If you are using multiple system behind ELB, you can use MEMCACHE instead of APC. You should use MySQL Replication in your architecture along with load balancer.
You can tune the server configuration of NGINX and PHP-FPM separately. If any process of PHP-FPM is taking time you can terminate that particular request (request_terminate_timeout, request_slowlog_timeout ). You can check all the configuration related info here PHP-FPM configuration
Also if you are using AWS, you should try to utilize their other services too in your architecture like S3 as CDN, ElastiCache, Cloudfront etc.
You can also use Google Page Speed service
I develop a web server's PHP scripts to retrieve tiled images to cover Earth surfaces such as World Wind.
Suppose I have get_image.php that returns image/jpeg or image/png as the response.
The initial condition is also that I have Windows Server 2003 to test my script on and preinstalled Apache 2.2.16 with a thread model. So I had to install thread safe PHP 5.3 to use it as Apache's module.
After script has been succesfully written I decided to produce a load testing using JMeter. Starting with a single virtual user performing a request per no more than 0.5 seconds and increasing the number of virtual users every minute at the moment of nine virtual users I get some not handled requests though looking in server's Task Manager I see no more than 8-10% of CPU. The maximum performance I get is 1600-1700 successful responses per a minute (requests are produced by eight virtual users).
I am not a system administrator and not experienced dealing with heavy performances, so my question is: can this be a problem of PHP thread safety discussed here? How can I determine the problem I face? Would it be better to try my script as CGI with IIS + FastCGI or we have to look at Linux-based web server?
P.S. I also use memcached server and php_memcache.dll (so-called thread safe version) downloaded from http://downloads.php.net/pierre. This module is not officially supported for Windows and probably it's not really thread safe so it could cause an additional effect to my problem in the case of the problem described is PHP thread safe issue.
If I were you I would use XDebug to track down which parts of your application ar eating the time and causing the failed requests.
Since you are on windows ( i presume ) then you can use a handy little program called WinCacheGrind to open and review the output files. It will show you, line by line or function by function, exactly how long the different blocks of code in your application take to run.
If you find that there is nothing surprising in the grind then that is the time to start looking at the environment.