elephantinsideasnakefinal

Making Things Faster With Gearman and Supervisor

Sometimes our services need to perform some huge tasks after user interaction. For example, we need to send a letter, generate a report file, or call external APIs. These kinds of tasks can be slow because of third parties and can consume the resources of your server.

In this case, an application can become a snake eating an elephant, as in the book The Little Prince. You take some data from a user and make him wait because the snake needs some time to digest an elephant (or something else that your app needs to do):

A snake eating an elephant

To process this functionality faster, you need to make the parts of your application asynchronous. You can achieve this by delegating this task to a more powerful server or running it in a background process.

And Gearman is a proper tool that can be used to do this.

What Are We Going to Do?

In this tutorial, we will create a simple application that will delegate a task from a client to the Gearman worker. Our application will calculate a Fibonacci sequence in three processes. To run worker processes, we will install and configure Supervisor.

Please note that the examples in this tutorial need PHP7 to run.

So What Is Gearman Anyway?

First, let’s discover what is Gearman from its homepage:

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates.

In other words, Gearman is a queuing system that is easy to scale on many servers and flexible to use because of multi-language support.

Install Gearman

If you are running Debian/Ubuntu, run the following command to install Gearman with the necessary tools and PHP extension:

After that, run the Gearman server and check the status:

But you will not see anything helpful after the status command because we haven’t started any worker yet. Just remember this until we need it.

Create a Client

And we are ready to start a script called client.php. This script will create a Gearman client and send information to a server on the same machine:

You may have noticed that we sent numbers in a JSON format. Gearman clients and workers talk to each other in a string format, so one of the ways to serialize an array is to use the json_encode() function or something similar.

After receiving an answer from the worker, we will unserialize it with json_decode() and output as CSV rows:

Leave a Comment

Scroll to Top