Search code examples
ruby-on-rails-4activerecordsidekiq

Sidekiq and Rails model validations


I have a model validation on :create that validates a certain attribute is unique and I create many elements using some background jobs on sidekiq using 50 concurrency on a pool of 50 mysql connections.

For some reasons I see many duplicates where I have the validation, is there a problem with concurrency?

The order of magnitude is of some thounsands records on millions of inserts, my only problem is that I'm apparently not able to debug this, and I can't predict what will happen if I add a node to my DB cluster or that happens if I change how I insert things in my DB.

Has anyone any idea what's happening here or how to proceed in debugging this?


Solution

  • Validating uniqueness with Rails is one giant race condition. Don't use it. The only way to guarantee uniqueness in the database is to use a unique index.

    https://rietta.com/blog/2015/05/04/validates-uniqueness-race-condition-in-ruby-on-rails/