Close
  • Latest News
  • Artificial Intelligence
  • Video
  • Big Data and Analytics
  • Cloud
  • Networking
  • Cybersecurity
  • Applications
  • IT Management
  • Storage
  • Sponsored
  • Mobile
  • Small Business
  • Development
  • Database
  • Servers
  • Android
  • Apple
  • Innovation
  • Blogs
  • PC Hardware
  • Reviews
  • Search Engines
  • Virtualization
Read Down
Sign in
Close
Welcome!Log into your account
Forgot your password?
Read Down
Password recovery
Recover your password
Close
Search
Logo
Logo
  • Latest News
  • Artificial Intelligence
  • Video
  • Big Data and Analytics
  • Cloud
  • Networking
  • Cybersecurity
  • Applications
  • IT Management
  • Storage
  • Sponsored
  • Mobile
  • Small Business
  • Development
  • Database
  • Servers
  • Android
  • Apple
  • Innovation
  • Blogs
  • PC Hardware
  • Reviews
  • Search Engines
  • Virtualization
More
    Home Applications
    • Applications
    • Database
    • Development
    • IT Management

    How to Scale Your Ruby Applications

    Written by

    Ezra Zygmuntowicz
    Published June 29, 2009
    Share
    Facebook
    Twitter
    Linkedin

      eWEEK content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

      Rails makes everything so simple for developers that it’s easy to get trapped into thinking it will solve every problem, making it easy to stop paying attention to what goes on behind the scenes. Rather than relying on Rails as a cure-all, developers need to focus on scalability from the start.

      The truth is that Rails will only take you 80 percent of the way. To go that last 20 percent to a truly scalable application, here are five things to consider:
      No. 1: Pay attention to your database
      Database queries, especially in high numbers, are big performance bottlenecks. To get comments on a blog, for example, ActiveRecord may issue one query per comment if you aren’t careful. On a popular blog that gets hundreds of comments, this means a few hundred SQL queries per page, which can be slow and inefficient.

      This is known as the “n+1 query problem” and you want to avoid it. Make sure to use the proper “#include” statements in order to fetch these related objects in one query. Also, make sure not to take this too far and pull in thousands of objects at once. It is all about finding balance.
      Rails eliminates a lot of the database grunt work but not all of it. Rails insulates programmers from SQL, but as the site grows and the application needs to scale, you will want to hand-optimize the database. To do this, you need to know what is happening there. Always tail your logs in development mode and watch to make sure SQL queries get dumped to your logs. That way, you know when the database is running too many queries and when to step in to make the process more efficient.
      No. 2: Divorce long-running queries
      No surprise: you want your application to run fast for its users. That said, users don’t care what goes on behind the scenes. As such, anything that doesn’t need to have results returned immediately to the client should be pushed to the background.

      If a user makes a request to resize profile images, encode a video or do a roll-up report-things that may take time-there’s no need for them to wait while a Web request is made. Instead, take the request, queue up the work to be done in the background, return status updates and have the page refresh itself when the job is done.
      Rails typically serves one request at a time, and that’s a detriment if there are long-running queries that block other incoming requests. Do the minimal amount of work possible during a Web request, and set up a queuing mechanism for the rest so your database isn’t overloaded. This makes the application fast and keeps the front-end Web servers open.
      A similar point: a lot of Rails applications deal with file uploads and user-generated assets. A lot of these applications store that data on Amazon S3. Trying to upload a video to an application while simultaneously processing images or uploading the video to Amazon S3, ties up the front-end Web servers. This means slow going for the user. Twenty Web servers can handle a lot of traffic, but twenty users uploading simultaneously with multiple requests means that others might get their requests rejected or timed out.
      The bottom line: for efficiency’s sake, never do image processing or upload files to another server during the request process. Instead, accept the upload, return success to client, and farm the rest of the heavy lifting for the background processing jobs to other servers.

      No. 3: Use caching techniques to keep the load off your application servers and databases
      Any time you can cache a computation or database query, even if it is only for a short time, you increase the scalability of your whole system. You can drastically keep the load down on your database servers by liberal doses of memcached. Memcached works by letting you store queries or computed objects in a memory storage that is distributed out across all of your application servers.

      The general pattern is that when you fetch or compute an object, you store it in memcached. Then the next time you need said object, you first check memcached and, only if it doesn’t exist there, you then fall back to the database or recompute the object and store it back in the cache.
      A good developer needs to be aware of all the various caching features of the HTTP protocol on which all Web applications are built. By using these caching features (that can help you scale by not needing to repeatedly query or compute objects when serving them from cache), you can seriously cut down on the load of your whole stack.

      Monitor and Measure Everything

      No. 4: Monitor and measure everything
      Monitor and measure everything and I do mean everything: servers, resource usage, behavior of applications, response times on pages-anything and everything relevant. As you are monitoring, collect as many metrics as you can. That way, if problems arise, you’re armed with information, performance trends and context. This is where monitoring tools designed to spot performance problems really come in handy.
      Without monitoring and logging, you lack visibility into the system. If a problem does arise and you don’t have relevant metrics to fall back on, you are faced with investigating everything it could possibly be-sounds pretty inefficient, huh?
      No. 5: Make your staging environment a faithful replica of your production environment
      Many developers develop and test their applications locally and then jump the gun by deploying straight to production. They then face problems, because the actual production environment acts differently from their laptop.
      The closer your staging and quality assurance environments are to your deployment environment, the better. The staging environment doesn’t have to be as big, but it should at least be running the same versions of all the software. Ideally, tests should also be run with a copy of the production data set to mimic the conditions of deployment. The big benefit here is catching errors during staging before the application is pushed to production; it will save you time, energy and stress later on.
      Ruby on Rails gets you to your endpoint faster and gives you time to think about how you can scale your application. But it won’t do the job for you. By keeping these five points in mind, your application will scale while your management headaches shrink.

      Ezra Zygmuntowicz is a co-founder and developer at Engine Yard. Ezra has been active in the Ruby community for over four years and has contributed to many open-source projects, including Rails, Merb, Rack and Rubinius. Ezra is also co-author of Deploying Rails Applications: A Step-by-Step Guide (published by The Pragmatic Programmers). He speaks at Ruby, Rails and cloud computing-related events throughout the United States. He can be reached at ez@engineyard.com.

      Ezra Zygmuntowicz
      Ezra Zygmuntowicz
      Ezra Zygmuntowicz is a co-founder and developer at Engine Yard. Ezra has been active in the Ruby community for over four years and has contributed to many open-source projects, including Rails, Merb, Rack and Rubinius. Ezra is also co-author of Deploying Rails Applications: A Step-by-Step Guide (published by The Pragmatic Programmers). He speaks at Ruby, Rails and cloud computing-related events throughout the United States.

      Get the Free Newsletter!

      Subscribe to Daily Tech Insider for top news, trends & analysis

      Get the Free Newsletter!

      Subscribe to Daily Tech Insider for top news, trends & analysis

      MOST POPULAR ARTICLES

      Artificial Intelligence

      9 Best AI 3D Generators You Need...

      Sam Rinko - June 25, 2024 0
      AI 3D Generators are powerful tools for many different industries. Discover the best AI 3D Generators, and learn which is best for your specific use case.
      Read more
      Cloud

      RingCentral Expands Its Collaboration Platform

      Zeus Kerravala - November 22, 2023 0
      RingCentral adds AI-enabled contact center and hybrid event products to its suite of collaboration services.
      Read more
      Artificial Intelligence

      8 Best AI Data Analytics Software &...

      Aminu Abdullahi - January 18, 2024 0
      Learn the top AI data analytics software to use. Compare AI data analytics solutions & features to make the best choice for your business.
      Read more
      Latest News

      Zeus Kerravala on Networking: Multicloud, 5G, and...

      James Maguire - December 16, 2022 0
      I spoke with Zeus Kerravala, industry analyst at ZK Research, about the rapid changes in enterprise networking, as tech advances and digital transformation prompt...
      Read more
      Video

      Datadog President Amit Agarwal on Trends in...

      James Maguire - November 11, 2022 0
      I spoke with Amit Agarwal, President of Datadog, about infrastructure observability, from current trends to key challenges to the future of this rapidly growing...
      Read more
      Logo

      eWeek has the latest technology news and analysis, buying guides, and product reviews for IT professionals and technology buyers. The site’s focus is on innovative solutions and covering in-depth technical content. eWeek stays on the cutting edge of technology news and IT trends through interviews and expert analysis. Gain insight from top innovators and thought leaders in the fields of IT, business, enterprise software, startups, and more.

      Facebook
      Linkedin
      RSS
      Twitter
      Youtube

      Advertisers

      Advertise with TechnologyAdvice on eWeek and our other IT-focused platforms.

      Advertise with Us

      Menu

      • About eWeek
      • Subscribe to our Newsletter
      • Latest News

      Our Brands

      • Privacy Policy
      • Terms
      • About
      • Contact
      • Advertise
      • Sitemap
      • California – Do Not Sell My Information

      Property of TechnologyAdvice.
      © 2024 TechnologyAdvice. All Rights Reserved

      Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.

      ×