node.js and couchbase full stack json - munich nosql

44

Upload: philipp-fehre

Post on 04-Jul-2015

2.227 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Node.js and couchbase   Full Stack JSON - Munich NoSQL
Page 2: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Couchbase  Server  and  node.js  

Full  Stack  JSON

Philipp  Fehre  

Developer  Evangelist,  Couchbase,  Inc.

Page 3: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Who  am  I?

Ex-­‐Consultant  of  many  projects  

NoSQL  Database  guy  of  many  Databases  

Open  Source  Enthusiast  of  many  lines  of  code  

JavaScripting  Rubyist

Page 4: Node.js and couchbase   Full Stack JSON - Munich NoSQL

What  do  we  need  for  a  Game?

Page 5: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Requirements  for  a  Game  API

• RESTful,  probably  JSON  interface  for  stateful  data  

• SPEED!!!  Users  are  fickle.    Remember  that  fetching  a  data  item  is  inherently  synchronous.  

• Ability  to  Scale  True  social  games  need  state,  and  scale  Scale  means  scale  up  and  scale  down  

• Efficiency  Let’s  be  honest,  most  players  are  free  players.    However,  we  need  free  players  to  get  the  scale  we  need  to  get  enough  pay  players.

Page 6: Node.js and couchbase   Full Stack JSON - Munich NoSQL

How  Do  We  Get  There?

Page 7: Node.js and couchbase   Full Stack JSON - Munich NoSQL

node.js  

• REST  is  native  HTTP  is  native!  JSON  is  native!  

• Event  driven  programming  model  Leads  your  software  development  to  be  fast  

• Scale  out  ready  Because  of  the  loose  coupling,  node  can  scale  out  as  far  as  you  have  machines  However,  this  means  it  needs  Couchbase  

• Very  efficient  use  of  system  resources  Single  threaded  but  multi-­‐process,  event  driven  model  ensures  good  handoff  from  compute  to  IO

Page 8: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Couchbase  

• Document  database  Designed  for  JSON  

• Sub-­‐millisecond  latency  Gives  you  the  consistent  performance  needed  to  build  complex,  interactive  game  play  

• Designed  for  Scale  Add  and  remove  nodes  as  needed  

• Efficiency  Couchbase  manages  system  resources  such  as  memory  and  CPU  efficiently

Page 9: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JSON  we  all  know  and  love

Page 10: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JSON  in  node.js/* $ cat example.json * { * "foo": "bar" * } */ !var fs = require("fs"); var rawData = fs.readFileSync("./example.json"); var data = JSON.parse(rawData); console.log("property foo of data:", data.foo); !/* $ node read.js * property for of data: bar */

Page 11: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Couchbase  &  JSON

Page 12: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JSON  &  Couchbase

• Native  Data  format  Special  support  for  JSON  documents  is  provided  Couchbase  recognises  JSON  as  a  Datatype  

• Complex  queries  JSON  can  be  handled  by  the  View  engine  Build  indices  via  Map  /  Reduce

Page 13: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Couchbase  Views

Page 14: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JSON  as  the  APIcurl  https://api.github.com/users/sideshowcoder

{ "login": "sideshowcoder", "id": 108488, "avatar_url": "https://avatars.githubusercontent.com/u/…", "gravatar_id": "5cde19029032f151ca09687f7c8783eb", "url": "https://api.github.com/users/sideshowcoder", "html_url": "https://github.com/sideshowcoder", "followers_url": "https://api.github.com/users/…", "following_url": "https://api.github.com/users/…", ... }

Page 15: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JSON  in  the  Browser<html> <body> <script src="/jquery-1.11.0.min.js"></script> <div id="user-name"></div> <div id="last-active"></div> <script> $.getJSON("https://api.github.com/users/sideshowcoder", function (data) { $("#user-name").text(data.login); $("#last-active").text(data.updated_at); }) </script> </body> </html>

Page 16: Node.js and couchbase   Full Stack JSON - Munich NoSQL

JavaScript  is  the  Programming  Language  of  the  Web

Page 17: Node.js and couchbase   Full Stack JSON - Munich NoSQL

DEMO

Page 18: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Building  an  API  with  Couchbase,  node.js,  and  AngularJS

Page 19: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Userssignup  /  signin  /  signout  

Page 20: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Keying

•Use  a  Unique  value  for  key  (email,  username,  sku,  isbn,  etc.)  example  u::phil  

•Predictable  Keys  can  follow  Key-­‐Value  pa[erns  Users  typically  can  be  done  this  way  and  are  the  most  numerous  items

Page 21: Node.js and couchbase   Full Stack JSON - Munich NoSQL

The  Database

Page 22: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Smart  clientReuse  your  database  connection

Page 23: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Connecting  to  Couchbase

var _db = null; !var db = function (cb) { if (_db) return cb(null, _db); _db = new couchbase.Connection(dbConfig, function(err) { if (err) return cb(err); cb(null, _db); }) }

Page 24: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Creating  a  new  userAdd  

User  Datamodel    Serialisation

Page 25: Node.js and couchbase   Full Stack JSON - Munich NoSQL

AuthenticationLoading  is  quick  

use  Bcrypt

Page 26: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Game  StateRoutes

Page 27: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Question  ListBe  lazy  we  expect  free  users  Don’t  overwrite  by  accident  

Referential  Keys

Page 28: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Consistent  DataCouchbase  is  a  CP  System

Page 29: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Dealing  with  concurrency

• Other  users  are  ratings  beers  also,  so  we  use  a  CAS  update  we  don’t  want  to  accidentally  overwrite  another  users  rating  that  is  being  saved  at  the  same  time  as  ours  

• Retry  the  operation,  if  appropriate  Also  useful  if  you  have  internal  structure  that  you  want  to  maintain

Actor  1 Actor  2

Couchbase  Server

CAS  mismatch  &  retry

Success

Page 30: Node.js and couchbase   Full Stack JSON - Munich NoSQL

The  Frontend

Page 31: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Rendering  JSON  responsesUsing  response  data  directly

Page 32: Node.js and couchbase   Full Stack JSON - Munich NoSQL

One  more  thing

Page 33: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Showing  the  answer  countCouchbase  map  reduce  in  action

Page 34: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Couchbase  Viewshttp://localhost:9000/index.html?na#sec=views&viewsBucket=default

Page 35: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Development  vs.  Production  Views• Development  views  index  a  

subset  of  the  data.  

• Publishing  a  view  builds  the  index  across  the  entire  cluster.  

• Queries  on  production  views  are  scattered  to  all  cluster  members  and  results  are  gathered  and  returned  to  the  client.

Page 36: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Eventual  Persistence

Page 37: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Storage  to  Index

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

View Engine

Indexers

Application Server

storage ops

Replica Couchbase Cluster Machine

Page 38: Node.js and couchbase   Full Stack JSON - Munich NoSQL

When  to  use  and  when  not  to  use

• views  operate  on  the  persisted  data  

• don’t  use  for  “login”  

• data  can  be  “stale”  Counts  and  alike  are  ok  to  be  stale

Page 39: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Creating  views  with  code

Page 40: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Querying  the  View

Page 41: Node.js and couchbase   Full Stack JSON - Munich NoSQL

FINAL  DEMO

Page 42: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Links• https://github.com/couchbaselabs/node-­‐couch-­‐qa  • https://github.com/couchbase/couchnode  • https://blog.couchbase.com/game-­‐servers-­‐and-­‐couchbase-­‐nodejs-­‐part-­‐1  • https://blog.couchbase.com/game-­‐servers-­‐and-­‐couchbase-­‐nodejs-­‐part-­‐2  • https://blog.couchbase.com/game-­‐servers-­‐and-­‐couchbase-­‐nodejs-­‐part-­‐3  • https://github.com/brett19/node-­‐gameapi

Page 43: Node.js and couchbase   Full Stack JSON - Munich NoSQL

Thank  you!

[email protected]  !

github:  @sideshowcoder  twitter:  @ischi  

web:  sideshowcoder.com

Page 44: Node.js and couchbase   Full Stack JSON - Munich NoSQL