9. It’s Easy, Really.
1. Realize Your Site is Slow
2. Optimize the Database
3. Cache the Hell out of Everything
4. Scale Messaging
5. Deal With Abuse
10. It’s Easy, Really.
1. Realize Your Site is Slow
2. Optimize the Database
3. Cache the Hell out of Everything
4. Scale Messaging
5. Deal With Abuse
6. Profit
13. Don’t Be Like Us
• Munin
• Nagios
• AWStats & Google Analytics
• Exception Notifier / Exception Logger
• Immediately add reporting to track problems.
14. Test Everything
• Start Before You Start
• No Need To Be Fancy
• Tests Will Save Your Life
• Agile Becomes
Important When Your
Site Is Down
15. <!-- served to you through a copper wire by sampaati at 22 Apr
15:02 in 343 ms (d 102 / r 217). thank you, come again. -->
<!-- served to you through a copper wire by kolea.twitter.com at
22 Apr 15:02 in 235 ms (d 87 / r 130). thank you, come again. -->
<!-- served to you through a copper wire by raven.twitter.com at
22 Apr 15:01 in 450 ms (d 96 / r 337). thank you, come again. -->
Benchmarks?
let your users do it.
<!-- served to you through a copper wire by kolea.twitter.com at
22 Apr 15:00 in 409 ms (d 88 / r 307). thank you, come again. -->
<!-- served to you through a copper wire by firebird at 22 Apr
15:03 in 2094 ms (d 643 / r 1445). thank you, come again. -->
<!-- served to you through a copper wire by quetzal at 22 Apr
15:01 in 384 ms (d 70 / r 297). thank you, come again. -->
22. class AddIndex < ActiveRecord::Migration
def self.up
add_index :users, :email
end
def self.down
remove_index :users, :email
end
end
Repeat for any column that appears in a WHERE clause
Rails won’t do this for you.
24. class DenormalizeFriendsIds < ActiveRecord::Migration
def self.up
add_column "users", "friends_ids", :text
end
def self.down
remove_column "users", "friends_ids"
end
end
25. class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend
after_create :add_to_denormalized_friends
after_destroy :remove_from_denormalized_friends
def add_to_denormalized_friends
user.friends_ids << friend.id
user.friends_ids.uniq!
user.save_without_validation
end
def remove_from_denormalized_friends
user.friends_ids.delete(friend.id)
user.save_without_validation
end
end
28. That’s where we are.
Seriously.
If your Rails application is doing anything more
complex than that, you’re doing something wrong*.
* or you observed the First Rule of Butterfield.
35. class Status < ActiveRecord::Base
class << self
def count_with_memcache(*args)
return count_without_memcache unless args.empty?
count = CACHE.get(“status_count”)
if count.nil?
count = count_without_memcache
CACHE.set(“status_count”, count)
end
count
end
alias_method_chain :count, :memcache
end
after_create :increment_memcache_count
after_destroy :decrement_memcache_count
...
end
36. class User < ActiveRecord::Base
def friends_statuses
ids = CACHE.get(“friends_statuses:#{id}”)
Status.find(:all, :conditions => [“id IN (?)”, ids])
end
end
class Status < ActiveRecord::Base
after_create :update_caches
def update_caches
user.friends_ids.each do |friend_id|
ids = CACHE.get(“friends_statuses:#{friend_id}”)
ids.pop
ids.unshift(id)
CACHE.set(“friends_statuses:#{friend_id}”, ids)
end
end
end