Race conditions in django

Django 1.4+ supports select_for_update, in earlier versions you may execute raw SQL queries e.g. select … for update which depending on underlying DB will lock the row from any updates, you can do whatever you want with that row until the end of transaction. e.g. from django.db import transaction @transaction.commit_manually() def add_points(request): user = User.objects.select_for_update().get(id=request.user.id) … Read more

Do I need to be concerned with race conditions with asynchronous Javascript?

All Javascript event handler scripts are handled from one master event queue system. This means that event handlers run one at a time and one runs until completion before the next one that’s ready to go starts running. As such, there are none of the typical race conditions in Javascript that one would see in … Read more

How to make sure there is no race condition in MySQL database when incrementing a field?

Here’s 3 different approaches: Atomic update update table set tries=tries+1 where condition=value; and it will be done atomically. Use transactions If you do need to first select the value and update it in your application, you likely need to use transactions. That means you’ll have to use InnoDB, not MyISAM tables. Your query would be … Read more

Hidden threads in Javascript/Node that never execute user code: is it possible, and if so could it lead to an arcane possibility for a race condition?

No, this cannot happen. Yes, there are indeed “hidden” background threads that do the work for asychronous methods, but those don’t call callbacks. All execution of javascript does happen on the same thread, synchronously, sequentially. That data event callback will always be executed asynchronously, that is, after the current script/function ran to completion. While there … Read more