Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:50:39+00:00 2026-05-31T09:50:39+00:00

I’m connecting to MySQL db from two different PHP instances. The first thread adds

  • 0

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. Sometimes 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.

The simplified pseudocode looks something like

Thread 1

$db = get_mysql_connection();
$db->beginTransaction();
$rowid = $db->query("insert data..");
$db->commit();

//For Debugging purposes only
$db->check_if_row_exists($rowid); //Always returns true

send_to_queue($rowid);

Thread 2

$rowid = fetch_from_queue();
$db = get_mysql_connection();
$db->check_if_row_exists($rowid); //Sometimes returns false;
usleep(1000000);
$db->check_if_row_exists($rowid); //Always returns true.

I cannot understand, why thread 1 shows that data has a valid entry, while thread 2, which certainly makes a query sometime after thread 1, is unable to find the data. I’m using transactions to commit the data, is that doing something weird?

I’m using Gearman as the queue. Thread 1 is run through Apache, while Thread 2 simply runs as a standalone process.

Edit 1:
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.

Edit 2:
As pointed out by N.B., Innodb delays writing the data to disk, and hence it is not visible to second thread.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:50:41+00:00Added an answer on May 31, 2026 at 9:50 am

    Pasting from my comment seeing that there’s no need to change anything:

    There are no race conditions here. Thread 1 can see its transactions
    and data. Thread 2 can’t because they haven’t reached the disk yet (no
    fsync call has been made yet by InnoDB). So naturally, you’ll always
    see data from thread 1, but if it’s not on the disk at the time of
    calling – you won’t see it within thread 2. Threads 1 and 2 don’t
    share the SAME mysql connection thread, both use a different one. So
    to cut it short – if not on disk, no data available. It’s not on disk
    because InnoDB will delay the write until the drive is ready to write.
    That’s why you see it after usleep.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.