Data Corruption in Rails with Peter Bhat Harkins
Today's Ruby Rogues podcast features Data Corruption in Rails with Peter Bhat Harkins. Peter started in rails since the time version 1.0 was released. He spent 5 years consulting full time, and now runs a consultancy for SAAS companies at Revenue.systems.
Few months ago, he spoke at the Rails Remote Conf about Data Corruption in Rails. The issue comes up when a .valid call returns false. It happened twice on his end. Tune in to learn about it, and understand how you can provide an effective solution!
Building with Partial Writes
One instance where the issue arises involves partial writes. Take for example, creating a blog post which has different fields. When you change the headline field and call post.save, it won't rewrite the other fields back to the database.
According to Peter, there is a hidden race condition when 2 fields are interdependent. If 2 users are editing at the same time—both doing a partial write, each user has valid blog post. But when those 2 partial writes happen, they end up with a blog post in an invalid database.
An Ongoing Issue
Peter experienced this with coupon codes that can be used on a website or on a phone. But if those are used on the phone on the last day, they become marked not only as used but also as expired.
Consider having 1,000 coupons: 800 were used but 202 of them expired. That is just not possible. In Peter's case, he already fixed it once but saw the issue again later on.
How to Fix Data Corruption
You can fix the issue in a couple of ways. For one, you can simply turn off partial writes. People generally don't want to do it, but Rails has both pessimistic and optimistic locking that you can use.
Pessimistic locking in particular uses a database locking feature. You can pull out a record and save it back. With that done, none should be updating it.
Download and listen to Data Corruption in Rails with Peter Bhat Harkins. Don't forget to leave a rating and review if you enjoyed the show. We would love to hear from you!
David: Rode Podcaster Studio One
Charles: Devchat audience survey, My Ruby Story
Email Peter: email@example.com