Throttle ActiveRecord creation
One of the sites I maintain, www.radio1.nl, was recently bombarded by GeenStijl. This is the Dutch equivalent of a slashdotting. The app held up quite well, even though I hadn’t explicitly hardened the application.
There was definitely noticable slowdown though, so I quickly developed a throttle plugin to keep the site more responsive. A week after the event, we were GeenStijl’d again. This time with the plugin in place there were zero problems!
I have released the plugin as open source, check it out on github. It’s been in production for a while and it’s held really well. From the readme:
Gimme A Break
This plugin adds a way to throttle ActiveRecord creation.
Add gimme_a_break to your model to enable the throttling. It will then not be possible to create models in quick succession. Object creation is usually very “expensive” (in time) and blocks most database servers. Database IO is usually the bottleneck in web applications, and thottling this back is a great way to increase overall availability.
An error message will be added to the base object and the object will not be valid. It’s up to you to display this error to the user. When you follow ActiveRecord convention you will be fine. The error message can be localized using the key gimme_a_break.notice with {{count}} as the timeout in seconds.
The throttle is applied per running instance. If you have two Passenger instances running, people will be able to create twice the amount of objects. This is exactly what you want, since the limit should scale linearly with the capacity of your servers. If your database keeps getting overloaded, reduce the timeout.
No comments yet.