1 / 85

A COMMAND PROMPT TALK

A COMMAND PROMPT TALK. Node.js and postgres. An Introductory Glance With Live Code, Later. 2010 AURYNN SHAW, COMMAND PROMPT INC. Hi!. What is Node.js. Fast Pure event-driven server-side javascript programming environment. Ew, javascript. Built on Google’s V8

omar
Télécharger la présentation

A COMMAND PROMPT TALK

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A COMMAND PROMPT TALK

  2. Node.js and postgres • An Introductory Glance • With Live Code, Later 2010 AURYNN SHAW, COMMAND PROMPT INC.

  3. Hi!

  4. What is Node.js • Fast • Pure event-driven • server-side • javascript programming environment

  5. Ew, javascript • Built on Google’s V8 • Runs on modern unix-likes (Linux, OSX, Freebsd, Cygwin) • Speaks network natively, and easily (Hello world is in http!) • Ends up Similar to Python, Perl, Ruby, ETc.

  6. Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block

  7. Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block • Keeps the slow tasks from blocking • Keeps the quick tasks quick

  8. blocking? # Standard synchronous processing fh = open(“somefile”,”r”) contents = fh.read() # what if somefile is huge, 1GB+ ? print contents

  9. Every step waits

  10. How is node different?

  11. Did I mention fast? • Single thread can do many thousands of connections • standard library doesn’t block • Keeps the slow tasks from blocking • Keeps the quick tasks quick

  12. Really good for anything I/O

  13. While slow tasks run, node carries on

  14. Non-blocking! fs.open(“somefile”, function (fd) { // When the file descriptor gets returned. var buffer = null; fs.read(fd, buffer, length, position, function (e, bytes) { // bytes read are here. // buffer will be not-null. }); });

  15. As always, there’s caveats to this

  16. Some caveats • node is entirely single-threaded • Event-driven programming is *really* different

  17. A maze of callbacks fs.open(“/tmp/hello”, function (fd) { var buffer = null; fs.read(fd, buffer, length, position, function (e, bytes) { // bytes read are here. // buffer will be not-null. }); });

  18. Some caveats • node is entirely single-threaded • Event-driven programming is *really* different • Javascript’s prototype model is also really different • The syntax has warts

  19. Build a bridge out of her? /* This allows you to kind of inherit properly. */ var o = function () {}; var p = function () { /* Because ‘this’ changes */ var self = this; o.call(this); }; p.prototype = o; p.prototype.constructor = p;

  20. So why is this important?

  21. Why is it important? • Javascript owns the frontend

  22. Why is it important? • Javascript owns the frontend • More jquery and javascript programmers every day

  23. Why is it important? • Javascript owns the frontend • More jquery and javascript programmers every day • Javascript is fast

  24. Faster every day

  25. And now, we can use postgres!

  26. When I got here... • postgres.js worked.

  27. When I got here... • postgres.js worked. • Used the simple query protocol

  28. When I got here... • postgres.js worked. • Used the simple query protocol • Used SQL escape to prevent injection attacks.

  29. whyyyy SOURCE: http://www.flickr.com/photos/striatic/2192192956/

  30. But it did work!

  31. And so I wanted to make it BETTER

  32. So, I had to ask,

  33. How do I not allow this?

  34. Well...

  35. to prepare a query • postgres.js Didn’t use libpq.

  36. SOURCE: http://www.flickr.com/photos/tanaka/2319325408/

  37. to prepare a query • postgres.js Didn’t use libpq. • Speaks pure protocol

  38. to prepare a query • postgres.js Didn’t use libpq. • Speaks pure protocol • ...So I had to learn the protocol.

  39. Wax on, wax off MONTAGE!

  40. Sadly, reality was a lot less exciting

  41. Complexity Abounds • Simple protocol sends one message to pg • Easy to queue, internally.

  42. REALLY SIMPLE MESSAGE QUEUE QUERY QUERY QUERY QUERY QUERY QUERY RESULTS

  43. Really, though • Simple protocol sends one message to PG • Easy to queue, internally. • Prepared statements require at least 5 • Parse, describe, execute, bind, flush

  44. THIS TYPE OF QUEUE DOESN’T WORK SYNC EXECUTE BIND DESCRIBE PARSE QUERY QUERY

  45. And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response • But it does give responses...

  46. And while all this is happening • You cannot block the main thread

  47. And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response

  48. And while all this is happening • You cannot block the main thread • And postgres expects a synchronous request/response • But it does give responses...

  49. So, we buffer message sets

  50. KIND OF LIKE THIS SIMPLE PREPARED GROUP SIMPLE SIMPLE PREPARED GROUP SIMPLE SIMPLE

More Related