Optimizing Resource Costs
Last updated October 11, 2024
Table of Contents
Heroku prorates all costs to the second for the resources that you use. This article provides guidance on how to optimize Heroku resources and minimize costs for different use cases.
Optimizing for Prototypes or Heroku Buttons
Use the least expensive plans available to you when you’re building prototypes and experimenting with your app. For example, Eco dynos sleep after 30 minutes of inactivity and don’t consume hours while sleeping.
Only personal apps can use Eco dynos. Personal apps are apps that aren’t a part of a Heroku Team or Enterprise Team.
If you have apps that rely on app.json
to deploy, such as apps deployed through Heroku Buttons or set up with the Platform API, specify the appropriate plan within the app.json
. Heroku Buttons already default to the least expensive plans.
{
"formation": {
"web": {
"quantity": 1,
"size": "basic"
}
}
}
When you’re done experimenting with your prototype, make sure you:
- Scale down your dynos so they don’t stay running
- Remove or delete any database resources that are no longer in use. You can export Heroku Postgres data that you want to keep.
- Downgrade or delete any third-party add-ons from your app
Optimizing for Review Apps and CI Apps
Review Apps default to using the least expensive dyno available to you. You can optimize costs for your Review Apps by:
- Setting Review Apps to be destroyed automatically after 1, 2, 5, 14, or 30 days of inactivity
- Manually deleting any Review Apps as soon as you’re done testing
- Using In-Dyno databases for Heroku Postgres and Heroku Key-Value Store for Heroku CI. In-Dyno databases run inside your test-run’s dynos and are ephemeral, so they provide great flexibility and speed for test runs in your CI workflow.
- Specifying low-cost plans for add-ons in the
app.json
. If your Review Apps or CI appears to ignore your app.json file, contact Heroku Support.
Optimizing for Staging Apps
Staging apps are used to test changes in a production-like setting before promoting to production. It’s important to adjust the resources while testing so you’re not using more than necessary.
Upgrade to larger resource plans as necessary when you’re:
- Testing major code changes
- Executing database migrations
Staging apps incur costs for resources used. Optimize the resources in your staging apps by:
- Load testing to see how it performs under real-world traffic. Use these results to help determine appropriate scaling on your production apps.
- Scaling down the dynos when you’re done testing
- Using a third-party add-on from the Elements marketplace to put the dynos to sleep when they’re inactive
- Using Heroku Scheduler to run one-off dynos at scheduled intervals. Dynos are automatically turned off after the task is completed.
Optimizing for Production Apps
Optimizing Dynos
Consider optimizing your dyno usage so your app makes the most efficient use of available resources. Depending on factors like how much CPU or memory your app uses, different dyno types are better suited.
It’s a good idea to monitor your apps and databases to optimize your usage. Adding an application performance monitoring (APM) add-on, such as New Relic or Librato can monitor your resource usage and collect metrics over time.
Scale down your dynos when they’re not needed by:
- Manually scaling your app’s dynos through the Heroku Dashboard or the CLI
- Enabling autoscaling on Performance, Private and Shield dynos after optimizing dyno performance
- Using a third-party add-on from the Elements marketplace
Optimizing Heroku Postgres
Choose the right Heroku Postgres plan depending on your app’s usage and requirements for availability and uptime. The right plan depends on how much downtime your app can tolerate on a monthly basis. You can also consider using Terraform to create and remove your apps on demand without wasting resources.
You can further improve the performance of your Heroku Postgres database by knowing your specific workload and doing performance tuning. You can also consider enabling WAL compression. Keep in mind, enabling this feature reduces Write-ahead Logging (WAL) volume at the cost of a slight increase in CPU usage.
Steps to optimize the performance of your database include:
- Tracking system metrics on your application’s logs
- Controlling the number of connections to your database and using connection pooling when appropriate to avoid connection limits and Out of Memory errors.
- Identifying under-optimized queries
- Watching out for locks in the database
- Cleaning up dead rows in the database periodically
- Considering separate caching
- Running the heroku
pg:diagnose
command to find common issues
You can learn more in Heroku Postgres Database Tuning.
How to Track Your Usage and Costs
- For non-Heroku Enterprise accounts, track your current usage and costs on your account’s Billing tab.
- For Enterprise accounts, track your current usage and costs on the Usage tab on your Enterprise Team page.