more responsive more engaging with couchbase server and node.js

53
Titanium Sponsors Platinum Sponsors Gold Sponsors

Upload: couchbase

Post on 05-Jul-2015

590 views

Category:

Technology


2 download

DESCRIPTION

More responsive more engaging with couchbase server and node.js Presented at KCDC 2014

TRANSCRIPT

Page 1: More responsive more engaging with couchbase server and node.js

Titanium  Sponsors

Platinum  Sponsors

Gold  Sponsors

Page 2: More responsive more engaging with couchbase server and node.js

More  Responsive,  More  Engaging  with  Couchbase  Server  

and  Node.jsMatt  Ingenthron  and  Brett  Lawson  

Couchbase,  Inc.  

2

Page 3: More responsive more engaging with couchbase server and node.js

Who  am  I?

Ma$  has  done  web  stuff  for  years.  He  is  a  cofounder  of  Couchbase  and  has  been  a  contributor  to  the  memcached  project,  one  of  the  maintainers  of  the  Java  spymemcached  client  and  worked  on  nearly  every  component  of  Couchbase  Server  at  one  Bme  or  another.  He  is  Director  of  Developer  SoluBons  where  he  heads  up  Couchbase's  work  in  geFng  the  right  client  interface  needed  for  Node.js,  PHP,  Java,  .NET  Ruby  and  Python  developers  (among  others).  !Bre$  has  been  an  avid  programmer  since  the  young  age  of  9  and  has  been  working  in  in  the  games  industry  for  9  years.    At  the  Gogii  game  studio  he  worked  on  highly  scalable  social  game  servers  using  Node.js  and  Couchbase,  designed  to  scale  to  tens  thousands  of  concurrent  users.    He  has  most  recently  joined  Couchbase  Inc.  to  lead  the  architecture  and  development  of  the  Node.js  and  PHP  SDKs  and  contributes  to  the  development  of  the  C  SDK,  a.k.a.  libcouchbase.  !

3

Page 4: More responsive more engaging with couchbase server and node.js

What  do  we  need  for  a  Game?

4

Page 5: More responsive more engaging with couchbase server and node.js

Requirements  for  a  Game  API

5

• 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: More responsive more engaging with couchbase server and node.js

How  Do  We  Get  There?

6

Page 7: More responsive more engaging with couchbase server and node.js

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

7

Page 8: More responsive more engaging with couchbase server and node.js

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

8

Page 9: More responsive more engaging with couchbase server and node.js

JSON  we  all  know  and  love

9

Page 10: More responsive more engaging with couchbase server and node.js

JSON  in  node.js

10

/* $ 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: More responsive more engaging with couchbase server and node.js

Couchbase  &  JSON

11

Page 12: More responsive more engaging with couchbase server and node.js

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

12

Page 13: More responsive more engaging with couchbase server and node.js

Couchbase  Views

13

Page 14: More responsive more engaging with couchbase server and node.js

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

14

{ "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: More responsive more engaging with couchbase server and node.js

JSON  in  the  Browser

15

<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: More responsive more engaging with couchbase server and node.js

JavaScript  is  the  Programming  Language  of  the  Web

16

Page 17: More responsive more engaging with couchbase server and node.js

DEMO

17

Page 18: More responsive more engaging with couchbase server and node.js

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

18

Page 19: More responsive more engaging with couchbase server and node.js

19

Userssignup  /  signin

Page 20: More responsive more engaging with couchbase server and node.js

20

! app.post("/signup", function (req, res) { var credentials = req.body User.create(credentials, function (err, user) { if (err) { res.render("signup", { message: err }) } else { req.session.userId = user.userId res.redirect("/") } }) })

Page 21: More responsive more engaging with couchbase server and node.js

21

app.post("/signin", function (req, res) { var credentials = req.body User.authenticate(credentials, function (err, user) { if (err) { res.render("signup", { message: err }) } else { req.session.userId = user.userId res.redirect("/") } }) })

Page 22: More responsive more engaging with couchbase server and node.js

The  Database

22

Page 23: More responsive more engaging with couchbase server and node.js

Connecting  to  Couchbase23

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

Reuse  your  database  connection

Page 24: More responsive more engaging with couchbase server and node.js

Smart  client

•  Abstracts  the  cluster  managing  connections  to  all  servers  reestablishes  failed  connection  

•  Manages  connecVon  handshake  Transfers  and  continuously  updates  the  cluster  map  Detects  cluster  configuration  changes  and  abstracts  them  for  the  user  

24

Page 25: More responsive more engaging with couchbase server and node.js

Keying

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

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

25

Page 26: More responsive more engaging with couchbase server and node.js

Creating  a  new  userAdd  

User  Data  model    Serialisation

26

Page 27: More responsive more engaging with couchbase server and node.js

AuthenticationLoading  is  quick  

use  Bcrypt

27

Page 28: More responsive more engaging with couchbase server and node.js

Game  StateRoutes

28

Page 29: More responsive more engaging with couchbase server and node.js

29

app.get("/questions", auth, function (req, res) { res.set("Content-Type", "application/json") QuestionList.forUser(req.user, function (err, list) { if (err) { res.statusCode = 500 res.end() } else { res.end(JSON.stringify(list)) } }) }) !

Page 30: More responsive more engaging with couchbase server and node.js

30

app.post("/questions", auth, function (req, res) { var questions = req.body QuestionList.saveForUser(req.user, questions, function (err) { if (err) { res.statusCode = 500 return res.end() } else { QuestionList.forUser(req.user, function (err, list) { if (err) { res.statusCode = 500 res.end() } else { res.end(JSON.stringify(list)) } }) } }) })

Page 31: More responsive more engaging with couchbase server and node.js

Question  ListBe  lazy  we  expect  free  users  

Referential  Keys

31

Page 32: More responsive more engaging with couchbase server and node.js

Dealing  with  concurrency

• If  a  question  is  answered  form  two  devices  at  the  same  time,  they  collide  

We  don’t  want  to  accidentally  overwrite  an  answer  edit  since  we’re  sending  a  full  document  update  right  after  theirs  

• Retry  the  operation,  if  appropriate

32

Actor  1 Actor  2

Couchbase  Server

CAS  mismatch  &  retry

Success

[      {          "id":  "1",          "text":  "What  is  couchbases  upcoming  query  language  called?",          "choices":  [              {                  "text":  "N1QL",                  "state":  true,                  "count":  1,                  "id":  "a",                  "$$hashKey":  "00Q"              },              {                  "text":  "SQL",                  "state":  false,                  "count":  0,                  "id":  "b",                  "$$hashKey":  "00R"              },              {  

Page 33: More responsive more engaging with couchbase server and node.js

33

!QuestionList.prototype.load = function (includeCount, cb) { var that = this db.get(this.key, function (err, data) { if (err) { … } else { that.cas = data.cas that.questions = data.value cb(null, that) } }) } !QuestionList.prototype.save = function (cb) { db.set(this.key, this, { cas: this.cas }, cb) }

Page 34: More responsive more engaging with couchbase server and node.js

Consistent  DataWe  don’t  need  quorum  or  merge  data  in  the  application  

layer  

34

Page 35: More responsive more engaging with couchbase server and node.js

The  Frontend

35

Page 36: More responsive more engaging with couchbase server and node.js

AngularJS

• Provide  views  template  data  bindings  

• Encapsulate  logic  in  Model-­‐View-­‐“Whatever  works”  

• Templates  are  based  on  “enhanced”  HTML

36

Page 37: More responsive more engaging with couchbase server and node.js

Rendering  JSON  responsesUsing  response  data  directly

37

Page 38: More responsive more engaging with couchbase server and node.js

One  more  thing

38

Page 39: More responsive more engaging with couchbase server and node.js

Showing  the  answer  countCouchbase  map  reduce  in  action

39

Page 40: More responsive more engaging with couchbase server and node.js

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

40

Page 41: More responsive more engaging with couchbase server and node.js

Development  vs.  Production  Views

41

• 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 42: More responsive more engaging with couchbase server and node.js

Eventual  Persistence

42

Page 43: More responsive more engaging with couchbase server and node.js

43

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 44: More responsive more engaging with couchbase server and node.js

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  If  you  have  1M  likes  it’s  ok  to  be  off  by  5  for  a  short  amount  of  time

44

Page 45: More responsive more engaging with couchbase server and node.js

Couchbase  3.0Many  improvements  to  views  are  coming

45

Page 46: More responsive more engaging with couchbase server and node.js

Watch  out  for  N1QL  coming  up!

46

Page 47: More responsive more engaging with couchbase server and node.js

Creating  views  with  code

47

var countsDDoc = { "views": { "counts": { "map": "function (doc, meta) { if(doc[0]) {…}”, "reduce": "_sum" } } } !db.setDesignDoc("ncqa", countsDDoc, function (err, data) { if (err) { console.log("failed to setup view.", err) } else { console.log("setup view done.") } db.shutdown() })

Page 48: More responsive more engaging with couchbase server and node.js

Querying  the  View

48

!QuestionList.prototype.loadCounts = function (cb) { var that = this db.conn(function (err, conn) { var opts = {stale: false, group: true, keys: choicesKeys} var q = conn.view("ncqa", "counts", opts) q.query(function (err, results) { … }) }) }

Page 49: More responsive more engaging with couchbase server and node.js

FINAL  DEMO

49

Page 50: More responsive more engaging with couchbase server and node.js

What  to  do  next…• get  the  code,  set  it  up,  run    

https://github.com/couchbaselabs/node-­‐couch-­‐qa  

• Read  Brett's  blogs    https://blog.couchbase.com/game-­‐servers-­‐and-­‐couchbase-­‐nodejs-­‐part-­‐1  

• Write  your  own!    We  help  you  along!  http://www.couchbase.com/communities/nodejs

50

Page 51: More responsive more engaging with couchbase server and node.js

Resources• 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

51

Page 52: More responsive more engaging with couchbase server and node.js

Thank  you!

52

[email protected]  [email protected]  

!@brett19x  @ingenthr

Page 53: More responsive more engaging with couchbase server and node.js

53