This website uses cookies to better the user experience of its visitors. Where applicable, this website uses a cookie control system, allowing users to allow or disallow the use of cookies on their computer/device on their first visit to the website. This complies with recent legislative requirements for websites to obtain explicit consent from users before leaving behind or reading files such as cookies on a user’s computer/device. To learn more click Cookie Policy.

Privacy preference center

Cookies are small files saved to a user’s computer/device hard drive that track, save, and store information about the user’s interactions and website use. They allow a website, through its server, to provide users with a tailored experience within the site. Users are advised to take necessary steps within their web browser security settings to block all cookies from this website and its external serving vendors if they wish to deny the use and saving of cookies from this website to their computer’s/device’s hard drive. To learn more click Cookie Policy.

Manage consent preferences

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
Cookies list
Name _rg_session
Provider rubygarage.org
Retention period 2 days
Type First party
Category Necessary
Description The website session cookie is set by the server to maintain the user's session state across different pages of the website. This cookie is essential for functionalities such as login persistence, ensuring a seamless and consistent user experience. The session cookie does not store personal data and is typically deleted when the browser is closed, enhancing privacy and security.
Name m
Provider m.stripe.com
Retention period 1 year 1 month
Type Third party
Category Necessary
Description The m cookie is set by Stripe and is used to help assess the risk associated with attempted transactions on the website. This cookie plays a critical role in fraud detection by identifying and analyzing patterns of behavior to distinguish between legitimate users and potentially fraudulent activity. It enhances the security of online transactions, ensuring that only authorized payments are processed while minimizing the risk of fraud.
Name __cf_bm
Provider .pipedrive.com
Retention period 1 hour
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to support Cloudflare Bot Management. This cookie helps to identify and filter requests from bots, enhancing the security and performance of the website. By distinguishing between legitimate users and automated traffic, it ensures that the site remains protected from malicious bots and potential attacks. This functionality is crucial for maintaining the integrity and reliability of the site's operations.
Name _GRECAPTCHA
Provider .recaptcha.net
Retention period 6 months
Type Third party
Category Necessary
Description The _GRECAPTCHA cookie is set by Google reCAPTCHA to ensure that interactions with the website are from legitimate human users and not automated bots. This cookie helps protect forms, login pages, and other interactive elements from spam and abuse by analyzing user behavior. It is essential for the proper functioning of reCAPTCHA, providing a critical layer of security to maintain the integrity and reliability of the site's interactive features.
Name __cf_bm
Provider .calendly.com
Retention period 30 minutes
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to distinguish between humans and bots. This cookie is beneficial for the website as it helps in making valid reports on the use of the website. By identifying and managing automated traffic, it ensures that analytics and performance metrics accurately reflect human user interactions, thereby enhancing site security and performance.
Name __cfruid
Provider .calendly.com
Retention period During session
Type Third party
Category Necessary
Description The __cfruid cookie is associated with websites using Cloudflare services. This cookie is used to identify trusted web traffic and enhance security. It helps Cloudflare manage and filter legitimate traffic from potentially harmful requests, thereby protecting the website from malicious activities such as DDoS attacks and ensuring reliable performance for genuine users.
Name OptanonConsent
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonConsent cookie determines whether the visitor has accepted the cookie consent box, ensuring that the consent box will not be presented again upon re-entry to the site. This cookie helps maintain the user's consent preferences and compliance with privacy regulations by storing information about the categories of cookies the user has consented to and preventing unnecessary repetition of consent requests.
Name OptanonAlertBoxClosed
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonAlertBoxClosed cookie is set after visitors have seen a cookie information notice and, in some cases, only when they actively close the notice. It ensures that the cookie consent message is not shown again to the user, enhancing the user experience by preventing repetitive notifications. This cookie helps manage user preferences and ensures compliance with privacy regulations by recording when the notice has been acknowledged.
Name referrer_user_id
Provider .calendly.com
Retention period 14 days
Type Third party
Category Necessary
Description The referrer_user_id cookie is set by Calendly to support the booking functionality on the website. This cookie helps track the source of referrals to the booking page, enabling Calendly to attribute bookings accurately and enhance the user experience by streamlining the scheduling process. It assists in managing user sessions and preferences during the booking workflow, ensuring efficient and reliable operation.
Name _calendly_session
Provider .calendly.com
Retention period 21 days
Type Third party
Category Necessary
Description The _calendly_session cookie is set by Calendly, a meeting scheduling tool, to enable the meeting scheduler to function within the website. This cookie facilitates the scheduling process by maintaining session information, allowing visitors to book meetings and add events to their calendars seamlessly. It ensures that the scheduling workflow operates smoothly, providing a consistent and reliable user experience.
Name _gat_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _gat_UA-* cookie is a pattern type cookie set by Google Analytics, where the pattern element in the name contains the unique identity number of the Google Analytics account or website it relates to. This cookie is a variation of the _gat cookie and is used to throttle the request rate, limiting the amount of data collected by Google Analytics on high traffic websites. It helps manage the volume of data recorded, ensuring efficient performance and accurate analytics reporting.
Name _ga
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga cookie is set by Google Analytics to calculate visitor, session, and campaign data for the site's analytics reports. It helps track how users interact with the website, providing insights into site usage and performance.
Name _ga_*
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga_* cookie is set by Google Analytics to store and count page views on the website. This cookie helps track the number of visits and interactions with the website, providing valuable data for performance and user behavior analysis. It belongs to the analytics category and plays a crucial role in generating detailed usage reports for site optimization.
Name _gid
Provider rubygarage.org
Retention period 1 day
Type First party
Category Analytics
Description The _gid cookie is set by Google Analytics to store information about how visitors use a website and to create an analytics report on the website's performance. This cookie collects data on visitor behavior, including pages visited, duration of the visit, and interactions with the website, helping site owners understand and improve user experience. It is part of the analytics category and typically expires after 24 hours.
Name _dc_gtm_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _dc_gtm_UA-* cookie is set by Google Analytics to help load the Google Analytics script tag via Google Tag Manager. This cookie facilitates the efficient loading of analytics tools, ensuring that data on user behavior and website performance is accurately collected and reported. It is categorized under analytics and assists in the seamless integration and functioning of Google Analytics on the website.

What if I Tell You That Ruby on Rails Is Scalable

  • 80247 views
  • 11 minutes
Sviatoslav A.

Sviatoslav A.

Copywriter

Vlad V.

Vlad V.

Chief Executive Officer

Share

Ruby on Rails is a great framework for startups, but we often hear people talk about Rails scalability issues when a startup project grows too large (read: becomes very popular).

One of the key events that triggered the discussion that Rails can't scale was when Twitter switched to Scala in order to handle their growing number of user requests.

But as a counterexample, we would like to mention that Shopify is an advanced Rails application that has scaled quite well many years in a row. So who’s right? Do you need to ditch this framework if your app goes big?

Let's take a look at how to scale a Ruby on Rails application to find out.

What Is Rails Scalability?

A framework’s scalability is the potential for an application built with this framework to be able to grow and manage more user requests per minute (RPM) in the future. Actually, it's incorrect to talk about framework scalability, because it's not the framework that must, or can, scale, but rather the architecture of the entire server system. But we should acknowledge that, to a certain extent, application architecture does have an impact on scalability.

In short, telling 'How to scale Ruby on Rails' or 'Ruby on Rails scalability issues' is basically incorrect.

Let's take a look at the example below. This is what the entire server architecture looks like at the very beginning of a Rails project.

Rails scalability - single server architecture

What we usually have is a single server, on which we install the following software:

  • Nginx server;
  • Rack application server – Puma, Passenger, or Unicorn;
  • Single instance of your Ruby on Rails application;
  • Single instance of your database; usually, a relational database like PostgreSQL.

This server computer will be able to cope with, say, 1,000 or even up to 10,000 requests per hour easily. But let’s assume that your marketing is very successful, and your Rails application becomes much more popular; your server starts getting 10 or 100 times more requests. When the load increases to a high enough level, a single server architecture cracks under pressure. That is, the application becomes unresponsive to users.

That’s why we’ll explain how to resolve this scalability issue – serving data to users – with Ruby on Rails.

Let's Scale Your Ruby on Rails Application!

Vertical Scalability with Ruby on Rails

Scaling vertically is the simplest way to make the server handle an increased number of RPMs. Vertical scaling means adding more RAM, upgrading the server’s processor, etc. In other words, you give your server computer more power. Unfortunately, this approach doesn’t work in many situations, and here are a couple of reasons why.

Vertically scaling a server running an app gives a positive effect only at the initial stage. When your traffic increases yet again, you eventually come to the point when upgrading the processor or adding more RAM is technically impossible.

But there’s another downside to this approach. When you need to scale your Ruby on Rails application, there are usually some parts that demand more computational resources than others. For example, Facebook needs servers offering different performance for updating the news feed and processing images. Image processing is used less frequently than the news feed. Therefore, Facebook installs a less powerful server for image processing than for the news feed. We’ll talk more about this architectural approach (Service-Oriented Architecture or SOA) to scaling Rails app shortly.

When vertical scalability is no longer practical, or when we look for other scalability options right away, we scale a Rails application horizontally.

Horizontal Scalability with Ruby on Rails

We can scale a Rails application horizontally similarly to how we scale with many other frameworks. Horizontal scaling means converting the single server architecture of your app to a three-tier architecture, where the server and load balancer (Nginx), app instances, and database instances are located on different servers. In such a way, we allocate equal and smaller loads among machines.

Rails scalability - three-tier server architecture

Nginx

Nginx, a commonly used server software for Rails applications, is deployed on a single machine to serve as a load balancer and reverse-proxy. You need a medium-powered server for Nginx, because this server requires little computing power to function normally under high loads. Nginx’s sole aim is to filter and distribute the load among multiple servers.

We set up this server to receive the initial request and forward it to the first machine. The second request will be sent from Nginx to the second machine, and so on. When you have only three machines with your Rails application instances, then the fourth request from the client (browser) will be sent, naturally, to the first machine again.

App Instances

As we mentioned previously, you need additional servers to run app instances separately from the Nginx server. From the user’s perspective, the app stays the same; they simply access different app instances thanks to Nginx.

For communication between the application and Nginx, we use a special interface called Rack, which is an application server. There are many application servers for Rails-based apps, the best known being Unicorn, Phusion Passenger, and Puma. Application servers are responsible for input-output, letting the application deal with user requests.

Although Rails applications are mostly monolithic, we can extract a functional block and use it as a standalone service. This will also lessen the load on servers.

Database Instances

Scaling a database is another important aspect of scaling a Rails application. We can deploy a database on the same server as the application to economize, but this economization has several drawbacks. When every application instance saves and retrieves data from its own database instance, then data for the entire application is spread across many machines. This is very inconvenient for website users. If Nginx redirects a user to a different app instance than where their data is stored, they can’t even sign in because their data is located on a different machine!

Another reason to separate a database from other servers is to create a fault-tolerant architecture. If our database receives too many requests and can’t manage them, it collapses. But other database instances in the data tier can accommodate this load, and the Rails app will continue to work.

That’s why, when you are ready to scale your Rails application, your database should be transferred to its own server right away. The database tier can be a single server with a database that’s used by all app instances. Or, this tier can consist of several machines each running databases. In order to update data across all databases, we use database replication. Once a database has new data to share, it informs all others about changes (which is standard procedure for multi-master relationships among databases).

In a master-slave replication variant, databases communicate with the master (or main) database, which stores important data about other databases. All other database instances receive updates only when the master database updates data. Multi-master database replication and master-slave replication are the two common approaches to making our data layer consistent across multiple databases.

When the database receives a large quantity of data, it must save it quickly, update its status quickly, and send new data back to the user. An ordinary relational MySQL database is usually replaced by PostgreSQL or MongoDB databases to accommodate large quantities of data when you scale an app.

In order to further reduce the load on the database, several other techniques can be used. Caching is one important solution, although it’s indirectly related to database scalability. Caching provides cached data from a storehouse to a client more quickly. Imagine that your Rails app compiles statistics about user activity, such as user sessions. This produces a high load on the CPU. We can calculate this information once per day, and then show the cached data to the user at any time.

The other two solutions for reducing the load on a database are Redis and Memcached, which are special databases that perform read/write operations extremely fast.

Other Options when Scaling a Rails App

Let's say you want to spend as little money as possible on new servers. Is it possible to scale your Rails application while minimizing server costs? Indeed it is.

First, consider code optimization. Let's assume your service offers image editing, which is demanding on the the CPU. Given that your startup has evolved very quickly, it’s almost a given that your image editing algorithm wasn't optimized in its initial form, so there’s a lot of code to refactor and optimize. For example, at RubyGarage we needed to parse a huge CSV file with a user data. It took as much as 74 seconds for the algorithm to parse the file. But when our developer removed unnecessary checks and reused some resources, the algorithm took only 21 seconds to accomplish the same task. Code optimization helps you get more out of existing server resources.

Another option is to re-write computationally intensive tasks in a low-level language, such as C++. Due to low concurrency support – because of the Matz Ruby Interpreter (MRI) – Ruby isn't so great for computation compared to low-level programming languages.

It's also possible to stick with Ruby and Ruby on Rails in your project, but deploy a service-oriented architecture in order to scale your application. Imagine your project uses chat functionality (as one of our client’s apps does). The chat can be loaded with many requests from users, while the other parts of this application are used infrequently. It's unnecessary to break the server structure into three tiers. But it's possible to break the application into two parts!

Service-Oriented Architecture with Ruby on Rails

We deployed the application part responsible for chat functionality on a separate server as a stand-alone application. Chat could then communicate with the original application and database, but the load on the main server was decreased. Further, it will be easier, faster, and less expensive to scale just this small part of your application – either vertically or horizontally – should the load increase once more. The service-oriented architecture is a great solution for scaling Rails application and is used by many companies, including Facebook and Shopify.

Ruby on Rails Application Architecture and Scalability

We mentioned before that the Rails application architecture can negatively affect scalability. Scalability issues become apparent if an application consists of many services that cannot run separately from other services.

Let's consider an example of a bad Rails application architecture. An application can constantly save its current status when a user enters new data in a form or visits several web pages successively. If the load balancer redirects this user to the next server, this next server won't know anything about this new data. This will prevent us from scaling the app horizontally. The application’s status must be saved either on the client (browser) or on the database.

If we avoid this common architectural mistake, or similar mistakes, when building a application, we will be able to scale it with no trouble.

Tools that help you scale Rails apps

Proper scalability of a Rails application is only possible when you find and eliminate bottlenecks. Finding bottlenecks is a bit difficult to do manually. And we can’t figure out scalability issues by just scratching our heads.

A much more effective way to locate problems when scaling applications is to use dedicated software. New Relic, Flame Graphs, Splunk, StatsD, and other software programs are all great options, and they work similarly: each checks and collects statistics about your application. We must then interpret the gathered data to figure out what to change.

We can use Flame Graphs to learn about CPU or RAM loads when running our application. When the app interacts with the database, we can track code paths. Learning what processes take the most time can lead us to our application’s bottleneck(s). When we’ve identified a bottleneck, we can design a solution to remove it.

You can see a strangely looking graphic with vibrant colors, which we took from Flame Graph, in the screenshot below. No matter it's appearance, the graphic is genuinely helpful to track how our code works from the very beginning.

Rails scalability - Flame Graph for monitoring Rails app

In conclusion, we would like to repeat what Shopify said several years ago at a conference dedicated to Ruby on Rails scalability: “There isn't any magic formula to scaling a Rails application.”

In this article, we mentioned some problems that might appear when scaling a Ruby on Rails application, and described several solutions. But applications are unique. Therefore, you must know the characteristics of your particular application in order to overcome your applications specific limitations. It's important to know exactly what part of a Rails application to scale initially as we begin optimizing from crucial issues down to minor bottlenecks.

CONTENTS

Authors:

Sviatoslav A.

Sviatoslav A.

Copywriter

Vlad V.

Vlad V.

Chief Executive Officer

Rate this article!

Nay
So-so
Not bad
Good
Wow
27 rating, average 4.74 out of 5

Share article with

Comments (2)
Ashish Garg
Ashish Garg almost 6 years ago
Great Article. Thanks!
Reply
Anastasia Z.
Anastasia Z. almost 6 years ago Ashish Garg
Hi, Ashish! Thanks for your appreciation ;)
Reply

Subscribe via email and know it all first!