Skip to content

Instantly share code, notes, and snippets.

@freeformz
Forked from ryandotsmith/hack-reactor.md
Last active December 12, 2015 08:28
Show Gist options
  • Save freeformz/4743788 to your computer and use it in GitHub Desktop.
Save freeformz/4743788 to your computer and use it in GitHub Desktop.

Tales From a Heroku User

Here are some things I have learned along the way.

Last Updated: 2013-02-08

Original Audience: Hack Reactor

About

  • @ryandotsmith
  • Engineer at Heroku for almost 3 years.
  • Programming since university.
  • Studied Math.
  • Built armature radios in junior high.

Things I have worked on

  • The Heroku API
  • Message queues (e.g. queue_classic)
  • Heroku's usage & billing system
  • DNS at Heroku
  • Log delivery and visibility tooling (e.g. l2met, log-shuttle)

Technologies that I spend considerable time with

  • Heroku
  • PostgreSQL
  • Redis
  • Ruby
  • Go
  • UNIX

Heroku

Things Heroku makes trivial:

  • Keeping a process alive.
  • Keeping your data alive.
  • Routing HTTP requests to more than one process.
  • Adding new processes and new apps.
  • SSL Termination.
  • Operating System Maintenance.
  • Log routing.
  • Security.

At one time in our history, everyone made their own electricity.

img

A dynamo that would power a business. The business would employ people to build and maintain the dynamo. Dyanmos were abandoned as they were neither safe or economical. I feel that this is similar to servers and Heroku.

How Heroku works

This is a great time for questions. Stop me at any point and we can take a deep dive into a particular concept or system.

img

How to design your app knowing how Heroku works

Stateless & Uncoordinated

  • Processes are distributed
  • Your database might be distributed
  • l2met example

img

Web Processes are Precious

  • Handle work in the background
  • worker pattern example

img

12 Factor

12factor.net

  • Good for any app
  • Forced if running on Heroku

Workflow

I find that Heroku is most powerful when you learn to embrace the UNIX toolkit.

Config

There are no configuration files. Files are clunky and hard to manage, the unix way of dealing with config is to use the UNIX environment memory.

$ man env

Grabbing the config from your Heroku app.

$ heroku config -s > .env

Loading your config locally.

$ source .env

Copying config from one app to another.

$ heroku config:set $(heroku config -s)

Log Analysis

Don't think of your logs as static files. Think of the logs as live streams of your app's health and status.

$ ruby -e 'puts "at=hack-reactor"'

Counting the number of HTTP requests per second.

$ heroku logs -t -p router | pv -lr >/dev/null

Sampling your response times.

$ heroku logs -t -p router | awk '{print $10,$11,$12}'

Using tools like log-shuttle and l2met we can build charts of system metrics.

img

Interacting with your database

A database provided by the Heroku addons catalog is just a database. With Heroku Postgresql, you can use standard UNIX tools to interact with it.

Connecting to your database.

$ export url=$(heroku config:get DATABASE_URL)
$ psql $url

Using your editor to build queries.

$ export PSQL_EDITOR="vim -f -c ':set ft=sql'"
$ psql $url
database - \e

Similar feature available in bash.

$ echo 'hello world' | grep 'hello' | sed 's/hello/bye'
$ fc

Writing query results to your local filesystem.

$ echo 'select 1 \g out.txt' | psql $url
$ cat out.txt

Getting Real

It is clear that Heroku is the best place to develop your application, but it is also a great place to run and scale your application. A couple of things to keep in mind when you grow your traffic and your uptime.

img

  • Choose the right ratio of work between your process and your database.
  • Loading functions in your database is OK. Be aware that it won't scale horizontally.
  • Scaling horizontall can help increase throughput when your latencies are high.
  • Think about latency vs. throughput and how to balance the two.

Maximizing the uptime of your Heroku app

img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment