Note that there are some explanatory texts on larger screens.

plurals
  1. POAbsurd MySQL behavior over same data set but different threads
    primarykey
    data
    text
    <p>I'm connecting to MySQL db from two different PHP instances. The first thread adds a new row, passes the 'id' of new row to the second thread via a queue. <em>Sometimes</em> the second thread is unable to find the new row data, even though theoretically it should hit the db after first threads has finished its job.</p> <p>The simplified pseudocode looks something like</p> <p>Thread 1</p> <pre><code>$db = get_mysql_connection(); $db-&gt;beginTransaction(); $rowid = $db-&gt;query("insert data.."); $db-&gt;commit(); //For Debugging purposes only $db-&gt;check_if_row_exists($rowid); //Always returns true send_to_queue($rowid); </code></pre> <p>Thread 2</p> <pre><code>$rowid = fetch_from_queue(); $db = get_mysql_connection(); $db-&gt;check_if_row_exists($rowid); //Sometimes returns false; usleep(1000000); $db-&gt;check_if_row_exists($rowid); //Always returns true. </code></pre> <p>I cannot understand, why thread 1 shows that data has a valid entry, while thread 2, which certainly makes a query <em>sometime</em> after thread 1, is unable to find the data. I'm using transactions to commit the data, is that doing something weird?</p> <p>I'm using Gearman as the queue. Thread 1 is run through Apache, while Thread 2 simply runs as a standalone process.</p> <p><strong>Edit 1:</strong> This happens when thread 2 is running concurrently to thread 1. Apparently it reaches some race condition with Thread 1, but I can't figure out why. </p> <p><strong>Edit 2:</strong> As pointed out by N.B., Innodb delays writing the data to disk, and hence it is not visible to second thread. </p> <p>How should I handle this scenario? Sleep/Usleep is nearly always a suboptimal solution, as under heavy load conditions, the disk i/o time may increase. Is there some way to 'notify' the second thread that Innodb has finished its disk i/o?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload