Slide 1

Slide 1 text

Databases! Preetam Jinka March 2014 Intro to

Slide 2

Slide 2 text

Me. 2nd year math major Twitter/GitHub: @PreetamJinka Website: http://preet.am/ Email: [email protected] Credit: Yugank Singhal

Slide 3

Slide 3 text

Code along! https://github.com/PreetamJinka/intro-to-databases 1. Get Node.js 2. Clone the repository 3. Run npm install

Slide 4

Slide 4 text

Disclaimers. ● Limited scope ● Alternatives used

Slide 5

Slide 5 text

Ask questions!

Slide 6

Slide 6 text

What the heck is a database? DEFINITION.

Slide 7

Slide 7 text

What the heck is a database?

Slide 8

Slide 8 text

Database Management Systems Systems that manage organized collections of data. From now on: “database” ⇒ DBMS

Slide 9

Slide 9 text

There are tons of databases out there. What sets them apart?

Slide 10

Slide 10 text

1. The structure of data. - How does the database look at data? 2. The questions you can ask. - Queries

Slide 11

Slide 11 text

Types ● Relational ○ MySQL, PostgreSQL, Oracle, … ● Document stores ○ MongoDB, CouchDB, Couchbase, … ● Graph ○ Neo4j, … ● Key-value stores ○ File system, Redis, FoundationDB, … ● Search ○ Lucene, Solr, Elasticsearch, … … and a bunch of others I’m missing.

Slide 12

Slide 12 text

Types ● Relational ○ MySQL, PostgreSQL, Oracle, … ● Document stores ○ MongoDB, CouchDB, Couchbase, … ● Graph ○ Neo4j, … ● Key-value stores ○ File system, Redis, FoundationDB, … ● Search ○ Lucene, Solr, Elasticsearch, … … and a bunch of others I’m missing.

Slide 13

Slide 13 text

Key-value stores Like associative arrays, dictionaries, maps, hashes, … key value "2+2" 4 1 true "foo" "bar"

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

File systems! key value /etc/resolv.conf # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 127.0.1.1 .gitignore ## .gitignore ## # dependencies node_modules/ # secret tokens tokens.js

Slide 16

Slide 16 text

Demo!

Slide 17

Slide 17 text

Redis A really fast advanced key-value store. “Data structure server” strings, hashes, lists, sets and sorted sets

Slide 18

Slide 18 text

Querying Redis

Slide 19

Slide 19 text

Relational databases Think “tables” By “relational,” we mean having to do with relations.

Slide 20

Slide 20 text

Relational databases username first name last name bob Bob Barker alice Alice Cooper pj Preetam Jinka Languages table username language bob JavaScript alice C++ pj Go Users table

Slide 21

Slide 21 text

Relational databases username first name last name bob Bob Barker alice Alice Cooper pj Preetam Jinka Languages table username language bob JavaScript alice C++ pj Go Users table Relation!

Slide 22

Slide 22 text

SQL Structured Query Language This is how we interact with most, if not all, relational databases.

Slide 23

Slide 23 text

Demo!

Slide 24

Slide 24 text

Document databases Type of key-value stores. Values are “documents”

Slide 25

Slide 25 text

Example document { "_id": "post_1", "title": "Hello, world", "content": "This is my first blog post!", "tags": ["general"] } post_1 is the “key”

Slide 26

Slide 26 text

MongoDB’s collections Collections are groups of documents. They’re like tables in MongoDB’s context.

Slide 27

Slide 27 text

Document database queries ● Get individual documents ● Find documents that ○ have a certain value ○ have properties that… ■ have a certain value

Slide 28

Slide 28 text

Demo!

Slide 29

Slide 29 text

Map-reduce Source: http://docs.mongodb.org/manual/core/map-reduce/

Slide 30

Slide 30 text

Map-reduce

Slide 31

Slide 31 text

Demo!

Slide 32

Slide 32 text

IMPORTANT: There’s no one database to rule them all. Unfortunate. :-/

Slide 33

Slide 33 text

Use the right tool for the job.

Slide 34

Slide 34 text

You wouldn’t use a hash table for an ordered map, right? Please say yes :P

Slide 35

Slide 35 text

Case study

Slide 36

Slide 36 text

The application and the database Application Application Application Database State State State “stateless web application are far more flexible and scalable than their stateful counterparts” — Application State - We all want to be Stateless

Slide 37

Slide 37 text

Database State State State web_app( ) State Web page

Slide 38

Slide 38 text

Twelve-Factor App Methodology 12factor.net “Twelve-factor processes are stateless and share-nothing. Any data that needs to persist must be stored in a stateful backing service, typically a database.”

Slide 39

Slide 39 text

Dude, where’s my state?

Slide 40

Slide 40 text

In the database(s).

Slide 41

Slide 41 text

Pro tips Write abstractions. Keep things simple. Mock and test.

Slide 42

Slide 42 text

Pro tips Ask for help.

Slide 43

Slide 43 text

That’s all (for now), folks! Thanks for coming! @PreetamJinka [email protected]