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 Slide

  2. @sergimansilla
    Monday, October 22, 12

    View Slide

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

    View Slide

  4. Normal developers
    Monday, October 22, 12

    View Slide

  5. JavaScript Developer
    Monday, October 22, 12

    View Slide

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

    View Slide

  7. debugging
    Real
    Monday, October 22, 12

    View Slide

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

    View Slide

  9. analysis
    Static
    Monday, October 22, 12

    View Slide

  10. Free
    Linux
    VM
    Monday, October 22, 12

    View Slide

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

    View Slide

  12. Real terminal
    Monday, October 22, 12

    View Slide

  13. Collaboration
    Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  19. one
    thread
    single
    Monday, October 22, 12

    View Slide

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

    View Slide

  21. Pure madness
    Monday, October 22, 12

    View Slide

  22. Modularization
    Monday, October 22, 12

    View Slide

  23. Black
    box
    coding
    Monday, October 22, 12

    View Slide

  24. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

  36. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View 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 Slide

  44. Monday, October 22, 12

    View Slide

  45. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  49. Monday, October 22, 12

    View Slide

  50. Monday, October 22, 12

    View Slide

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

    View Slide

  52. Monday, October 22, 12

    View Slide

  53. Monday, October 22, 12

    View Slide

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

    View Slide

  55. Monday, October 22, 12

    View Slide

  56. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  60. Monday, October 22, 12

    View 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 Slide

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

    View 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 Slide

  64. Monday, October 22, 12

    View Slide

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

    View Slide

  66. Monday, October 22, 12

    View Slide

  67. Monday, October 22, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  71. Monday, October 22, 12

    View Slide

  72. Monday, October 22, 12

    View 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 Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  80. Happy coding!
    Monday, October 22, 12

    View Slide

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

    View Slide