We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Storage

Arguably the most important part of your typical web application is the storage of data. It would be pretty useless if each time you logged into your account on YouTube, Twitter or GitHub, all of your subscriptions, tweets, or repositories were gone.

Memory vs. Disk

When you run a program on your computer (like our HTTP server), the program is loaded into memory. Memory is a lot like a scratch pad. It's fast, but it's not permanent. If the program terminates or restarts, the data in memory is lost.

When you're building a web server, any data you store in memory (in your program's variables) is lost when the server is restarted. Any important data needs to be saved to disk via the file system.

Option 1: Raw Files

We could take our user's data, serialize it to JSON, and save it to disk in .json files (or any other format for that matter). It's simple, and will even work for small applications. Trouble is, it will run into problems fast:

  • Concurrency: If two requests try to write to the same file at the same time, you'll get overwritten data.
  • Scalability: It's not efficient to read and write large files to disk for every request.
  • Complexity: You'll have to write a lot of code to manage the files, and the chances of bugs are high.

Option 2: Database

At the end of the day, a database technology like MySQL, PostgreSQL, or MongoDB "just" writes files to disk. The difference is that they also come with all the fancy code and algorithms that make managing those files efficient and safe. In the case of a SQL database, the files are abstracted away from us entirely. You just write SQL queries and let the DB handle the rest.

We will be using option 2: PostgreSQL. It's a production-ready, open-source SQL database. It's a great choice for many web applications, and as a back-end engineer, it might be the single most important database to be familiar with.

Assignment

  1. macOS with brew

    brew install postgresql@15
    

    Linux / WSL (Debian). Here are the docs from Microsoft, but simply:

    sudo apt update
    sudo apt install postgresql postgresql-contrib
    
  2. psql --version
    
  3. sudo passwd postgres
    

    Enter a password, and be sure you won't forget it. You can just use something easy like postgres.

    • Mac: brew services start postgresql@15
    • Linux: sudo service postgresql start
  4. Enter the psql shell:

    • Mac: psql postgres
    • Linux: sudo -u postgres psql

    You should see a new prompt that looks like this:

    postgres=#
    
  5. CREATE DATABASE chirpy;
    
  6. \c chirpy
    

    You should see a new prompt that looks like this:

    chirpy=#
    
  7. ALTER USER postgres WITH PASSWORD 'postgres';
    

    For simplicity, I used postgres as the password. Before, we altered the system user's password, now we're altering the database user's password.

  8. From here you can run SQL queries against the chirpy database. For example, to see the version of Postgres you're running, you can run:

    SELECT version();
    

When you're done, type exit or \q to leave the psql shell.

Run and submit the CLI tests.