In Video controller – Show action, I’ve tried with two scenarios. The first one uses increment_counter, and writes to the db directly:
Video.increment_counter(:views_count, @video.id)
The second approach uses the save method.
@video.views_count = @video.views_count + 1
@video.save
In ApacheBench, I do:
ab -n 100 -c 10 http://127.0.0.1:3000vidoes/18
Video id is 18. I make sure to reset views_count on each ab run, via Video.find(18).update_attribute(:views_count, 18).
After running ApacheBench for both scenarios, views_count is consistent at 100. I would have imagined increment_counter to be more accurate than save method, since it writes to the db directly.
Am I benching correctly?
The reason you get the correct answer with both methods is that you’re not really testing parallelism here. While apachebench is firing off 10 concurrent requests, your rails server is only handling one of them at a time. Try one of the following to exploit the race condition present in the second scenario:
thin -s 4and then putting all those servers behind an nginx or haproxy instance.