Why Node, Express and Postgres - presented 23 Feb 15, Talkjs, Microsoft Auditorium

56
WEB SERVICES Why Node, Express and Postgres?

Transcript of Why Node, Express and Postgres - presented 23 Feb 15, Talkjs, Microsoft Auditorium

WEB SERVICESWhy Node, Express and Postgres?

ABOUT ME.

• Programming as a hobby.

• Final student, NTU (Biz IT).

• Mainly involved in tech-related

entrepreneurial projects.

• Latest project is

VogueVerve.com.

CONTENTS.

• Nodejs

• Expressjs

• Node modules

– Passport

– Sequelize

– Bluebird

• Postgres, Mongo, Couchbase

IT’S SIMPLE.

IT’S JS FRONT-TO-BACK.

WHAT IS NODEJS?

• It is a server side Javascript

runtime environment.

• Powered by Chrome V8.

• Main intention of real-time

communication.

• It can be a standalone server.

WHO USES IT?

MAIN ATTRIBUTES.

• Single threaded.

• Async, non-blocking I/O.

• Event-oriented.

IT DOESN’T WAIT FOR

ANYTHING.

USING JS FOR SERVER-SIDE

APPLICATION SOUNDED LIKE

A BAD IDEA.

Search “Node.js is cancer”

HOW TO NODE?

• Delegate computationally

intensive tasks to lower-level

languages like C++.

• Make clusters (to combat

single-threading).

• Node errors will crash the

server. Gotta catch ‘em all.

MODULAR CONCEPT.

NPM REPOSITORY.

• Modularity and versioning.

• Updates and releases.

• The best thing since beer.

WHAT IS EXPRESS?

EXPRESS.

• It is a Node Module.

• The most popular web

framework for Node.

• “Sinatra inspired”.

• Makes building web

application incredibly easy.

ROUTE HANDLING

`REQ` AND `RES`

OBJECTS.

• `req` comes for the user.

• `res` is sent back to the user.

• I use it extensively for “API-

styled” web platform.

REQ AND RES OBJECTS

API IN 3 SIMPLE STEPS

NODE MODULES

PASSPORTJS.

• Authentication API.

• Supports almost any kind of

authentication framework.

• FaceBook, Twitter, Google

OAuth modules already

available.

AN EXAMPLE

HOW TO USE IT?

• Documentation is abit skimpy

and untelling.

• Learn by example:

https://scotch.io/tutorials/easy-

node-authentication-setup-

and-local

SEQUELIZEJS.

• Object-Relational-Mapper

(ORM)

• Works with MySQL, Postgres

and others.

• Promise-styled. Very intuitive

and maintainable code.

• Support transactions.

ACTIVE RECORDS

RAW QUERIES?

• You get replacements for free.

BLUEBIRD.

• Very good performance.

• Prevents `callback hell` in

asynchronous coding.

• Makes code very readable.

• Parallelism.

WHY ARE PROMISES SO

GOOD?

Because it’s very intuitive and

readable.

Codes that solves a problem but

cannot be easily understood, is a

problem.

CALLBACK HELL

SOME ADVICES

• Don’t use modules that are not

well-maintained.

• Worry about performance, but

not too much.

POSTGRES, MONGO, COUCH.

Relational

data

Documents

?

Your data.Your storage.

vs

MONGO AND COUCH -

JSON

Denormalized into a single

document. Fast.

IN THE MIDDLE

Use MongoDB/Couchbase + a lot of indexing

OR

SQL with many many joins

SQL with serialized/encoded arrays

THE OLD WAYS

• Joins are slow.

• Serializing can be problematic.

• Atomic updates impossible.

• Values cannot be used as selection criteria.

MONGO AND COUCHBASE

CAN HANDLE SOME

“RELATIONAL” NEEDS AND

STILL REMAIN FAST. BUT…

THE PROBLEM

• What if Joe

changes

name?

• What if a

new field is

added to

users?

YOU MAY REACH A POINT

WHERE THERE IS JUST TOO

MUCH TO REPLICATE.

YOU CAN DO THIS AND IT IS

STILL PROBABLY FAST

You find yourself trying to do “joining”.

Are there any ORMs for

Mongo/Couchbase?

CHANGE IS THE ROOT OF ALL

PROBLEMS.

Will your data become more relational

than it is now?

http://www.sarahmei.com/blog/2013/11

/11/why-you-should-never-use-

mongodb/

POSTGRES JSON AND

HSTORE

• A field that can contain “array-

structured” data.

• Atomic operations: Key/values

can be searched for and

indexed.

• Fast.

• Best thing since NPM, and

beer.

A VERY FLEXIBLE

STRUCTURE

id name data

1 Mary { “country”: “Singapore”, “dataMeta”: { “following”: “299”, “followers”: “484” }}

2 Lucy { “country”: “France”}

3 Jane null

4 Tom {“dataMeta”: { “following”: “479” }}

5 Dick {“dataMeta”: { “following”: “12”, followers: “40”}}

WHAT ABOUT THE NEED FOR

SPEED?

PERFORMANCE

• Different sources will stake

different claims.

• Comparing apples with

oranges.

• They are all quite fast.

TEST USING YOUR DATA

STRUCTURE AND YOUR

TYPICAL ROUTINE.

IT IS NOT PERFECT

• Only HSTORE supports atomic

updates, JSON does not.

• HSTORE does not support

nested structure.

• Postgres JSON is very new.

Library support is adequate

but not mature.

Relational

data

Documents

CONCLUSION ON PG

• It is a RDB that is very capable

of handling schemaless data.

• Don’t over-normalize your

data.

• Use relations and constraints

for critical portions of your

data.

1. SERVER-SIDE JAVASCRIPT

IS A GOOD IDEA.

2. IF YOU ARE BUILDING A

WEB SERVICE, USE EXPRESS.

3. ASK NOT WHAT A TECH

CAN DO FOR YOU. ASK WHAT

IT CANNOT DO.

KEEP IN TOUCH!

• Facebook: /calvintwr

• Email: [email protected]