« HE:labs
HE:labs

Performance Protips

Postado por Flavia Fortes em 18/12/2013

Hi, everyone!

This is my first post in English. A few weeks ago we received Ali to work with us as you can see here. Ali is from Azerbaijan and we only communicate in English. Now that we have him, we are trying our best to talk and write in English most of the time. And it's being an awesome experience. <3

So, inspired by this situation I decided that I would write the first English post on HE:labs blog. Let's see how it goes.

I implemented on one of our projects here at HE:labs a sequence of complex calculations. After pushing these changes to the production environment, the website had lots of access which caused on an application error in Heroku. The complex calculations being generated by several users at the same time caused a server timeout.

As a programmer who is still learning how to code and how to deal with this kind of performance issues, I immediately saw an opportunity to grow. I asked help for more experienced programmers and now I'm gonna show you what I learned.

I discovered an amazing tool called Rollbar which catches errors and gives you information about it. This helps on discovering what happened and fixing it. It emails you whenever an exception occurs. This way, you can discover an error and fix it before your client calls you to warn you that some of his users complained. You anticipate that. Nice, uh? After this lesson, I put Rollbar in all of my projects. And I truly can't see myself working in any project that doesn't have this kind of 24/7 analyzer. Here at HE:labs we also use Airbrake and we recommend it.

To put the rollbar add-on on your Heroku app:

1 $ heroku addons:add rollbar --app yourapp

I also started to use New Relic to monitor app performance. It's another amazing tool that we recommend. If you don't know New Relic yet, it's a monitoring service which will watch your application health and availability and provide information about the root cause of app performance issues. In my case I was able to see which exactly were the queries that were taking so long to respond. They have these awesome graphics that show you which action or view needs some optimization.

To put the newrelic add-on on your Heroku app:

1 $ heroku addons:add newrelic --app yourapp

The first thing I thought when the performance issue happened was that I would have to simulate the timeout on the staging environment. But to accomplish that I would have to clone the production database. I was using postgres on Heroku. That is what I have done and I will show this Heroku protip for you.

You are going to need the pg:backup add-on on both of your apps:

1 $ heroku addons:add pgbackups

This next command line will create a database backup of your production app

1 $ heroku pgbackups:capture --expire --app production-app

After this, you will use the pg:backups:restore on you staging app:

1 $ heroku pgbackups:restore HEROKU_POSTGRESQL_TURQUOISE -a staging-app \
2     `heroku pgbackups:url -a production-app`

Be aware of which color is the postgres database of your staging app, in this example was TURQUOISE.

In this particular case I also cloned the database to my local environment, like this:

After capturing the database shot, I downloaded it:

1 $ curl -o latest.dump `heroku pgbackups:url`

And restored it locally:

1 $ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U youruser -d yourdb latest.dump

After all that, we had to do some refactoring. Mauro did a lot of changes using Ruby Memoization and solving N+1 queries. I can talk about that in the next post. What do you guys think? Let me know!

I hope this can help you in some manner and I expect to improve my English and come back writing soon enough.

Hugs!

Compartilhe

Sabia que nosso blog agora está no Medium? Confira Aqui!