First: Project Questions?


Today: Databases

The Problem: Persistent Data Storage

Over the semester, we worried about a several different kinds of state:

A Postgres database isn’t the only way to deal with server-side, persistent state. Let’s look at the problem in some more detail.

Plan A: Files on the Filesystem


Walk Through Solving Husky Shop

Husky Shop Tables:

Plan B: Key-Value Store

This is a library or service that stores values associated with keys.

Minimally, has two operations: get and put



Walk Through Solving Husky Shop

Plan C: Relational Database

A Relational Database Management System, or SQL Database, splits data up into a set of tables, specifies a query language (SQL), and provides “transactions”, which offer a set of guaranteed reliability properties.





Scaling SQL Databases:


NoSQL is the name for the broad idea that getting rid of features from SQL DBs results in a useful datastore.

Generally these systems don’t use SQL as the query language, and they don’t provide the ACID data reliability guarantees.

Example 1: Memcached

Example 2: Redis

Example 3: MongoDB


Draw picture with 6 notes showing replicas (n=3) and sharding.

CAP Theorem

You can’t have both Consistency and Availablility in the same system.

Proof: Consider a data store with two nodes (A and B) that currently can’t communicate due to a netsplit. We do a write to node A and then read the same key from node B.

The system must, before the netsplit is resolved:

This means that real-world distributed storage systems can be classfied into one of two categories:

When a system allows you to tune read and write quorms, you’re trading off immediate reads, immediate writes, and reads getting old data.

Example 4: CouchDB

Offline support:

Example 5: Riak KV



Example: Counter

Example: Set

Single-use Set CRDT:

{ adds: // List of items added dels: // List of items removed }

Using this for the car example:

Example 6: Cassandra

Other Options

CockrochDB: A distributed SQL database?

Persistent Browser State


Service Worker

PouchDB + CouchDB

npm install pouchdb
var PouchDB = require('pouchdb');
var db = new PouchDB('kittens');

var doc = {
  "_id": "mittens",
  "name": "Mittens",
  "occupation": "kitten",
  "age": 3,
  "hobbies": [
    "playing with balls of yarn",
    "chasing laser pointers",
    "looking cute"

db.get('mittens').then(function (doc) {