Home » Mysql » Implementing a simple queue with PHP and MySQL?

Implementing a simple queue with PHP and MySQL?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a PHP script that retrieves rows from a database and then performs work based on the contents. The work can be time consuming (but not necessarily computationally expensive) and so I need to allow multiple scripts to run in parallel.

The rows in the database looks something like this:

+---------------------+---------------+------+-----+---------------------+----------------+
| Field               | Type          | Null | Key | Default             | Extra          |
+---------------------+---------------+------+-----+---------------------+----------------+
| id                  | bigint(11)    | NO   | PRI | NULL                | auto_increment |
.....
| date_update_started | datetime      | NO   |     | 0000-00-00 00:00:00 |                |
| date_last_updated   | datetime      | NO   |     | 0000-00-00 00:00:00 |                |
+---------------------+---------------+------+-----+---------------------+----------------+

My script currently selects rows with the oldest dates in date_last_updated (which is updated once the work is done) and does not make use of date_update_started.

If I were to run multiple instances of the script in parallel right now, they would select the same rows (at least some of the time) and duplicate work would be done.

What I’m thinking of doing is using a transaction to select the rows, update the date_update_started column, and then add a WHERE condition to the SQL statement selecting the rows to only select rows with date_update_started greater than some value (to ensure another script isn’t working on it). E.g.

$sth = $dbh->prepare('
    START TRANSACTION;
    SELECT * FROM table WHERE date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;
    UPDATE table DAY SET date_update_started = UTC_TIMESTAMP() WHERE id IN (SELECT id FROM table WHERE date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;);
    COMMIT;
');
$sth->execute(); // in real code some values will be bound
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);

From what I’ve read, this is essentially a queue implementation and seems to be frowned upon in MySQL. All the same, I need to find a way to allow multiple scripts to run in parallel, and after the research I’ve done this is what I’ve come up with.

Will this type of approach work? Is there a better way?

Answers:

I think your approach could work, as long as you also add some kind of identifier to the rows you selected that they are currently been worked on, it could be as @JuniusRendel suggested and i would even think about using another string key (random or instance id) for cases where the script resulted in errors and did not complete gracefully, as you will have to clean these fields once you updated the rows back after your work.

The problem with this approach as i see it is the option that there will be 2 scripts that run at the same point and will select the same rows before they were signed as locked. here as i can see it, it really depends on what kind of work you do on the rows, if the end result in these both scripts will be the same, i think the only problem you have is for wasted time and server memory (which are not small issues but i will put them aside for now…). if your work will result in different updates on both scripts your problem will be that you could have the wrong update at the end in the TB.

@Jean has mentioned the second approach you can take that involves using the MySql locks. i am not an expert of the subject but it seems like a good approach and using the ‘Select …. FOR UPDATE‘ statement could give you what you are looking for as you could do on the same call the select & the update – which will be faster than 2 separate queries and could reduce the risk for other instances to select these rows as they will be locked.

The ‘SELECT …. FOR UPDATE’ allows you to run a select statement and lock those specific rows for updating them, so your statement could look like:

START TRANSACTION;
   SELECT * FROM tb where field='value' LIMIT 1000 FOR UPDATE;
   UPDATE tb SET lock_field='1' WHERE field='value' LIMIT 1000;
COMMIT;

Locks are powerful but be careful that it wont affect your application in different sections. Check if those selected rows that are currently locked for the update, are they requested somewhere else in your application (maybe for the end user) and what will happen in that case.

Also, Tables must be InnoDB and it is recommended that the fields you are checking the where clause with have a Mysql index as if not you may lock the whole table or encounter the ‘Gap Lock‘.

There is also a possibility that the locking process and especially when running parallel scripts will be heavy on your CPU & memory.

here is another read on the subject: http://www.percona.com/blog/2006/08/06/select-lock-in-share-mode-and-for-update/

Hope this helps, and would like to hear how you progressed.

Questions:
Answers:

We have something like this implemented in production.

To avoid duplicates, we do a MySQL UPDATE like this (I modified the query to resemble your table):

UPDATE queue SET id = LAST_INSERT_ID(id), date_update_started = ... 
WHERE date_update_started IS NULL AND ...
LIMIT 1;

We do this UPDATE in a single transaction, and we leverage the LAST_INSERT_ID function. When used like that, with a parameter, it writes in the transaction session the parameter that, in this case, it’s the ID of the single (LIMIT 1) queue that has been updated (if there is one).

Just after that, we do:

SELECT LAST_INSERT_ID();

When used without parameter, it retrieves the previously stored value, obtaining the queue item’s ID that has to be performed.

Questions:
Answers:

Edit: Sorry, I totally misunderstood your question

You should just put a “locked” column on your table put the value to true on the entries your script is working with, and when it’s done put it to false.

In my case i have put 3 other timestamp (integer) columns: target_ts , start_ts , done_ts.
You

UPDATE table SET locked = TRUE WHERE target_ts<=UNIX_TIMESTAMP() AND ISNULL(done_ts) AND ISNULL(start_ts);

and then

SELECT * FROM table WHERE target_ts<=UNIX_TIMESTAMP() AND ISNULL(start_ts) AND locked=TRUE;

Do your jobs and update each entry one by one (to avoid data inconcistencies) setting the done_ts property to current timestamp (you can also unlock them now). You can update target_ts to the next update you wish or you can ignore this column and just use done_ts for your select

Questions:
Answers:

Each time the script runs I would have the script generate a uniqid.

$sctiptInstance = uniqid();

I would add a script instance column to hold this value as a varchar and put an index on it. When the script runs I would use select for update inside of a transaction to select your rows based on whatever logic, excluding rows with a script instance, and then update those rows with the script instance. Something like:

START TRANSACTION;
SELECT * FROM table WHERE script_instance = '' AND date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000 FOR UPDATE;
UPDATE table SET date_update_started = UTC_TIMESTAMP(), script_instance = '{$scriptInstance}' WHERE script_instance = '' AND date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;
COMMIT;

Now those rows will be excluded from other instances of the script. Do you work, and then update the rows to set the script instance back to null or blank, and also update your date last updated column.

You could also use the script instance to write to another table called “current instances” or something like that, and have the script check that table to get a count of running scripts to control the number of concurrent scripts. I would add the PID of the script to the table as well. You could then use that information to create a housekeeping script to run from cron periodically to check for long running or rogue processes and kill them, etc.

Questions:
Answers:

I have a system working exactly like this in production. We run a script every minute to do some processing, and sometimes that run can take more than a minute.

We have a table column for status, which is 0 for NOT RUN YET, 1 for FINISHED, and other value for under way.

The first thing the script does is to update the table, setting a line or multiple lines with a value meaning that we are working on that line. We use getmypid() to update the lines that we want to work on, and that are still unprocessed.

When we finish the processing, the script updates the lines that have the same process ID, marking them as finished (status 1).

This way we avoid each of the scripts to try and process a line that is already under processing, and it works like a charm. This doesn’t mean that there isn’t a better way, but this does get the work done.

Questions:
Answers:

I have used a stored procedure for very similar reasons in the past. We used the FOR UPDATE read lock to lock the table while a selected flag was updated to remove that entry from any future selects. It looked something like this:

CREATE PROCEDURE `select_and_lock`()
 BEGIN
  START TRANSACTION;
  SELECT your_fields FROM a_table WHERE some_stuff=something 
   AND selected = 0 FOR UPDATE;
  UPDATE a_table SET selected = 1;
  COMMIT;
 END$$

No reason it has to be done in a stored procedure though now I think about it.