0

I have few instances of my application, from each instance we have a single thread that picks a unprocessed item from a MySql table, and starts processing it. The table structure is as follows:

id   |   status  | other_params
     |           |             
     |           |

'status' field will denotes whether the entry is processed or not.

I am facing issues as to how, I will ensure that when one instance/thread picks up an entry from the table, no other thread picks that entry for processing.

I have thought of solution about changing the status to 'PROCESSING' and to 'PROCESSED' when done, but for the change to be visible to other threads, I need to commit. And if the node processing the request fails, it would always be 'PROCESSING'.

Also the operation is an heavy operation, so I don't want more than one thread to do the task.

Any ideas someone can provide will be helpful.

3 Answers 3

1

how, I will ensure that when one instance/thread picks up an entry from the table, no other thread picks that entry for processing.

You can do that by providing row level lock. Also put a lock wait timeout.

Suppose you have 2 threads T1,T2 which are trying to pick the same unprocessed item from the table. If T1 fails due to any reason, the transaction will timeout and the lock will be released. In that case, T2 can go and process it.

8
  • I read that locks will be on index on which we queried, I mean total no. of scanned rows. And if say lock is held by one thread, and other thread also tries to grab the record, how to handle it, so that it does not wait on it? Jun 12, 2016 at 5:38
  • That is upon how you chose your index. If your index is Unique, it will always end up taking that index and hence a particular row. Jun 12, 2016 at 5:46
  • I need to choose unprocessed items, so it can scan more items right. One way I can think of is, in a separate transaction fetch unprocessed items, and then get the primary key(unique key) and then lock the exact row. Does this approach seems right to you? Jun 12, 2016 at 6:06
  • Yes. That is similar to worker model. You can do that Jun 12, 2016 at 6:07
  • I have tried locking the row, but the second thread T2(T1 has the loick), it times out. I cant afford to have a timeout. How to handle it, any ideas? Jun 12, 2016 at 9:42
1

Have you considered acquiring a pessimistic lock on the row table? You will also have to do the work inside a transaction for this to work.

select * from your_table where id=1 for update;

Here are some links on pessimistic locking

manual

stackvoerflow

4
  • How do we handle the case for machine getting down, any ideas? Jun 12, 2016 at 5:41
  • If the machine processing the request falls down. Should the work be simply repeated or do you need to take some additional action, like cleaning up? Jun 12, 2016 at 5:50
  • I think mysql will release all held in a transaction locks then it detects connection failure. But you should probably test that. Here an excerpt from the manual If the connection for a client session terminates, whether normally or abnormally, the server implicitly releases all table locks held by the session (transactional and nontransactional). If the client reconnects, the locks will no longer be in effect. In addition, if the client had an active transaction, the server rolls back the transaction upon disconnect Jun 12, 2016 at 6:17
  • Hmm.. got it. Also suppose Transaction 1 held the lock, and Transaction 2 will also look for any probable requests it can process. How to ensure that Transaction 2 doesn't wait for one row only. I just want it to throw an exception or something if all the rows it has asked are locked, otherwise just give me one. Any idea around this? Jun 12, 2016 at 9:57
0

Please think of worker thread model.

Master thread would run on specific interval to fetch unprocessed records and hand them over to the worker thread.

It would be worker thread's responsiblity to mark status to Processed once successful processing.

Master thread should also cache the id's of the reocord it sent to workers.(it would be required to eliminate them for subsequent execution)

1
  • So both worker and master thread will reside on the same application, right? Jun 12, 2016 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.