shlogg · Early preview
Augusts Bautra @augustsbautra

Prevent Race Conditions With Retryable In Rails

Wrap .first_or_create!/find_or_create_by! calls in retryable block to prevent race conditions. Use Retryable gem with on: [ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid] for a clean solution.

tl;dr

Wrap all .first_or_create!/find_or_create_by! calls in retryable block.

Retryable.retryable(
  on: [ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid]
) do .. end

    
    

    
    




  
  
  Discussion

Oftentimes in Rails apps we have a situation where only one of something is allowed. A user may be allowed only one profile picture, a question only one answer, only one pinned post etc.
In these situations I prefer using the "ensure" pattern with .first_or_create!/find_or_create_by!. There's a gotcha, however. Both of these methods are vulnerable to race conditions, espe...