Node.js and couchbase Full Stack JSON - Munich NoSQL

3,308 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,308
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
28
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Node.js and couchbase Full Stack JSON - Munich NoSQL

  1. 1. Couchbase  Server  and  node.js  
 
 Full  Stack  JSON Philipp  Fehre   Developer  Evangelist,  Couchbase,  Inc.
  2. 2. Who  am  I? Ex-­‐Consultant  of  many  projects   NoSQL  Database  guy  of  many  Databases   Open  Source  Enthusiast  of  many  lines  of  code   JavaScripting  Rubyist
  3. 3. What  do  we  need  for  a  Game?
  4. 4. 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.
  5. 5. How  Do  We  Get  There?
  6. 6. 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. 7. 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. 8. JSON   we  all  know  and  love
  9. 9. 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 */
  10. 10. Couchbase  &  JSON
  11. 11. 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. 12. Couchbase  Views
  13. 13. 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/…", ... }
  14. 14. 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>
  15. 15. JavaScript  is  the   Programming  Language   of  the  Web
  16. 16. DEMO
  17. 17. Building  an  API   with  Couchbase,  node.js,  and  AngularJS
  18. 18. Users signup  /  signin  /  signout  
  19. 19. 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
  20. 20. The  Database
  21. 21. Smart  client Reuse  your  database  connection
  22. 22. 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); }) }
  23. 23. Creating  a  new  user Add   User  Datamodel     Serialisation
  24. 24. Authentication Loading  is  quick   use  Bcrypt
  25. 25. Game  State Routes
  26. 26. Question  List Be  lazy  we  expect  free  users   Don’t  overwrite  by  accident   Referential  Keys
  27. 27. Consistent  Data Couchbase  is  a  CP  System
  28. 28. 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
  29. 29. The  Frontend
  30. 30. Rendering  JSON  responses Using  response  data  directly
  31. 31. One  more  thing
  32. 32. Showing  the  answer  count Couchbase  map  reduce  in  action
  33. 33. Couchbase  Views http://localhost:9000/index.html? na#sec=views&viewsBucket=default
  34. 34. 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.
  35. 35. Eventual  Persistence
  36. 36. Storage  to  Index Couchbase Server EP EngineRAM Cache Disk Write Queue Replication Queue View Engine Indexers Application Server storage ops Replica Couchbase Cluster Machine
  37. 37. 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
  38. 38. Creating  views  with  code
  39. 39. Querying  the  View
  40. 40. FINAL  DEMO
  41. 41. 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
  42. 42. Thank  you! philipp@couchbase.com   ! github:  @sideshowcoder   twitter:  @ischi   web:  sideshowcoder.com

×