37
The SAM Pattern A Distributed System View of Front-End Architectures J.J. DUBRAY xgen.io

The SAM Pattern: a Distributed System View of Front-End Architectures

Embed Size (px)

Citation preview

Page 1: The SAM Pattern: a Distributed System View of Front-End Architectures

The SAM Pattern

A Distributed System View of Front-End Architectures

J.J. DUBRAYxgen.io

Page 2: The SAM Pattern: a Distributed System View of Front-End Architectures

© 2016 xgen.io

Modern GUIs have become just a Node in a Dynamic Distributed System• your friends, • players • your watch• IoT sensors• …

Page 3: The SAM Pattern: a Distributed System View of Front-End Architectures

Events and CallbacksComposabilityEncapsulationSubscriptionsSeparation of ConcernData Consistency

from

to

Maintainability…

Ingo Mayer et al (https://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf)© 2016 xgen.io

Page 4: The SAM Pattern: a Distributed System View of Front-End Architectures
Page 5: The SAM Pattern: a Distributed System View of Front-End Architectures

© 2016 xgen.io

Page 6: The SAM Pattern: a Distributed System View of Front-End Architectures

The GUI and the Back-End API

© 2016 xgen.io

Page 7: The SAM Pattern: a Distributed System View of Front-End Architectures

The Devs and the Architect

© 2016 xgen.io

Page 8: The SAM Pattern: a Distributed System View of Front-End Architectures

Redux

© 2016 xgen.io

Page 9: The SAM Pattern: a Distributed System View of Front-End Architectures

Angular2

Page 10: The SAM Pattern: a Distributed System View of Front-End Architectures

SCRUM

Source: http://www.bangkokpost.com/learning/advanced/1009037/footbridge-with-powerpole-through-it-fixed

CONSTRUCTION HAD TO BE STARTED OR BUILDER WOULD BE FINED

Page 11: The SAM Pattern: a Distributed System View of Front-End Architectures

Code Reuse

© 2016 xgen.io

Page 12: The SAM Pattern: a Distributed System View of Front-End Architectures

AWSOutage

22-wheel truck downs 47 power poles

Source: http://www.bangkokpost.com/learning/learning-from-news/641220/22-wheel-truck-downs-47-power-poles-18-wheeler-drags-12-year-old-to-death

Page 13: The SAM Pattern: a Distributed System View of Front-End Architectures

After some intense research …

Leading Web Frameworks like React and Angular came up with component Model by 1. simply putting a box around the

code we used to write2. add a change detection

mechanism to re-render components automatically each time their properties change

Page 14: The SAM Pattern: a Distributed System View of Front-End Architectures

Introducing

Functional Reactive Programming

Page 15: The SAM Pattern: a Distributed System View of Front-End Architectures

Functional Reactive Programming•The main idea of FRP is to build programs in a declarative only way, by defining [event] streams, how they are linked together and what happens if a new stream value arrives over time.

Source: http://blog.jhades.org/functional-reactive-programming-for-angular-2-developers-rxjs-and-observables/Jennifer Paykin http://www.cis.upenn.edu/~jpaykin/papers/pkz_LTTT.pdf

© 2016 xgen.io

Page 16: The SAM Pattern: a Distributed System View of Front-End Architectures

… BUT• FRP programs are purely functional•which alleviates the problems of synchronizing state with behavior• [They] can be built with very little to no application state variables, which are in general a source of errors. • To make it clearer: the application does have state,

but that state its typically stored on certain streams or in the DOM, not on the application code itself.

Source: http://blog.jhades.org/functional-reactive-programming-for-angular-2-developers-rxjs-and-observables/

Jennifer Paykin http://www.cis.upenn.edu/~jpaykin/papers/pkz_LTTT.pdf © 2016 xgen.io

Page 17: The SAM Pattern: a Distributed System View of Front-End Architectures

Source: https://twitter.com/sstephenson/status/730039913052176384

© 2016 xgen.io

Page 18: The SAM Pattern: a Distributed System View of Front-End Architectures

Semantics Matter

Source: Lee Byron, Immutable User Interfaceshttps://vimeo.com/album/3953264/video/166790294© 2016 xgen.io

Page 19: The SAM Pattern: a Distributed System View of Front-End Architectures

Angular2

TemplateEvent

Event

Component

Component

Component

Component

Template

Template

Template

reactive()

© 2016 xgen.io

Page 20: The SAM Pattern: a Distributed System View of Front-End Architectures

MobX

ObservableComputable Reaction

Computable

Computable Reaction

ObservableObservableObservable

Observable

Action(Mutation)

Action(Mutation)

© 2016 xgen.io

functional() reactive()

Page 21: The SAM Pattern: a Distributed System View of Front-End Architectures

ThunksSagas

React/Redux

Reducer(Action + Mutation)

Store View

functional() reactive()Action(Intent)

Action(Intent)

© 2016 xgen.io

Page 22: The SAM Pattern: a Distributed System View of Front-End Architectures

Elm

Update(Mutation) View

Command(Action)

Subscription

Subscription

Source: https://www.gitbook.com/book/sporto/elm-tutorial/details© 2016 xgen.io

Task

Task

Task

functional() reactive()

Page 23: The SAM Pattern: a Distributed System View of Front-End Architectures

Cycle.js

Intent$

Intent$

Model$

Intent$

Driver

View$

Driver

© 2016 xgen.io

functional() reactive()

Page 24: The SAM Pattern: a Distributed System View of Front-End Architectures

Over-Network Reactive Dependency Graphs

© 2016 xgen.io

Source: G. Salvaneschi et alhttp://www.guidosalvaneschi.com/attachments/papers/2013_Towards-Distributed-Reactive-Programming_pdf.pdf

Page 25: The SAM Pattern: a Distributed System View of Front-End Architectures

Introducing SAM

- State-Action-Model -

Page 26: The SAM Pattern: a Distributed System View of Front-End Architectures

The SAM Pattern

View

Model

V = f(M) ??

© 2016 xgen.io

Page 27: The SAM Pattern: a Distributed System View of Front-End Architectures

The Paxos ProtocolConsensus based protocol to achieve data consistency in distributed systems?

Source: Mark Chu-Carroll http://www.goodmath.org/blog/2015/01/30/paxos-a-really-beautiful-protocol-for-distributed-consensus/https://en.wikipedia.org/wiki/Paxos_(computer_science)

© 2016 xgen.io

Page 28: The SAM Pattern: a Distributed System View of Front-End Architectures

The SAM Pattern

View

Model

V = f(M) Counter = Counter + 1Counter = Counter + 1var p_counter = counter + 1

counter = p_counter

var p_counter = counter + 1

counter.accept(p_counter)

var p_counter = counter + 1

counter.accept(p_counter).then(learn)

1 Proposers

2 Acceptor(s) 3 Learners

© 2016 xgen.ioDr. Leslie Lamport: TLA+http://research.microsoft.com/en-us/um/people/lamport/pubs/state-machine.pdf

Page 29: The SAM Pattern: a Distributed System View of Front-End Architectures

The SAM Pattern is Unapologetically Focused on Mutation

View

Model

Actions

V = f(Model)

State next-action(Model)

1 Proposers

2 Acceptor(s)

3Learner(s)

© 2016 xgen.io

Page 30: The SAM Pattern: a Distributed System View of Front-End Architectures

State-Action-Model

View

Model

V = f(Model)

next-action(Model)

Action

Action

Action

Actionevent

Unit of Work

Unit of Work

Unit of Work

State

State

State

Component

Component

Page 31: The SAM Pattern: a Distributed System View of Front-End Architectures

State-Action-ModelView

Model

V = f(Model)

next-action(Model)

Action

event

Unit of Work

State

Page 32: The SAM Pattern: a Distributed System View of Front-End Architectures

Programming languages provide no well-defined notion of a program step

Dr. Leslie Lamport

http://research.microsoft.com/en-US/um/people/Lamport/pubs/pluscal.pdf

© 2016 xgen.io

Page 33: The SAM Pattern: a Distributed System View of Front-End Architectures

SAM Pattern

View

Model

Actions

V = f(M)

State

Step

Allowed actions

Allowed proposals

Page 34: The SAM Pattern: a Distributed System View of Front-End Architectures

Unidirectional / Single State Tree Data Flow

Model

Actions

State

V = f(M)

nap(M)

Page 35: The SAM Pattern: a Distributed System View of Front-End Architectures

SAM Isolates APIs from the View

Model

Actions

State

V = f(M)

nap(M)

CUD

R

There is no need for • an immutable model (Redux)• declarative effects (Elm, ~Redux)They create more problems than they solve

Page 36: The SAM Pattern: a Distributed System View of Front-End Architectures

Conclusion•Programming model • Centered on Mutation, not

immutability• True Single State Tree, no

Sagas/Stateful components• Focused on ”what’s allowed”,

not subscriptions• View Components are 100%

decoupled from the application business logic• Functional UI/HTML (code

generation), not templates

•Architecture• Side-effects

friendly• Wiring agnostic• Truly Isomorphic• Action “Hang

back” / Generic Cancellations• 3rd party Actions

(OAuth)