The Importance Of Queues To Improve Performance

6
Oct 2017

Posted by: Category:Web Design & Development

The Importance Of Queues To Improve Performance

The Importance Of Queues To Improve Performance

Queues are everywhere. When you delete a photo on your phone or an email on your computer, it goes to the Bin and will be deleted from there after 30 days. That is a queue. Without it you will not be able to recover anything removed by mistake.

When you work without an internet connection and are able to write 3 or 4 emails offline, have them saved in outbox and get them sent when you get to the WIFI connection, that is a queue. Without it you will not be able to write several messages offline, the mailing program would just complain that connection is not available after you finished your very first email and would not allow you to create the second one.

Literally, a queue is also a long line of people in Burger King. Several managers just take a next customer and serve them their order. If there was just a single manager, the queue processing would take ages and cost huge loss.

So, a queue allows the work to be shared among multiple “processors” (also known as “agents” or “workers”). So, these 3 examples just illustrate the basic principles of queues:

1. Queues make the user experience better

2. Queues allow to postpone the heavy processing and thus make the interface more robust

3. Queues allow separation of work between many workers

No surprise that if you apply queues in your application, its development will become more joyful and the application will become more robust.

A quick example is sending a notification email after a successful purchase. If you make your user wait until the email gets sent in real time and your email server is slow, your user will not get as happy as he or she could be if the interface would quickly say “Hey, thanks for purchase” or for that matter “Thanks for visiting”.

To achieve that speed of interface response, you just need to save the notification into your notifications queue and process it later soon, but not immediately. So, this article is about how to work with queues

Implementing Queues In PHP

A queue is, technically a data structure where data comes from one end and leaves the queue from another end. An example is a car wash and a line of cars: the first car from the line gets into the car wash and then leaves it, then the second car gets in, and so on.

That is why the queue is also called a FIFO: “first in, first out” — in contrast to a stack, another data structure, one in which “last in, first out” approach is followed for example- your browser history.

Talking about the major process of queuing, let’s say your application has to send an email to a user after visit.

Step 1. Create A Queue

With the fields like- with the fields id, data, status, error message

Step 2. Implement Your Queue Adapter.

<?php
class Queue {

public function addItem($item) {

$query = 'INSERT INTO (status, data, created_at) my_queue VALUES ("new", "' . json_encode( $item ) . '", NOW())';
sql_run_query($query);
}
};

?>

Step 3. Find In Your Code Where The Heavy Operation Is (Sending The Email):

<?php
//this line is the heavy operation and can take up to 10 seconds of waiting since our mail server is slow
 Mail::send (array( 
 'to' => 'user@server.com', 
  'subject' => 'Thanks for visiting', 
 'body' => 'Thank you very much, here are the details...
 ));  
 echo  'Thanks for visiting!'; 
?>

Step 4. Queue the Heavy Operation

<?php
//this is now a super-fast operation since it's just a database INSERT, no real email is sent now
  $QueueManager-> addItem(array( 
 'to' => 'user@server.com', 
  'subject' => 'Thanks for visiting', 
  'body' => 'Thank you very much, here are the details...
  ));  
  echo  'Thanks for visiting!'; 
?>

Leave a Reply