Building & Testing Scalable Rails Applications

  • Published on

  • View

  • Download


Building & Testing Scalable Rails ApplicationsMike Smith Technical Lead - Ruby developer @evilmikeBuilding a scalable appA typical Rails stack Passenger forwards HTTP requests Rails processes generate responses Database is shared by all Rails processesNginx+Passenger Rails concurrency Multiple Rails worker processes per server # of processes is limited by available memory Each Rails process handles one request at a timeKey Concept! Theres a finite number of requests that can be handled concurrently Additional requests wait until the next Rails process becomes availableSpeed up your slow requestsDatabase Queries Identify slow database queries Optimize queries & add table indexes ActiveRecord::Base.explain (Rails 3.2)Rails Caching Page Caching Action Caching Fragment Caching Low-level CachingLong-running Tasks Dont execute long-running tasks in the request handler Use background workersResque, Delayed JobFree Rails to handle dynamic requestsStatic Content Serve with Nginx, Apache, etc. CSS, Images & Javascript User-uploaded & generated files (S3, etc) Consider using a CDNHTTP 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: ae7d971f0Scale your infrastructure upScale verticallyIncrease the size of your servers More memory, More CPU Good for Database servers & Caching serversScale horizontallyAdd more servers Serve more requests at a time Good for Application servers & Background workersTwo app serversNginx+Passenger Nginx+PassengerHTTP Load Balancer4 Application Server Instances2 Application Server InstancesLoad testing tipsTest in a realistic environment Use realistic data Use an environment that replicates productionConsider your location Latency can affect load test results Consider running your load test from the region where most of your users originateConsider the effects of caching To test worst-case performance, you may need to bust some caches Vary the URL or query parametersGET /blog/article_1GET /blog/article_2GET /blog/article_3...Pay attention to request headers Emulate the HTTP headers of your expected clientsAccept-Encoding: gzip, deflate Accept: */* User-Agent:Try Sign up & test with 250 virtual users for free Engine Yard Cloud users Or signup directly