• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Chat product reduce server request and increase perfomance

Dear Everybody,

I am developing a live chat software in LAMP. For updating or handling the chat requests it uses it uses around 9 concurrent ajax server requests. In this case  360 requests per user per minute(approximately). Which is overloading the the server.

If i am making all these as single ajax requests, it will increase the processing time and which will reduce the product performance.  As i am using mysql for storing is there will be an delay after long run of the software as history is saved in the database.

Thank you for your suggestions.
  • 3
  • 3
  • 2
1 Solution
Ray PaseurCommented:
Well, I guess the standard "tuning" ideas should be followed first.  Look at your data base structure.  Are the indexes correct?  Some strong guidance is available here:

You may want to investigate MemcacheD and APC.  You may also want to think about decoupling the "real-time" activities from things like saving the history.  You can make a POST-method request to an asynchronous script, passing the historical information and it can write the updates with a "low priority" query.

As your application starts to grow to scale, you will find that data base normalization may not be your friend.  Make a Google search for the exact phrase, "Should I Normalize My Database" and read the very good ideas on both sides of the question.  The more tables you have to join, the slower your application will be.  Eventually it will be too slow.  Here is how I come to the math.

360 requests per user per minute means you have 167 milliseconds to complete each request for each user.  If you have 10 users, you have about 17ms for each request.  That's about as fast as a single server can handle a request if there is any data base activity, because disks spinning at 7,200 RPM can make two revolutions in 17ms.  If you assume any variation in the distribution of the arrival rate for the requests, you will quickly find that your disk I/O operations are spending more time in the queue than they are in the SQL server!

I'm not an expert in tuning large applications at scale, but I know some people who are and they offer consulting services as well as PHP training.  It might be a good investment of your time and money to bring them into the project before a load situation causes you trouble.

Best of luck with it, ~Ray
JacobbabyAuthor Commented:
Thank you Ray,

My concern is if 1000 users are there then there will be 360*1000 requests and whether mysql can handle this much requests. i am dynamically create tables for saving the history data is there any limit for maximum mysql tables possible in a particular database.
Ray PaseurCommented:
If you have 1,000 users you have a real problem.  Your community is not big enough to make it worthwhile unless the 1,000 users are paying a substantial monthly fee and they're all repeat customers.  Nobody will pay you to advertise to a community of 1,000.  So the common economics may work against you.

MySQL need not be a problem, but you may need several instances of the MySQL server.  PHP will not be a problem (Facebook is written in PHP) but you may also need several dedicated servers.  Fortunately servers get faster and more capable with each new generation and the costs also decline over time.  

If you're thinking of building your own data center, be aware of the cost structure.  Today, electricity is 75% of the cost of running a data center.  If you focus on the cost of real estate and the cost of equipment acquisition and human capital, you will have missed the center of gravity!

If you're serious about the project you should consider your overall budget to get from design to launch, and invest in expert assistance at the design phase.  Changes made early in the process cost a tiny fraction of changes that must be made once an application is deployed.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

greetings Jacobbaby, , I have looked into the PHP platform doing realtime CHAT (as opposed to forum type chat), and it is well documented that PHP is NOT configured for the continuous requests required for chat, if the number of chat users at any time exceed about 3 to 4 hundred (300-400). The reason is that PHP (and other server platforms) do several many things at the start up for each PHP page request, PHP sets up memory blocks (ZEND optimizer) and starts processor threads and several other things, This will reduce the time (increase  speed) at which the PHP page can do it's work and deliver it's output, and is made to use for many different page requests, however in chat the AJAX requests go to the same page request , over and over again, so if there are, say 400 users in chat, then the PHP allotment of memory and threads gets overly crowed and slow. It is not so much that there is not memory to use, but that the ZEND memory manager does so much in the checking and creating the process heap memory blocks that things slow down. I had some links about this PHP chat thing, but I can not find them. An example often sited is the FaceBook Chat and their use of PHP, , , facebook could not get chat to work usin the PHP platform, so they set up special chat servers using a special JAVA platform (can not use Tomcat java), that only did the ajax chat requests. But they may have changed it again, that was two years ago or more.

= = = = = = = = = = = = = = = =
as to your chat setup, you say - "uses around 9 concurrent ajax server requests", from the chat PHP that I have done , this seems utterly ridiculous, and as if you are not optimizing your code to reduce the CHAT data transfer via ajax to an absolute MINIMUM.

You also say - '360 requests per user per minute", , again this is a chat KILLER, even on a special JAVA platform for chat, and in PHP you can not hope to even do just a few users in chat.

Many PHP chat Classes have ONE ajax chat request about every two or three seconds, the one I worked on did one request every 2500 milliseconds. This is about 20 requests per minute per user.

There are hundreds of PHP chat code out there, available online for free and paid. You should download several of these and look at their code, to see the reduction of ajax transfer that some of them have developed over the years. I learned many things about AJAX and PHP, when I studied the code for some CHAT php that others had done.
I do not see why you want to do the whole CHAT development thing, since their are PHP code already in existence that have been developed for years.
Like I said, though PHP may not be the platform for any but small number of chat user connections at any one time.

Trying to be brief in my explanations , so much of it may be technically incorrect, but you can research it your self.
Also depending on your chat processes in PHP a data base access for much of chat can be inefficient, you might consider a file storage for chat data, since you may not need any of the database filter and ordering operations.
JacobbabyAuthor Commented:
Dear All,

Thank you for your response,

 I am continuing my research on developing a optimized chat application using PHP and mysql. I am now planning to combine my chat request to make it 3 from 9. In my searching i came across pthreads, some body please tell me whether it will be good to use it, will it overload my server cpu. I am planning to combine my 9 chat request in to 3.

Each will contain 3 function calls which will do the actions and response from these three will be grouped and sent back as response. With this i think the chat request will be reduced to 1/3 rd of current situation. I want to call the three sub-functions parallel for that I am using pthreads to implement multi threading.

One thing i want to make clear is whether it will overload my chat server or not.
I will say something here, mostly because at this point of time, that this question has been up here, ,  there is not likely to be any experts reading this except those that follow this question.
I will say right off, that I have not used the PHP pthreads class, If you want some experts that may have used the PHP pthreads class, you should post a New Question, specifically about the PHP pthreads class.

Although in some development platforms, using separate threads does help speed things up to finish a task, however in any SERVER configuration, the amount of time it takes to do page tasks, is not so much what your specific page PHP code is doing (if you have efficient code), but on how may code pages are using the server processors at the same time, and this can be made so much better (page speed) by adding server hardware (processors) and load balancing configurations.
But the point that I was trying to make before, that even with massive hardware and great load balancing configurations (as in facebook PHP servers) , ,  the large latency of each PHP ajax page request (not related to thread use) is going to bog down the PHP chat servers (load balancing) once you get so many users on Chat at the same time (even if you use different PHP pages for different "chat rooms" for the ajax requests), say if you have 400 people signed into chat ajax at the same time. Calling ajax requests for 400 users once every second can over work PHP's ability to divide out the tasks neccessary to do all that work and all the other server work needed for that web site.

However you can make PHP chat ajax efficient (use an absolute minimum of ajax requests) and have very successful chat on your site, as long as you can limit the amount of people using the chat at the same time.

But my statements are guessing at what and how and, how many a CHAT site can be set up in PHP, as I said based on info from 2 years ago.
But I can tell you that all php chat works great and as expected during the "testing" and using with just a few "test users", , BUT you can not know how it will work with larger number of people signed in to chat, ,  UNTIL that load of work happens.

I will not likely say any more here, and you might have to do research and testing without much expert comment on this question page?
All new developments depend on testing and trial and error, I hope you can find info on PHP chat configurations that can help you. Good Luck!
JacobbabyAuthor Commented:
Dear Slick812,

Thank you for your response. I will check it.
I remembered something from a Chat setup that I studied before, Their thinking was that because It was the PHP engine  page initiation workings that limited user numbers in chat, They had a plain TEXT file on the server that the AJAX would download the contents every 1750 milliseconds, and display in the DIV that had all of the CHAT dialogs (users talking). Then they had a separate Ajax call for sending the user's comments (chat input) to the server, the php server would take the user comment and format it to HTML then write it to the end of the  plain TEXT file on the server, this eliminated the Database chat traffic for user input, and supposedly increased the amount of chat users before server bog down because the  plain TEXT file on the server would NOT NEED to call the PHP engine. Could not say if this worked or not, but the idea seems worth a shot?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now