Building & Testing  Scalable Rails  Applications
Mike Smith• Technical Lead - Blitz.io• Ruby developer• @evilmike
Building a scalable app
A typical Rails stackNginx+Passenger   • Passenger forwards                    HTTP requests                  • Rails proc...
Rails concurrency• Multiple Rails worker processes per server• # of processes is limited by available  memory• Each Rails ...
Key Concept!• There’s a finite number of requests that can  be handled concurrently• Additional requests wait until the nex...
Speed up your slow     requests
Database Queries• Identify slow database queries• Optimize queries & add table indexes• ActiveRecord::Base.explain (Rails ...
Rails Caching• Page Caching• Action Caching• Fragment Caching• Low-level Caching
Long-running Tasks• Don’t execute long-running tasks in the  request handler• Use background workers                 Resqu...
Free Rails to handle dynamic requests
Static Content• Serve with Nginx, Apache, etc.• CSS, Images & Javascript• User-uploaded & generated files (S3, etc)• Consid...
HTTP Caching• Caching layer in front of your Rails process• Varnish & Rack::Cache• Expiration model  •   Expires: Fri, 28 ...
Scale yourinfrastructure up
Scale vertically
Increase the size of     your servers• More memory, More CPU• Good for Database servers & Caching  servers
Scale horizontally
Add more servers• Serve more requests at a time• Good for Application servers &  Background workers
Two app servers           HTTP Load BalancerNginx+Passenger          Nginx+Passenger
2 Application Server Instances4 Application Server Instances
Load testing tips
Test in a realistic       environment• Use realistic data• Use an environment that replicates  production
Consider your location• Latency can affect load test results• Consider running your load test from the  region where most ...
Consider the effects of       caching• To test worst-case performance, you may  need to bust some caches• Vary the URL or ...
Pay attention to     request headers• Emulate the HTTP headers of your  expected clients • Accept-Encoding:    gzip, defla...
Try• Sign up & test with 250 virtual users for    free•          Engine Yard Cloud users    • http://cloud.engineyard.com/...
Building & Testing Scalable Rails Applications
Upcoming SlideShare
Loading in …5
×

Building & Testing Scalable Rails Applications

1,768 views
1,596 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,768
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
11
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Building & Testing Scalable Rails Applications

  1. 1. Building & Testing Scalable Rails Applications
  2. 2. Mike Smith• Technical Lead - Blitz.io• Ruby developer• @evilmike
  3. 3. Building a scalable app
  4. 4. A typical Rails stackNginx+Passenger • Passenger forwards HTTP requests • Rails processes generate responses • Database is shared by all Rails processes
  5. 5. Rails concurrency• Multiple Rails worker processes per server• # of processes is limited by available memory• Each Rails process handles one request at a time
  6. 6. Key Concept!• There’s a finite number of requests that can be handled concurrently• Additional requests wait until the next Rails process becomes available
  7. 7. Speed up your slow requests
  8. 8. Database Queries• Identify slow database queries• Optimize queries & add table indexes• ActiveRecord::Base.explain (Rails 3.2)
  9. 9. Rails Caching• Page Caching• Action Caching• Fragment Caching• Low-level Caching
  10. 10. Long-running Tasks• Don’t execute long-running tasks in the request handler• Use background workers Resque, Delayed Job
  11. 11. Free Rails to handle dynamic requests
  12. 12. Static Content• Serve with Nginx, Apache, etc.• CSS, Images & Javascript• User-uploaded & generated files (S3, etc)• Consider using a CDN
  13. 13. HTTP Caching• Caching layer in front of your Rails process• Varnish & Rack::Cache• Expiration model • Expires: Fri, 28 Sep 2012 01:12:32 GMT• Validation model (Conditional GET) • Last-Modified: Wed, 29 Aug 2012 04:58:08 GMT • ETag: ae7d971f0
  14. 14. Scale yourinfrastructure up
  15. 15. Scale vertically
  16. 16. Increase the size of your servers• More memory, More CPU• Good for Database servers & Caching servers
  17. 17. Scale horizontally
  18. 18. Add more servers• Serve more requests at a time• Good for Application servers & Background workers
  19. 19. Two app servers HTTP Load BalancerNginx+Passenger Nginx+Passenger
  20. 20. 2 Application Server Instances4 Application Server Instances
  21. 21. Load testing tips
  22. 22. Test in a realistic environment• Use realistic data• Use an environment that replicates production
  23. 23. Consider your location• Latency can affect load test results• Consider running your load test from the region where most of your users originate
  24. 24. Consider the effects of caching• To test worst-case performance, you may need to bust some caches• Vary the URL or query parameters GET /blog/article_1 GET /blog/article_2 GET /blog/article_3 ...
  25. 25. Pay attention to request headers• Emulate the HTTP headers of your expected clients • Accept-Encoding: gzip, deflate • Accept: */* • User-Agent:
  26. 26. Try• Sign up & test with 250 virtual users for free• Engine Yard Cloud users • http://cloud.engineyard.com/addons/blitz• Or signup directly • http://blitz.io

×