Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Couchbase  Server  and  node.js  
 
 Full  Stack  JSON Philipp  Fehre   Developer  Evangelist,  Couchbase,  Inc.

Slide 3

Slide 3 text

Who  am  I? Ex-­‐Consultant  of  many  projects   NoSQL  Database  guy  of  many  Databases   Open  Source  Enthusiast  of  many  lines  of  code   JavaScripting  Rubyist

Slide 4

Slide 4 text

What  do  we  need  for  a  Game?

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

How  Do  We  Get  There?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

JSON   we  all  know  and  love

Slide 10

Slide 10 text

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 */

Slide 11

Slide 11 text

Couchbase  &  JSON

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Couchbase  Views

Slide 14

Slide 14 text

JSON  as  the  API curl  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/…", ... }

Slide 15

Slide 15 text

JSON  in  the  Browser
$.getJSON("https://api.github.com/users/sideshowcoder", function (data) { $("#user-name").text(data.login); $("#last-active").text(data.updated_at); })

Slide 16

Slide 16 text

JavaScript  is  the   Programming  Language   of  the  Web

Slide 17

Slide 17 text

DEMO

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Users signup  /  signin  /  signout  

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

The  Database

Slide 22

Slide 22 text

Smart  client Reuse  your  database  connection

Slide 23

Slide 23 text

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); }) }

Slide 24

Slide 24 text

Creating  a  new  user Add   User  Datamodel     Serialisation

Slide 25

Slide 25 text

Authentication Loading  is  quick   use  Bcrypt

Slide 26

Slide 26 text

Game  State Routes

Slide 27

Slide 27 text

Question  List Be  lazy  we  expect  free  users   Don’t  overwrite  by  accident   Referential  Keys

Slide 28

Slide 28 text

Consistent  Data Couchbase  is  a  CP  System

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

The  Frontend

Slide 31

Slide 31 text

Rendering  JSON  responses Using  response  data  directly

Slide 32

Slide 32 text

One  more  thing

Slide 33

Slide 33 text

Showing  the  answer  count Couchbase  map  reduce  in  action

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

Eventual  Persistence

Slide 37

Slide 37 text

Storage  to  Index Couchbase Server EP Engine RAM Cache Disk Write Queue Replication Queue View Engine Indexers Application Server storage ops Replica Couchbase Cluster Machine

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Creating  views  with  code

Slide 40

Slide 40 text

Querying  the  View

Slide 41

Slide 41 text

FINAL  DEMO

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Thank  you! [email protected]   ! github:  @sideshowcoder   twitter:  @ischi   web:  sideshowcoder.com

Slide 44

Slide 44 text

No content