Slide 1

Slide 1 text

Creating scalable Web Applications @BastianHofmann

Slide 2

Slide 2 text

The usual Scaling Talk

Slide 3

Slide 3 text

Big Data Servers Caching Query Optimization Database Sharding Load Balancing Indices

Slide 4

Slide 4 text

But what about the frontend?

Slide 5

Slide 5 text

Rendering performance Asset loading Content Delivery Networks HTTP Caching Minifying JavaScript Image Compression Sprites Smooth animations

Slide 6

Slide 6 text

This talk will not be about any of this!

Slide 7

Slide 7 text

Application architecture

Slide 8

Slide 8 text

Structure

Slide 9

Slide 9 text

Code and component re-use

Slide 10

Slide 10 text

Rapid Development

Slide 11

Slide 11 text

Large code-bases

Slide 12

Slide 12 text

Frameworks to the rescue

Slide 13

Slide 13 text

?

Slide 14

Slide 14 text

Most Web Frameworks are incomplete

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

webserver HTML browser JS

Slide 24

Slide 24 text

e duplication de duplication ode duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplicatio code duplicat code duplic code dup code du code cod co co

Slide 25

Slide 25 text

So?

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Questions? Ask!

Slide 35

Slide 35 text

http://speakerdeck.com/u/bastianhofmann

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Mojito http://developer.yahoo.com/cocktails/mojito/

Slide 38

Slide 38 text

Meteor http://www.meteor.com

Slide 39

Slide 39 text

https://github.com/bashofmann/meteor_shoutbox_demo

Slide 40

Slide 40 text

DEMO

Slide 41

Slide 41 text

What about existing applications?

Slide 42

Slide 42 text

Legacy Code

Slide 43

Slide 43 text

Incremental Refactoring

Slide 44

Slide 44 text

many roads

Slide 45

Slide 45 text

The next SoundCloud http://backstage.soundcloud.com/2012/06/building-the- next-soundcloud/

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

status quo

Slide 48

Slide 48 text

webserver loadbalancer pgsql memcached mongodb services

Slide 49

Slide 49 text

webserver

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Large, Old Codebase

Slide 52

Slide 52 text

MVC

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

complicated routing

Slide 55

Slide 55 text

PHP templates

Slide 56

Slide 56 text

lazy DB queries in view

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Duplication and only some Code re-use

Slide 59

Slide 59 text

We can do better

Slide 60

Slide 60 text

Components

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Self contained

Slide 63

Slide 63 text

Can be addressed and rendered separately

Slide 64

Slide 64 text

Server JS Browser JSON HTML HTML

Slide 65

Slide 65 text

JS is part of the component

Slide 66

Slide 66 text

Share code between server and client

Slide 67

Slide 67 text

Templates, Validation, Entities,...

Slide 68

Slide 68 text

It needs to be fast

Slide 69

Slide 69 text

We called them Widgets

Slide 70

Slide 70 text

PHP Controller Mustache Template JavaScript view class Widget Providing data Handling browser events Displaying data

Slide 71

Slide 71 text

Why no models in the frontend? http://backbonejs.org/#Model

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 74

Slide 74 text

Remember: self contained

Slide 75

Slide 75 text

Do not fetch data directly

Slide 76

Slide 76 text

Sssssssslllloooooowww

Slide 77

Slide 77 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu Account Account Account Account Account Publication1 Publication2 Publication3

Slide 78

Slide 78 text

Require stuff

Slide 79

Slide 79 text

http://www.infoq.com/presentations/Evolution-of-Code- Design-at-Facebook/

Slide 80

Slide 80 text

The Preparer

Slide 81

Slide 81 text

Requirements

Slide 82

Slide 82 text

Resolver

Slide 83

Slide 83 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 84

Slide 84 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 85

Slide 85 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 86

Slide 86 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

EntityRequirement

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Request Cache

Slide 91

Slide 91 text

Multi-GET

Slide 92

Slide 92 text

ServiceRequirement

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Required / Optional

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Hides storage specific logic from controllers

Slide 97

Slide 97 text

Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces Connector Implementations

Slide 98

Slide 98 text

Widget Widget Widget Widget Preparer Fetch Requirements Resolver Resolver Services Connector Interfaces Connector Implementations

Slide 99

Slide 99 text

Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces Connector Implementations Batch requirements and pass them to resolvers

Slide 100

Slide 100 text

Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces Connector Implementations Call Services as effective as possible (Multi-GET,...)

Slide 101

Slide 101 text

Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces Connector Implementations Attach fetched data to Requirements and pass them back to the preparer

Slide 102

Slide 102 text

Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces Connector Implementations Distribute fetched data to the widgets that required it

Slide 103

Slide 103 text

Requirement doesn't need to be only data

Slide 104

Slide 104 text

WidgetRequirement

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

RequestDataRequirement

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

Data dependencies within a widget

Slide 109

Slide 109 text

=> Callbacks

Slide 110

Slide 110 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu CALLBACK CALLBACK CALLBACK

Slide 111

Slide 111 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu CALLBACK CALLBACK CALLBACK

Slide 112

Slide 112 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu CALLBACK CALLBACK CALLBACK

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

PHP 5.5 Generators https://wiki.php.net/rfc/generators

Slide 115

Slide 115 text

public function collect() { yield array( new EntityRequirement( 'account', Account::class, array('accountId' => $this->requestContect->getAccountId()) ), ); yield array( new ServiceRequirement( 'scienceDisciplines', AccountService::class, 'getScienceDisciplines', array('account' => $this->account) ) ); }

Slide 116

Slide 116 text

Data dependencies between Widgets

Slide 117

Slide 117 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 118

Slide 118 text

Prefills

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

Rendering

Slide 122

Slide 122 text

HTML

Slide 123

Slide 123 text

JSON

Slide 124

Slide 124 text

Templates

Slide 125

Slide 125 text

Mustache } http://mustache.github.com/

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

Helper Methods

Slide 128

Slide 128 text

• nl2br • truncate • pluralize • wordwrap • highlight • ...

Slide 129

Slide 129 text

http://pecl.php.net/package/v8js V8js

Slide 130

Slide 130 text

JavaScript

Slide 131

Slide 131 text

WidgetViews

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

Loading widgets from JavaScript

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

Rendering callbacks

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

Benefits

Slide 138

Slide 138 text

Enables developers to only focus on their components

Slide 139

Slide 139 text

Easier Refactoring

Slide 140

Slide 140 text

Error Handling

Slide 141

Slide 141 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 142

Slide 142 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu EXCEPTION

Slide 143

Slide 143 text

Profile Publications Publication Publication Publication LeftColumn Image Instiution Menu

Slide 144

Slide 144 text

Testing

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

Bandit Algorithm http://untyped.com/untyping/2011/02/11/stop-ab-testing- and-make-out-like-a-bandit/

Slide 147

Slide 147 text

Caching of Components

Slide 148

Slide 148 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Instiution Menu

Slide 149

Slide 149 text

Faster UX

Slide 150

Slide 150 text

window.history.pushState

Slide 151

Slide 151 text

Conclusion?

Slide 152

Slide 152 text

Think about your architecture

Slide 153

Slide 153 text

Refactor and make it better continuously

Slide 154

Slide 154 text

Frontend and backend are part of the same application

Slide 155

Slide 155 text

Don't rewrite your whole codebase in one go

Slide 156

Slide 156 text

h"p://twi"er.com/Bas2anHofmann h"p://profiles.google.com/bashofmann h"p://lanyrd.com/people/Bas2anHofmann h"p://speakerdeck.com/u/bas2anhofmann h"ps://github.com/bashofmann [email protected]