Slide 1

Slide 1 text

Scalable Application rchitecture github.com/mgechev twitter.com/mgechev blog.mgechev.com Minko Gechev

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Hold on for the second edition!

Slide 6

Slide 6 text

Story Time

Slide 7

Slide 7 text

Disclaimer • High-level architecture, doesn’t define interfaces • Suggestions, not rules • Works for me, may not work for you

Slide 8

Slide 8 text

Design Architecture ⚡

Slide 9

Slide 9 text

– Martin Fowler “…decisions that are hard to change…” Architecture

Slide 10

Slide 10 text

High-level constraints of the project

Slide 11

Slide 11 text

Dynamic requirements

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Scalable & flexible data layer

Slide 14

Slide 14 text

Data layer • RESTful API • WebSocket application service • WebRTC data-channel • Various data packages • Bert-RPC • JSON • etc.

Slide 15

Slide 15 text

Scalable team & codebase

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

High-level decisions for the architecture

Slide 18

Slide 18 text

Dynamic Requirements Scalable & flexible data layer Scalable team & codebase

Slide 19

Slide 19 text

Dynamic Requirements Scalable & flexible data layer Scalable team & codebase

Slide 20

Slide 20 text

abstraction |əbˈstrakʃ(ə)n| noun [ mass noun ] … 4 the process of considering something independently of its associations or attributes: the question cannot be considered in abstraction from the historical context in which it was raised.

Slide 21

Slide 21 text

WebRTC Gateway WebSocket Gateway

Slide 22

Slide 22 text

Gateway WebRTC Gateway WebSocket Gateway

Slide 23

Slide 23 text

Dynamic Requirements Scalable & flexible data layer Scalable team & codebase

Slide 24

Slide 24 text

Explicitness

Slide 25

Slide 25 text

Explicitness • Explicit state mutation • Explicit package mapping • Others…

Slide 26

Slide 26 text

Dynamic Requirements Scalable & flexible data layer Scalable team & codebase

Slide 27

Slide 27 text

Consistent state management

Slide 28

Slide 28 text

Consistent state • Domain objects should: • Have a single instance • Explicit update

Slide 29

Slide 29 text

Single instance !=>

Slide 30

Slide 30 text

Single instance !=> Identity map

Slide 31

Slide 31 text

Single instance !=> Identity map Explicit update !=>

Slide 32

Slide 32 text

Single instance !=> Identity map Explicit update !=> Strict conventions

Slide 33

Slide 33 text

redux++

Slide 34

Slide 34 text

redux++

Slide 35

Slide 35 text

Sample Tech Stack • Angular 2 • RxJS • ngrx • TypeScript • ImmutableJS

Slide 36

Slide 36 text

Sample application

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

High-level architecture

Slide 40

Slide 40 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 41

Slide 41 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 42

Slide 42 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 43

Slide 43 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 44

Slide 44 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 45

Slide 45 text

UI components

Slide 46

Slide 46 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 47

Slide 47 text

game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } }

Slide 48

Slide 48 text

game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } }

Slide 49

Slide 49 text

game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } }

Slide 50

Slide 50 text

UI components • Simple presentational logic • As stateless as possible • Delegate business logic to models

Slide 51

Slide 51 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 52

Slide 52 text

class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts

Slide 53

Slide 53 text

game.model.ts class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } }

Slide 54

Slide 54 text

class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts

Slide 55

Slide 55 text

BaseModel Model B Model A State tree Component tree state stream (game$) references (store.select(…))

Slide 56

Slide 56 text

class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts

Slide 57

Slide 57 text

class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts

Slide 58

Slide 58 text

Component Model ngrx

Slide 59

Slide 59 text

Component Model completeGame() ngrx

Slide 60

Slide 60 text

Component Model dispatch(action) ngrx completeGame()

Slide 61

Slide 61 text

Component Model ngrx stateObserver.next(state) dispatch(action) completeGame()

Slide 62

Slide 62 text

Component Model stateObserver.next(state) dispatch(action) ngrx completeGame()

Slide 63

Slide 63 text

class GameModel extends Model { game$: Observable; constructor(protected _store: Store, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts

Slide 64

Slide 64 text

Models (Façade) • Map the state stream • Delegate business logic to reducers • Delegate data-access logic to async services

Slide 65

Slide 65 text

Beware for violations of the SRP

Slide 66

Slide 66 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 67

Slide 67 text

Component Model stateObserver.next(state) dispatch(action) ngrx onProgress()

Slide 68

Slide 68 text

ngrx reducers dispatch(action) State tree Action New state New state

Slide 69

Slide 69 text

game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action: Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; };

Slide 70

Slide 70 text

game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action: Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; };

Slide 71

Slide 71 text

game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action: Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; };

Slide 72

Slide 72 text

game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action: Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; };

Slide 73

Slide 73 text

game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) => game.currentText); } …

Slide 74

Slide 74 text

game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) => game.currentText); } …

Slide 75

Slide 75 text

game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) => game.currentText); } …

Slide 76

Slide 76 text

game.component.html …
{{ currentText$ | async }}

Slide 77

Slide 77 text

game.component.html …
{{ currentText$ | async }}

Slide 78

Slide 78 text

Reducers • Pure functions of the state and an action • Implement the business logic

Slide 79

Slide 79 text

Async Services

Slide 80

Slide 80 text

UI components Façade (Active Record-like) (provides simplified interface to the components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers

Slide 81

Slide 81 text

Remote Service App

Slide 82

Slide 82 text

Remote Service App

Slide 83

Slide 83 text

Remote Service App

Slide 84

Slide 84 text

export abstract class AsyncService { abstract process(data: Action): Observable; } base.async-service.ts

Slide 85

Slide 85 text

export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private _store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts

Slide 86

Slide 86 text

export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private _store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts

Slide 87

Slide 87 text

export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private _store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts

Slide 88

Slide 88 text

Async services • Map actions to commands • Map responses/messages to actions • Use an abstract gateway

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Recap

Slide 91

Slide 91 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name uses as user$

Slide 92

Slide 92 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) uses as user$

Slide 93

Slide 93 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 94

Slide 94 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 95

Slide 95 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 96

Slide 96 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 97

Slide 97 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 98

Slide 98 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 99

Slide 99 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$

Slide 100

Slide 100 text

Async services Business facade Business logic Communication logic Immutable app state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses as user$ uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data)

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 103

Slide 103 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 104

Slide 104 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 105

Slide 105 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 106

Slide 106 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 107

Slide 107 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 108

Slide 108 text

Properties… • Predictable state management • Testable (easy to mock services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events

Slide 109

Slide 109 text

Resources • redux • ngrx • mgv.io/scalable-architecture • mgv.io/scalable-architecture-demo

Slide 110

Slide 110 text

Thank you! github.com/mgechev twitter.com/mgechev blog.mgechev.com