Architecting large Node.js applications

Preview:

DESCRIPTION

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

Citation preview

largeArchitecting

node.jsapplications

Monday, October 22, 12

@sergimansilla

Monday, October 22, 12

P r o g r a m

☐ C l o u d 9 I D E ?

☐ G r o w i n g p a i n s

☐ I n t r o d u c i n g A r c h i te c t

☐ Le s s o n s l e a r n e d

Monday, October 22, 12

Normal developers

Monday, October 22, 12

JavaScript Developer

Monday, October 22, 12

Cloud9 IDE - http://c9.io

Monday, October 22, 12

debuggingReal

Monday, October 22, 12

(Smart!)Code completion

Monday, October 22, 12

analysisStatic

Monday, October 22, 12

FreeLinux VM

Monday, October 22, 12

Bring your own machineMonday, October 22, 12

Real terminal

Monday, October 22, 12

Collaboration

Monday, October 22, 12

See each other typeDebug togetherProductivity++

Monday, October 22, 12

Create

Share Test

Deploy Run/Debug

Monday, October 22, 12

P r o g r a m

☑ C l o u d 9 I D E ?

☐ G r o w i n g p a i n s

☐ I n t r o d u c i n g A r c h i te c t

☐ Le s s o n s l e a r n e d

Monday, October 22, 12

10,000s

of JavaScriptLOC

Monday, October 22, 12

dynamic,typedweakly

languageMonday, October 22, 12

onethreadsingle

Monday, October 22, 12

Remote VM

Cloud9 datacenter

IDE server Remote VM

Remote VM

Openshift

Monday, October 22, 12

Pure madness

Monday, October 22, 12

ModularizationMonday, October 22, 12

Blackboxcoding

Monday, October 22, 12

Monday, October 22, 12

Java/.NET

Node.js

import(s)

require

Monday, October 22, 12

abstractingawayGreat for

require

Monday, October 22, 12

applicationmodularity

Not Great forrequ

ire

Monday, October 22, 12

duplicated modulesmaps to folder names

Relies on FSrequ

ire

Hard to configure module

Monday, October 22, 12

Dependency error handling

coding time compile time run time

KA-BO

OM!

- Server crash- Unhappy customers- Developer gets fired

Monday, October 22, 12

How to fix it?

Static dependency list

Named services

Easy configuration

Resolve at startup

No FS required

Pass an object

Monday, October 22, 12

P r o g r a m

☑ C l o u d 9 I D E ?

☑ G r o w i n g p a i n s

☐ I n t r o d u c i n g A r c h i te c t

☐ Le s s o n s l e a r n e d

Monday, October 22, 12

Architect

github.com/c9/architect

Monday, October 22, 12

Architect

Everything is a plugin

Plugins can consume plugins

An application is just a set of plugins

Monday, October 22, 12

Dependency model

require

Architect

runtime

compile time

Monday, October 22, 12

Dependency error handlingArchitect

coding time compile time run time

- Happy customers- Developer keeps job- Fails before release

Monday, October 22, 12

Monday, October 22, 12

Declare entity ‘presenter’ with behaviorUse ‘presenter’ to do a presentation

Monday, October 22, 12

Presenter Presentation

Dependency model

Monday, October 22, 12

Express our model

package.json

Builds dependency tree

executing codewithout

Monday, October 22, 12

Monday, October 22, 12

What’s next?

Extract the codeWrap in Architect plugin code

Make two plugins

Monday, October 22, 12

Function signature

Call when done

Monday, October 22, 12

Architect plugin code

Module.exportsOptions - we’ll get to thatImports - everything you ‘consume’Register - invoke when done

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

Easy to testMock dependencies

Monday, October 22, 12

Assert ‘dance’ is called 11 times

Monday, October 22, 12

No black magic

Specify dependency modelFeed architect a config file

Call ‘createApp’

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

Configuration

Per-plugin optionsNo global options objectSpecify in config file

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

Options

Automatically passed in at startupOptions are also dependencies

Fail if options aren’t present

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

Architect makes you think of your app as chunks of functionality

rather than sets of classes

Monday, October 22, 12

Think ‘chunks of functionality’

Implicit type constraintsKeep implementation private

Swap features instead of interfaces

Monday, October 22, 12

How does Cloud9 use it?

Open source version

Local version (OS + sync)

Hosted version

Normal

FTP

SSH

Monday, October 22, 12

Monday, October 22, 12

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

Here is something your DI framework can’t do

Monday, October 22, 12

IDE instance(FTP)

IDE instance(SSH)

IDE instance(Normal)

IDE instance(Normal)

Single node.js process

Other code (dashboard etc.)

Monday, October 22, 12

Monday, October 22, 12

Architect can do

Multiple instances of same pluginRun independently

But in the same process

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

HERE’S SOMETHING COOL

Monday, October 22, 12

Centralized eventbus

Loose coupling between plugins

No hard dependencies!

Can also do inter-context communication

Monday, October 22, 12

Eventbus

Plugin

Other plugin

Emit event

React on event

Monday, October 22, 12

Monday, October 22, 12

Monday, October 22, 12

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

P r o g r a m

☑ C l o u d 9 I D E ?

☑ G r o w i n g p a i n s

☑ I n t r o d u c i n g A r c h i te c t

☐ Le s s o n s l e a r n e d

Monday, October 22, 12

Modularize in feature blocks

Don’t over engineerDon’t create too small blocksThey aren’t interfaces!

Monday, October 22, 12

Use dependency injection

Architect (JavaScript)StructureMap (.NET)Spring (Java)

Monday, October 22, 12

Avoid context switching

Less code!Less errors!Less boilerplate!

Monday, October 22, 12

Loose coupling

Use an event bus

Smaller dependency graph

Monday, October 22, 12

github.com/c9/architect

Monday, October 22, 12

Happy coding!

Monday, October 22, 12

Sergi Mansilla@sergimansilla

github.com/sergi

http://c9.io

Monday, October 22, 12

Recommended