Web Application
Deployment: Heroku
   Copyright © 2025 by
 Robert M. Dondero, Ph.D
   Princeton University
                           1
                 Objectives
•   This lecture will cover:
    – How to deploy a web app and database to
      Heroku
                                                2
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               3
             Deployment Options
•   Question:
    –   Where/how might you run Penny app so it’s
        constantly available to the world…
        •   Using a production-quality HTTP server?
        •   Using a production-quality DBMS?
                                                      4
         Deployment Options
•   Answer 1: Your computer
                              5
     Question (11deployheroku)
•   Why would it be unreasonable to make your app
    publicly available on your computer? Choose
    all that apply:
    –   (a) Your computer would need to be turned on
        all/most of the time
    –   (b) Your computer would need to be connected to the
        Internet all/most of the time
    –   (c) Your computer would need to be powerful enough
        to handle a potentially large volume of requests
    –   (d) You would need sys admin skills to install &
        configure a production-quality web server & DBMS
•   Browse to https://cos333attend.cs.princeton.edu
    to answer
                        [1 point]                        6
             Deployment Options
•   Answer 2: Princeton CS Dept HTTP
    servers
    –   See
        https://csguide.cs.princeton.edu/publishing/w
        ebpages
    –   Provides MySQL databases
    –   Pros and cons:
        •   (pro) XXX.cs.princeton.edu address
        •   (pro) Free
        •   (con) Requires CS Dept approval
        •   (con) No access to server logs
                                                    7
             Deployment Options
•   Answer 3: Princeton OIT servers
    –   Pros and cons:
        •   (pro) Free
        •   (con) Requires OIT approval
        •   (con) Requires sponsorship of some Princeton
            department
        •   (con) Difficult; requires intense cooperation with
            OIT
                                                                 8
             Deployment Options
•   Answer 4: Commercial cloud service on
    your own
    –   Render.com, Heroku, DigitalOcean, ...
    –   Pros and cons:
        •   (pro) The service does sys admin for you
        •   (con) You may need to pay for it!
    –   The best option for most COS 333 projects
                                                       9
            Deployment Options
–   But which cloud service?
–   Must choose a service for:
    •   Web application
    •   Database
                                 10
         Deployment Options
A little COS 333 history:
Historical Phase   Application Cloud Service        DB Cloud Service
        1          Heroku (free forever)            Heroku (free forever)
                       Heroku eliminated its free tier
        2          Render (free forever)            Render (free for 3 mths)
        2          Render (free forever)            ElephantSQL (free forever)
         Heroku teamed with GitHub: GitHub Student Developer Pack
        3          Render (free forever)            Render (free for 3 mths)
        3          Render (free forever)            ElephantSQL (free forever)
        3          Heroku with GitHub Student       Heroku with GitHub Student
                   Developer Pack (free for 1 yr)   Developer Pack (free for 1 yr)
                                                                                     11
       Deployment Options
A little COS 333 history (cont.):
 Historical Phase   Application Cloud Service         DB Cloud Service
                    Render limited its free tier DBs to 1 month
                    ElephantSQL went out of the DB business
         4          Render (free forever)             Render (free for 1 mth)
         4          Render (free forever)             Neon (free forever)
         4          Heroku with GitHub Student        Heroku with GitHub Student
                    Developer Pack (free for 1 yr)    Developer Pack (free for 1 yr)
             Heroku extended GitHub Student Developer Pack to 2 yrs
         5          Render (free forever)             Render (free for 1 mth)
         5          Render (free forever)             Neon (free forever)
         5          Heroku with GitHub Student        Heroku with GitHub Student
                    Developer Pack (free for 2 yrs)   Developer Pack (free for 2 yrs)
                                                                                        12
      Deployment Options
Web app cloud services:
  Web App                  Cost
  Cloud Service
  Render.com Free          Free for 750 hours of running time per
                           month *
  Render.com Starter       $7/month
  Heroku Eco               $5 for 1000 dyno hours per month **
  Heroku Basic             ~$0.01 per hour (max of $7 per month)
  * Slow deployment; slow at runtime?
  ** GitHub Student Developer Pack provides $13/month
     for 2 years; cannot renew
                                                                    13
       Deployment Options
Database cloud services (cont.):
  DB Cloud         Time        Size    Concurrent       Cost
  Service          Period              Connections
  Render.com Free 1 month *    1 GB    97               0
  Render.com       Unlimited   1 GB    97               $7/month
  Starter
  Neon Free Plan   Unlimited   0.5GB   10000            0
  Neon Launch      Unlimited   10GB    10000            $19/month
  Heroku Mini      Unlimited   1 GB    20               $5/month **
  Heroku Basic     Unlimited   10 GB   20               $10/month
  * Can create another
  ** GitHub Student Developer Pack provides $13/month
     for 2 years; cannot renew
                                                                    14
         Deployment Options
•   All of those DB options (can) use
    PostgreSQL
                                        15
Deployment Options
             Michael
             Stonebreaker
                            16
             Deployment Options
•   PostgreSQL assessment (vs. SQLite)
    –   (con) Setup on local computer (not shown) is
        much more complex
    –   (con) Setup on cloud service is more complex
    –   (pro) Production-quality
        •   Distinct process
        •   Authentication
        •   Row-level (vs. database-level) locking
        •   Reasonable for apps deployed to the cloud
        •   …
                                                        17
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               18
        Heroku
Orion   Adam      James
Henry   Wiggins   Lindenbaum
                               19
                      Heroku
•   Render
    –   Can create app or DB first
•   Heroku
    –   Must create app first
    –   Then create DB specifically for the app
                                                  20
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               21
            Defining the App
•   Deployment
                       Recommended
         Heroku            Heroku
                                deploy
                           GitHub
                                push
      Local Computer    Local Computer
                                         22
           Defining the App
•   You must change names as appropriate…
                                        23
                 Defining the App
•   After performing setup steps in Git and
    GitHub Primer document...
•   Create a GitHub repo
    –   Browse to https://github.com
        •   Click the New button
            – For Repository name enter pennyheroku
            – Click the Private radio button
            – Check the Add a README file check box
            – Click the Create Repository button
                                                      24
               Defining the App
•   Clone the GitHub repo to my local
    computer
    $ git clone https://github.com/rdondero/pennyheroku.git
    –   Creates pennyheroku dir on local computer
                                                         25
                 Defining the App
•   Create these files in pennyheroku dir:
    –   From the PennyFlaskJinja app
        •   runserver.py (optionally)
        •   penny.sql
        •   penny.sqlite (optionally)
        •   book.py
        •   header.html, footer.html, index.html,
            searchform.html, searchresults.html
        •   penny.py
    –   Described later in this lecture
        •   database.py
                                                    26
                 Defining the App
•   Also create in pennyheroku dir:
    –   requirements.txt
             Flask
             psycopg2
             SQLAlchemy
             python-dotenv
             gunicorn
        •   Tells Heroku what additional modules the app uses
        •   Create manually, or issue command
            python –m pip freeze >
            requirements.txt
                                                           27
           Aside: Gunicorn
                                 WSGI            Python
                 HTTP
Browser                          HTTP            WSGI
                 Server
                                 Server          App
          HTTP            HTTP            WSGI
Chrome           Nginx       Gunicorn            Flask
Firefox          Apache      Phusion             Django
                              Passenger
                                                          28
                  Defining the App
•   Also create in pennyheroku dir:
    –   Procfile
        web: gunicorn penny:app
        •   Tells Heroku the app’s process type
            –   It’s a web app
        •   Tells Heroku the command to start the process
            –   Gunicorn (https://gunicorn.org/)
                                                            29
                 Defining the App
•   Also create in pennyheroku dir:
    –   runtime.txt
         python-3.12.3
        •   Tells Heroku which version of Python it should use
                                                            30
              Defining the App
•   Stage the app to the local git repo, commit
    the app to the local git repo, and push it to
    the GitHub repo
        $   cd pennyheroku
        $   git add .
        $   git commit -m "initial load"
        $   git push origin main
                                               31
                Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               32
                Deploying the App
•   Create a Heroku account
    –   Browse to http://signup.heroku.com
        •   Follow the instructions
            –   Need not provide a credit card number
    –   Link your Heroku account to the GitHub
        Student Developer Pack
        •   Procedure unknown
                                                        33
               Deploying the App
•   Log into Heroku
    –   Browse to https://id.heroku.com/login
        •   For Email address enter
            rdondero@cs.princeton.edu
        •   For Password enter yourpassword
                                                34
               Deploying the App
•   “Install Heroku” on GitHub
    –   Inform GitHub that Heroku is permitted to
        access the repo
    –   Browse to
        https://github.com/apps/heroku/installations/new
        •   Click rdondero
                                                      35
               Deploying the App
•   “Install Heroku” on GitHub (cont.)
    –   In the resulting Heroku page
        •   Click Only select repositories
        •   Click Select repositories
        •   Choose rdondero/pennyheroku
        •   Click Save
                                             36
             Deploying the App
•   (If necessary) Browse to Heroku
    dashboard
    –   Browse to
        https://dashboard.heroku.com/apps
    –   Click Dashboard
                                            37
               Deploying the App
•   Create the app
    –   In resulting page:
        •   Click New → Create new app button
    –   In resulting page:
        •   For App name enter pennyheroku
        •   For Choose a region choose United States
        •   Click Create app button
                                                       38
               Deploying the App
•   Configure the app
    –   In resulting page:
        •   Click GitHub: Connect to GitHub
        •   For Search for repository to connect to enter
            rdondero and pennyheroku
        •   Click on the Search button
        •   Click on the Connect button for
            rdondero/pennyheroku
                                                            39
               Deploying the App
•   Configure the app (cont.)
    –   In the Manual deploys area
        •   For Choose a branch to deploy select main
        •   Click the Deploy Branch button
        •   Observe the build log
                                                        40
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               41
               Creating the DB
•   In the pennyheroku page
    –   Click on the Resources tab
•   In the resulting Resources page
    –   Click on the Find more add-ons button
•   In the resulting Heroku Add-ons page
    –   Click on Heroku Postgres
                                                42
                Creating the DB
•   In the resulting Heroku Postgres page
    –   Under Plans & Pricing choose Essential 0
    –   Click on the Install Heroku Postgres button
•   In the resulting Online Order Form page
    –   As App to provision to choose pennyheroku
    –   Click on Submit Order Form
•   In the resulting pennyheroku page
    –   Click on Settings tab
    –   Click on Reveal Config Vars
        •   Note value of DATABASE_URL
        •   Save it someplace
        •   Let’s call it yourdburl
                                                 43
                Creating the DB
•   Note:
    –   Heroku reserves the right to change
        yourdburl at any time
        •   Changes DATABASE_URL in Heroku app, but…
        •   Breaks external apps
                                                       44
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               45
                    Using the DB
•   From a command-line
    –   Install psql
        •   Without installing PostgreSQL server
        •   See
            https://www.risingwave.dev/docs/current/install-psq
            l-without-postgresql/
                                                            46
                    Using the DB
•   From a command-line (cont.)
    –   Install psql (Mac)
        •   First install homebrew; then:
             $ brew update
             $ brew install libp
             $ brew link --force libpq
             $
                                            47
                   Using the DB
•   From a command-line (cont.)
    –   Install psql (MS Windows)
    –   Download the installer at
        https://www.postgresql.org/download/windows/
    –   Run the installer
        •   Select Command Line Tools and uncheck other
            options during installation
                                                          48
                 Using the DB
•   From a command-line (cont.)
    –   Install psql (Linux)
         $ sudo apt update
         $ sudo apt install postgresql-client
         $
                                                49
           Using the DB
Some psql statements
 sqlite3 Statement   psql Statement
 .help               \h
 .quit               \q
 .tables             \d
 .schema table       \d table
 .read file          \i file
                                      50
                      Using the DB
$ cat penny.sql
DROP TABLE IF EXISTS books;
CREATE TABLE books (isbn TEXT PRIMARY KEY, author TEXT, title TEXT);
INSERT INTO books   (isbn, author, title)
   VALUES ('123',   'Kernighan','The Practice of Programming');
INSERT INTO books   (isbn, author, title)
   VALUES ('234',   'Kernighan','The C Programming Language');
INSERT INTO books   (isbn, author, title)
   VALUES ('345',   'Sedgewick','Algorithms in C');
$
                                                                       51
              Using the DB
$ psql yourdburl
yourdbname=> \i penny.sql
DROP TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
yourdbname=> SELECT * FROM books;
 isbn | author    |        title
------+-----------+-----------------------------
 123 | Kernighan | The Practice of Programming
 234 | Kernighan | The C Programming Language
 345 | Sedgewick | Algorithms in C
(3 rows)
yourdbname=> \q
$
                                                   52
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               53
  Using the DB: Python
Python program
   DBAPI
                 Socket       PostgreSQL
  psycopg2
                 PostgreSQL     DBMS
                 protocol
                                    File I/O
                                Data
                                store
                                               54
          Using the DB: Python
•   Installing the psycopg2 driver:
     $ activate333
     $ python –m pip install psycopg2
     $
     or, if that fails:
     $ activate333
     $ python –m pip install psycopg2-binary
     $
                                               55
           Using the DB: Python
•   See pennyheroku/database1.py
    –   Baseline version
    –   Uses SQLite
           $ python database1.py
           123
           Kernighan
           The Practice of Programming
           234
           Kernighan
           The C Programming Language
                                         56
            Using the DB: Python
•   See pennyheroku/database2.py
    –   Uses PostgreSQL
    –   Uses an environment variable
        $ export DATABASE_URL=yourdburl
        $ python database2.py
        123
        Kernighan
        The Practice of Programming
        234
        Kernighan
        The C Programming Language
        $
                                          57
           Using the DB: Python
•   Problem:
    –   In the context of a Flask app…
    –   database2.py is too slow
                                         58
          Using the DB: Python
                     App Server
                                  Concurrent
                                  threads
                     penny.py
            HTTP     penny.py
Browser                              DB
            Server
                     penny.py
                     penny.py
                                               59
          Using the DB: Python
                     App Server
                                  Concurrent
                                  threads
                     penny.py
            HTTP     penny.py
Browser                              DB
            Server
                     penny.py
                     penny.py
                     penny.py
                     penny.py
                                               60
            Using the DB: Python
•   Solution:
    –   DB connection pooling
        •   Maintain a “pool” (queue) of open DB connections
            that threads can use
                                                           61
          Using the DB: Python
•   See pennyrender/database3.py
    –   Uses DB connection pooling
          $ export DATABASE_URL=yourexternaldburl
          $ python database3.py
          123
          Kernighan
          The Practice of Programming
          234
          Kernighan
          The C Programming Language
                                                    62
           Using the DB: Python
•   Problems???
    –   database3.py requires extra logic for
        connection pooling
    –   database3.py requires maintenance
        pgmmers to know SQL
    –   database3.py is (somewhat) specific to
        PostgreSQL
                                                 63
            Using the DB: Python
•   Solution: SQLAlchemy
    –   An Object Relational Mapper (ORM) for
        Python
        •   Maps each table to a class
        •   Maps each row to an object
    –   See optional lecture and tutorial material for
        more thorough description
                                                     64
        Using the DB: Python
•   Installing SQLAlchemy
      $ activate333
      $ python -m pip install SQLAlchemy
      $
                                           65
             Using the DB: Python
•   See pennyheroku/database.py
    –   Uses SQLAlchemy
         $ export DATABASE_URL=yourdburl
         $ python database.py
         123
         Kernighan
         The Practice of Programming
         234
         Kernighan
         The C Programming Language
                                           66
           Using the DB: Python
•   See pennyheroku/database.py (cont.)
       $ export DATABASE_URL=sqlite:///penny.sqlite
       $ python database.py
       123
       Kernighan
       The Practice of Programming
       234
       Kernighan
       The C Programming Language
       Also works with SQLite on local computer!!!
                                                      67
                 Agenda
•   Deployment options
•   Heroku
•   Defining the app
•   Deploying the app (demo)
•   Creating the DB (demo)
•   Using the DB (demo)
•   Using the DB: Python
•   Running the App (demo)
                               68
              Running the App
•   Browse to the app!
    –   https://pennyheroku-??????.herokuapp.com/
                                               69
                   Summary
•   The lecture has covered:
    –   Web app deployment options
    –   How to deploy a database and web app to
        Heroku
•   See also:
    –   Appendix 1: Render vs. Heroku
    –   Appendix 2: Cloud Service Types
                                                  70
  Appendix 1:
Render vs. Heroku
                    71
             Render vs. Heroku
•   Render
    –   (pro) Easier to create DB
    –   (pro) Easier to create app
    –   (pro) Stable DB URL
    –   (pro) Free
•   Heroku
    –   (pro) Faster deploys
    –   (pro) Faster initial app launch
    –   (pro) Faster app???
    –   (pro) DB does not expire
                                          72
                Render vs. Heroku
•   Render
    –   (con) Slower deploys
    –   (con) Slower initial app launches
    –   (con) Slower app???
    –   (con) DB expires after 1 month
         •   But easily can move data to new DB
•   Heroku
    –   (con) Harder to create app
    –   (con) Harder to create DB
    –   (con) Unstable DB URL
    –   (con) Costs money
         •   But non-renewable GitHub Student Developer Pack covers
             2 years
                                                                 73
    Appendix 2:
Cloud Service Types
                      74
            Cloud Service Types
•   Software as a Service (SaaS)
    –   “The capability provided to the consumer is to
        use the provider’s applications running on a
        cloud infrastructure.”
        -- https://en.wikipedia.org/wiki/Cloud_computing
    –   Examples: Google Docs, Microsoft Word
        Online
                                                      75
            Cloud Service Types
•   Platform as a Service (PaaS)
    –   “The capability provided to the consumer is to
        deploy onto the cloud infrastructure
        consumer-created or acquired applications
        created using programming languages, libraries,
        services, and tools supported by the provider.”
        -- https://en.wikipedia.org/wiki/Cloud_computing
    –   Examples: Render, Heroku, Google App Engine
                                                       76
            Cloud Service Types
•   Infrastructure as a Service (IaaS)
    –   “The capability provided to the consumer is to
        provision processing, storage, networks, and
        other fundamental computing resources where
        the consumer is able to deploy and run arbitrary
        software, which can include operating systems
        and applications.”
        -- https://en.wikipedia.org/wiki/Cloud_computing
    –   Examples: Amazon Web Services (AWS),
        Google Cloud Platform, Microsoft Azure
                                                       77
           Cloud Service Types
•   Which cloud service type for Penny?
    –   SaaS: impossible; too narrow
    –   IaaS: possible, but too broad
    –   PaaS: just right!
                                          78