Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Architecting large node.js applications

Architecting large node.js applications

Sergi Mansilla introduces one of the main components to hold Cloud9's architecture together: Architect.

Sergi Mansilla

October 22, 2012
Tweet

More Decks by Sergi Mansilla

Other Decks in Programming

Transcript

  1. large
    Architecting
    node.js
    applications
    Monday, October 22, 12

    View full-size slide

  2. @sergimansilla
    Monday, October 22, 12

    View full-size slide

  3. Program
    ☐ Cloud9 IDE?
    ☐ Growing pains
    ☐ Introducing Architect
    ☐ Lessons learned
    Monday, October 22, 12

    View full-size slide

  4. Normal developers
    Monday, October 22, 12

    View full-size slide

  5. JavaScript Developer
    Monday, October 22, 12

    View full-size slide

  6. Cloud9 IDE - http://c9.io
    Monday, October 22, 12

    View full-size slide

  7. debugging
    Real
    Monday, October 22, 12

    View full-size slide

  8. (Smart!)
    Code completion
    Monday, October 22, 12

    View full-size slide

  9. analysis
    Static
    Monday, October 22, 12

    View full-size slide

  10. Free
    Linux
    VM
    Monday, October 22, 12

    View full-size slide

  11. Bring your own machine
    Monday, October 22, 12

    View full-size slide

  12. Real terminal
    Monday, October 22, 12

    View full-size slide

  13. Collaboration
    Monday, October 22, 12

    View full-size slide

  14. See each other type
    Debug together
    Productivity++
    Monday, October 22, 12

    View full-size slide

  15. Create
    Share Test
    Deploy Run/Debug
    Monday, October 22, 12

    View full-size slide

  16. Program
    ☑ Cloud9 IDE?
    ☐ Growing pains
    ☐ Introducing Architect
    ☐ Lessons learned
    Monday, October 22, 12

    View full-size slide

  17. 10,000s
    of JavaScript
    LOC
    Monday, October 22, 12

    View full-size slide

  18. dynamic,
    typed
    weakly
    language
    Monday, October 22, 12

    View full-size slide

  19. one
    thread
    single
    Monday, October 22, 12

    View full-size slide

  20. Remote VM
    Cloud9 datacenter
    IDE server Remote VM
    Remote VM
    Openshift
    Monday, October 22, 12

    View full-size slide

  21. Pure madness
    Monday, October 22, 12

    View full-size slide

  22. Modularization
    Monday, October 22, 12

    View full-size slide

  23. Black
    box
    coding
    Monday, October 22, 12

    View full-size slide

  24. Monday, October 22, 12

    View full-size slide

  25. Java/.NET
    Node.js
    import(s)
    require
    Monday, October 22, 12

    View full-size slide

  26. abstracting
    away
    Great for
    require
    Monday, October 22, 12

    View full-size slide

  27. application
    modularity
    Not Great for
    require
    Monday, October 22, 12

    View full-size slide

  28. duplicated modules
    maps to folder names
    Relies on FS
    require
    Hard to configure module
    Monday, October 22, 12

    View full-size slide

  29. Dependency error handling
    coding time compile time run time
    KA-BOOM!
    - Server crash
    - Unhappy customers
    - Developer gets fired
    Monday, October 22, 12

    View full-size slide

  30. How to fix it?
    Static dependency list
    Named services
    Easy configuration
    Resolve at startup
    No FS required
    Pass an object
    Monday, October 22, 12

    View full-size slide

  31. Program
    ☑ Cloud9 IDE?
    ☑ Growing pains
    ☐ Introducing Architect
    ☐ Lessons learned
    Monday, October 22, 12

    View full-size slide

  32. Architect
    github.com/c9/architect
    Monday, October 22, 12

    View full-size slide

  33. Architect
    Everything is a plugin
    Plugins can consume plugins
    An application is just a set of plugins
    Monday, October 22, 12

    View full-size slide

  34. Dependency model
    require
    Architect
    runtime
    compile time
    Monday, October 22, 12

    View full-size slide

  35. Dependency error handling
    Architect
    coding time compile time run time
    - Happy customers
    - Developer keeps job
    - Fails before release
    Monday, October 22, 12

    View full-size slide

  36. Monday, October 22, 12

    View full-size slide

  37. Declare entity ‘presenter’ with behavior
    Use ‘presenter’ to do a presentation
    Monday, October 22, 12

    View full-size slide

  38. Presenter Presentation
    Dependency model
    Monday, October 22, 12

    View full-size slide

  39. Express our model
    package.json
    Builds dependency tree
    executing code
    without
    Monday, October 22, 12

    View full-size slide

  40. Monday, October 22, 12

    View full-size slide

  41. What’s next?
    Extract the code
    Wrap in Architect plugin code
    Make two plugins
    Monday, October 22, 12

    View full-size slide

  42. Function
    signature
    Call when done
    Monday, October 22, 12

    View full-size slide

  43. Architect plugin code
    Module.exports
    Options - we’ll get to that
    Imports - everything you ‘consume’
    Register - invoke when done
    Monday, October 22, 12

    View full-size slide

  44. Monday, October 22, 12

    View full-size slide

  45. Monday, October 22, 12

    View full-size slide

  46. Easy to test
    Mock dependencies
    Monday, October 22, 12

    View full-size slide

  47. Assert ‘dance’ is called 11 times
    Monday, October 22, 12

    View full-size slide

  48. No black magic
    Specify dependency model
    Feed architect a config file
    Call ‘createApp’
    Monday, October 22, 12

    View full-size slide

  49. Monday, October 22, 12

    View full-size slide

  50. Monday, October 22, 12

    View full-size slide

  51. Configuration
    Per-plugin options
    No global options object
    Specify in config file
    Monday, October 22, 12

    View full-size slide

  52. Monday, October 22, 12

    View full-size slide

  53. Monday, October 22, 12

    View full-size slide

  54. Options
    Automatically passed in at startup
    Options are also dependencies
    Fail if options aren’t present
    Monday, October 22, 12

    View full-size slide

  55. Monday, October 22, 12

    View full-size slide

  56. Monday, October 22, 12

    View full-size slide

  57. Architect makes you think of your app as
    chunks of functionality
    rather than sets of classes
    Monday, October 22, 12

    View full-size slide

  58. Think ‘chunks of functionality’
    Implicit type constraints
    Keep implementation private
    Swap features instead of interfaces
    Monday, October 22, 12

    View full-size slide

  59. How does Cloud9 use it?
    Open source version
    Local version (OS + sync)
    Hosted version
    Normal
    FTP
    SSH
    Monday, October 22, 12

    View full-size slide

  60. Monday, October 22, 12

    View full-size slide

  61. Swap feature per
    implementation
    On Open source: talk local filesystem
    On FTP: talk FTP library
    On SSH: talk via a SSH bridge
    Monday, October 22, 12

    View full-size slide

  62. Here is something your
    DI framework can’t do
    Monday, October 22, 12

    View full-size slide

  63. IDE instance
    (FTP)
    IDE instance
    (SSH)
    IDE instance
    (Normal)
    IDE instance
    (Normal)
    Single node.js process
    Other code
    (dashboard etc.)
    Monday, October 22, 12

    View full-size slide

  64. Monday, October 22, 12

    View full-size slide

  65. Architect can do
    Multiple instances of same plugin
    Run independently
    But in the same process
    Monday, October 22, 12

    View full-size slide

  66. Monday, October 22, 12

    View full-size slide

  67. Monday, October 22, 12

    View full-size slide

  68. HERE’S SOMETHING COOL
    Monday, October 22, 12

    View full-size slide

  69. Centralized eventbus
    Loose coupling between plugins
    No hard dependencies!
    Can also do inter-context communication
    Monday, October 22, 12

    View full-size slide

  70. Event
    bus
    Plugin
    Other plugin
    Emit event
    React on event
    Monday, October 22, 12

    View full-size slide

  71. Monday, October 22, 12

    View full-size slide

  72. Monday, October 22, 12

    View full-size slide

  73. And now scale up
    Need something inter-server
    Swap it with i.e. Redis PubSub
    Plugins will never notice
    Modular awesomeness!
    Monday, October 22, 12

    View full-size slide

  74. Program
    ☑ Cloud9 IDE?
    ☑ Growing pains
    ☑ Introducing Architect
    ☐ Lessons learned
    Monday, October 22, 12

    View full-size slide

  75. Modularize in feature blocks
    Don’t over engineer
    Don’t create too small blocks
    They aren’t interfaces!
    Monday, October 22, 12

    View full-size slide

  76. Use dependency injection
    Architect (JavaScript)
    StructureMap (.NET)
    Spring (Java)
    Monday, October 22, 12

    View full-size slide

  77. Avoid context switching
    Less code!
    Less errors!
    Less boilerplate!
    Monday, October 22, 12

    View full-size slide

  78. Loose coupling
    Use an event bus
    Smaller dependency graph
    Monday, October 22, 12

    View full-size slide

  79. github.com/c9/architect
    Monday, October 22, 12

    View full-size slide

  80. Happy coding!
    Monday, October 22, 12

    View full-size slide

  81. Sergi Mansilla
    @sergimansilla
    github.com/sergi
    http://c9.io
    Monday, October 22, 12

    View full-size slide