Tips for Optimizing Apps Running in Heroku

Heroku is a popular platform-as-a-service (PaaS) cloud that allows you to run your applications in a serverless manner. This post is about optimizing applications running in Heroku: making them run faster, giving them better security, and generally fine-tuning them.

What Is Heroku?

Heroku was the front-runner in the relatively new field of PaaS providers. It was among the first companies to offer a fully serverless solution. Heroku offered developers the ability to concentrate on writing their code rather than managing and tuning the running environment of their applications. Essentially, it removed the “Ops” part from DevOps. Heroku started as a PaaS runtime for Ruby applications but quickly grew to support additional popular languages such as Java, PHP, and JavaScript. Its ecosystem grew substantially as well, with over 150 add-ons. Last but not least, most of your database needs will be taken care of as well—either by Heroku’s core offerings (PostreSQL) or via add-ons (MongoDB, Redis) as a database-as-a-service solution.

Deploying Heroku Applications

Ease of use and being developer-centric are two of the main selling points of the Heroku platform. The platform is geared toward making the developer’s life easier, and it really shows. Accordingly, deploying applications to Heroku is easy. Here’s what you need to do:

  1. Install the Heroku cli
  2. Have a git repository ready
  3. Create an Heroku app
  4. Push the code
  5. Deploy one or more Heroku Dynos (essentially containers)

And… done. Once your app is there, it’s easy to scale it to more working Dynos (one command) and apply add-ons (backups, connecting to AWS services, sending emails, etc.) as well.

Heroku Gotchas

However, this ease of use comes with a price. Since Heroku itself uses the same AWS instances behind the scenes that you would normally use if you work with AWS services directly, the total price you’ll pay is composed of:

  1. The AWS infrastructure (which itself is not cheap, and costs much more than hosting on a bare-metal server)
  2. The Heroku platform premium on those services above
  3. Cost per each any additional add-on you might use

This can stack up to a lot of money really quickly. That’s why optimizing your Heroku applications is even more important than optimizing your regular AWS/on-premise infrastructure.

Optimizing Apps Running in Heroku

Choosing the Right Dyno Type

When deploying an Heroku application, you have several Dyno types you can choose from. Choosing the right Dyno for your application needs can save you a lot of money. For instance, there’s no point in starting with an extra large Dyno when your app can do just fine with the free tier. In addition, once you need a larger Dyno, you should consider whether you need to scale up (move to a larger Dyno) or scale out (run more instances). As a rule of thumb, running more containers of the same type will be cheaper than switching to a larger type of Dyno. That said, you should definitely check for your specific use case. Let’s compare the prices of the different Dynos available on Heroku:

Heroku Plans and Costs

Heroku Plans

The Standard plan is the minimum plan you should choose if you want to deploy commercial applications to Heroku. Switching from a 512MB RAM Dyno to a Dyno with twice that amount results in additional 25$ per month per Dyno. Switching to the Performance plan, which promises stable performance for your customers, raises your cost to at least 250$ per month per Dyno.

Logging

The high premium you pay for Heroku’s convenience and time saved on DevOps work suggests (or even dictates) that you should thoroughly analyze performance issues and solve them using existing resources instead of scaling up/scaling out. Heroku comes with a centralized distributed logging facility called Logplex. It collects logs from all your working Dynos and presents them in a unified interface.

Logplex Limits

However, it is provided only as a CLI tool. And the logs appear as you might expect: not in a very developer-friendly manner.

Heroku logs

In addition, the CLI tool provides only limited filtering and searching capabilities. Debugging the root cause via historic log data is difficult and requires manually parsing the log output. This makes debugging cumbersome and time-consuming. Finding the cause of performance issues without scaling infrastructure is vitally important for your (or your employer’s) financial peace of mind, so a better solution is needed.

Dedicated Cloud Logging

Dedicated centralized logging providers such as Papertrail have a rich suite of features tailored to this and other use cases. Papertrail facilitates speedy troubleshooting by performing lightning-fast searches on your aggregated data. This reduces the level of noise in the system by allowing you to set predefined filters for unwanted data. It also has an alerts mechanism that allows you to receive real-time alerts about events (and possible problems) as they unfold. Don’t worry: this supremely useful Heroku addon is ready for you here.

Gzipping Content

Another way to optimize your performance is to gzip content. Gzipping content is a well-known best practice and a “low hanging fruit” in the world of optimizing front-end performance. By gzipping text/js/css files, you reduce the payload size to be downloaded to the client’s machine. As server configuration is not available in Heroku (we are in the world of PaaS, remember?), you should use the relevant solution for your language and framework of choice.

Background Jobs

Non-blocking is the new gold standard and the holy grail of developers worldwide. You can utilize the benefits of the non-blocking async paradigm in Heroku using background jobs. With non-blocking, a client doesn’t need to wait for a response from the server that their email was sent, for instance. Likewise, if your app needs to store data on a remote service after client’s operation (like uploading a file), this can be done behind the scenes, thus improving the user’s experience. Heroku fulfills background jobs with worker Dynos. Worker Dynos are scaled independently of web Dynos.

Keep Your Packages Updated

Whatever language you use, you probably use some open-source components as a foundation or feature fulfillment instead of developing everything from scratch. That’s usually a great time-saving approach. However, it’s important to keep those packages updated. Package updates usually provide critical security fixes (you don’t want your application hacked). Moreover, software updates sometimes provide better performance, which can in itself help lower the Heroku bill. The same can apply to your language version as well, as security vulnerabilities are found in language source code (I’m thinking mainly about PHP here).

Conclusion

In this article, we’ve explained what Heroku is and how to deploy apps to it. Then we zoomed in on the importance of optimizing apps in Heroku. Lastly, we discussed in detail the different ways to optimize Heroku apps. We also touched on the limits of Heroku’s built-in logging facility and the way in which Papertrail can help you troubleshoot fast, move quickly, and save money on your Heroku bills. Heroku is a great platform for developers, and optimizing your apps will make the experience of using it even better.

Originally written for papertrail

Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked