How often have you developed a website that using an external program or even a separated process? This case is not a common problem for website application because you want to make it run efficiently and fast as possible, Yet you need to provide crucial functionality for end users. Then the problem arises of how to get a fast and in the other hand full featured application that can process efficiently. Using Multitasking PHP and MySQL is the answer for that problem. Gearman provides solution for Multitasking PHP and MySQL.
MultiTasking PHP and MySQL by Using Gearman
Many years ago livejournal.com was facing a same problem. Users were uploading a lot of pictures to the website that had to be processed into their respective account. One of the process is resizing functionally such as reducing the raw image size and creating thumbnails for the website to display. These pictures mostly consisted of kittens. Million Kittens picture overloading the web application in livejournal. Then Brad Fitzpatrick formulated a great idea to deal these kind of problem, which lead to Gearman invention.
Gearman comprises a client component, multi tasking the job requested by clients, and worker programs that register themself for work and then carry out those job requests. It provides a distributed application framework. Gearman capable of multi threading and is known to be able to carry out 50000 jobs per second. Some giant websites using the C version of Gearman such as Digg, Yahoo, livejournal, SixApart, Xing, and others.
Gearman Was originally written in Perl, but later rewritten in C by Eric Day because he wanted more compatibility through client API and better performance. The Gearman process is actual job server, it receive requests for jobs and then assign them. The Gearman also keeps track of available workers and busy workers. It then handles all results from workers, passing them to the client that requested the job.
The basic gearman setup consist of a client, gearman(job server), and a worker. You can see how gearman utilizing a web environment in example picture below.
You can get latest Gearman source from their website http://gearman.org/download/ or http://launchpad.net/gearmand. These sites provide links to everything you need to run Gearman (Gearman MySQL UDF and the job server). To install Gearman Job Server follow these steps :
- Unpack the source tar xvzf gearmand-1.0.tar.gz
- Go to gearmand source distribution directory cd gearmand-1.0 and then ./configure
- Now build and install it using these commands make and make install
After you done following steps above. Gearmand is installed and ready to use.
To install Gearman(PECL), the steps are essentially :
12345 cd gearman-0.6.*/phpize./configuremakesudo make install
Then after doing these steps make sure to specify loading the shared lib, gearman.so in /etc/php5/conf.d/gearman.ini (for Ubuntu) with this command
If you use Ubuntu, this may already be done for you. Otherwise, add this line to php.ini. Refer to your OS manual. After you done with installing part, you can now starting to Multitasking PHP and MySQL by using Gearman