Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan...

75
Scale-proof Frontend: a real world architecture 28 Novembre 2014 Milano

description

Quali problemi si possono riscontrare quando si progetta e sviluppa un'applicazione web che dovrà servire centinaia di migliaia di utenti? Hit per minute, scalabilità, e altro jargon sistemistico visto dal punto di vista dello sviluppo front end.

Transcript of Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan...

Page 1: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Scale-proof Frontend:a real world architecture

28 Novembre 2014 Milano

Page 2: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

IvanPrignano

@iprignano

NicolaRacco

@nicolaracco

Front-end developer Back-end developer

intro: ivan

Page 3: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

www.mikamai.com

UNCONVENTIONAL EVERYTHING

Page 4: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 5: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 6: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 7: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 8: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 9: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

27 M giocatori / giorno 67 M giocatori / mese

Page 10: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 11: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 12: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Il Progetto

Nicola ora

Page 13: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 14: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 15: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

• Tornei a più fasi

• Generazione dinamica delle bracket

• Backend per approvazione utenti e gestione torneo

• Scalabilità in base al carico

Sistema di gestione e visualizzazione di tornei

Page 16: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

2

Page 17: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

8

Page 18: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

BDD

Page 19: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 20: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Stack tecnologico

Ivan ora

Page 21: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 22: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Ruby on Rails

“Ruby on Rails is an open-source web framework that’s optimized for programmer happiness and sustainable productivity.

It lets you write beautiful code by favoring convention over configuration.”

WEB FRAMEWORK

• Minificazione asset out-of-the-box

• Turbolinks (pjax) out-of-the-box

Page 23: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

SassCSS PREPROCESSOR

@import "mixins"

$red: #ff0000

.box width: 100%

.box-title color: $red @include boldness()

.box { width: 100%;}

.box .box-title { color: #ff0000;

font-weight: bold; text-transform: uppercase;

}

Page 24: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

SlimTEMPLATING ENGINE

.box.box-title Sono un titolo!#box-content = @content

<div class=“box”><div class=“box-title”> Sono un titolo!</div><div id=“box-content”>

Sono un contenuto!</div>

</div>

Page 25: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

CoffeeScriptCOMPILE-TO-JS LANGUAGE

class Brackets.Tooltips constructor: (el) -> alert(el)

new Brackets.Tooltips('.elemento')

Brackets.Tooltips = (function() { function Tooltips(el) { alert(el); }

return Tooltips;

})();

new Brackets.Tooltips('.elemento');

Page 26: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Come funziona

ancora Ivan

Page 27: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Registrazione utente Approvazione team

Chiusura registrazioniPubblicazione torneo

Avanzamento di fase Annuncio vincitori

Page 28: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Registrazione utente Approvazione team

Chiusura registrazioniPubblicazione torneo

Avanzamento di fase Annuncio vincitori

Page 29: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Problema:la generazione dell’albero

Nicola!

Page 30: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

JSON

JSON EVERYWHERE

Page 31: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

209,"with_recording":false,"score":[0,0],"result_type":5,"best_of":1},"from":[{"position":97,"payload":

{"id":null,"with_recording":false,"score":[0,0],"result_type":5}},{"position":99,"payload":{"id":209,"with_recording":false,"score":

[0,0],"result_type":5}}]},{"position":102,"payload":{"id":88,"with_recording":false,"score":[1,0],"result_type":3,"best_of":

1},"from":[{"position":101,"payload":{"id":88,"with_recording":false,"score":[0,0],"result_type":5}},

{"position":103,"payload":{"id":77,"with_recording":false,"score":[0,0],"result_type":5}}]}]},{"position":108,"payload":{"id":

151,"with_recording":true,"score":[0,1],"result_type":2,"best_of":1},"from":[{"position":106,"payload":{"id":

168,"with_recording":false,"score":[0,0],"result_type":5,"best_of":1},"from":[{"position":105,"payload":

{"id":null,"with_recording":false,"score":[0,0],"result_type":5}},{"position":107,"payload":{"id":

168,"with_recording":false,"score":[0,0],"result_type":5}}]},{"position":110,"payload":{"id":151,"with_recording":true,"score":

[0,1],"result_type":2,"best_of":1},"from":[{"position":109,"payload":{"id":84,"with_recording":false,"score":

Page 32: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

HTML plain? Perché no!PRIMA PROVA

Page 33: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 34: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Semplice da scrivere e stilare

Facile da testare

PRO

Poco performante su grandi numeri

Zoom limitato

CONTRO

Page 35: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

SVG!SECONDA PROVA

Page 36: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 37: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 38: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 39: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 40: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 41: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Semplice quanto la versione HTML

Uber Zooming

PRO

Poco performante su device mobile

CONTRO

Page 42: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 43: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Registrazione utente Approvazione team

Chiusura registrazioniPubblicazione torneo

Avanzamento di fase Annuncio vincitori

Ivan

Page 44: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Registrazione utente Approvazione team

Chiusura registrazioniPubblicazione torneo

Avanzamento di fase Annuncio vincitori

Page 45: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Problema:modali per il match report

Ivan

Page 46: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 47: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 48: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Preloading static data

$.when( @startGetAllStaticData()... ).done => @staticData = { champions: @champions spells: @summonerSpells items: @items versions: @versions }

# Show the brackets!

Page 49: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Preloading static data

$.when( @startGetAllStaticData()... ).done => @staticData = { champions: @champions spells: @summonerSpells items: @items versions: @versions }

# Show the brackets!

Page 50: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Preloading static data

$.when( @startGetAllStaticData()... ).done => @staticData = { champions: @champions spells: @summonerSpells items: @items versions: @versions }

# Show the brackets!

Page 51: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

API Proxy

Cache delle chiamate

Nel caso le API ufficiali fossero down, il servizio funziona comunque

1 chiamata dal frontend = multiple sul backend

Brackets Proxy API

Page 52: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

history.pushState()

https://dominio.com/brackets/1

Page 53: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

history.pushState()

https://dominio.com/brackets/1/battles/64

Page 54: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Share dinamico

Page 55: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Share dinamico

Page 56: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 57: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Inoltre…

Nicola

Page 58: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 59: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

JSON

Page 60: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 61: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014
Page 62: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Risultati

Nicola

Page 63: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

da 2 a 16.384128^2

Page 64: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

~10.350 connessioniConcurrency di 300

Page 65: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

<0.05s

Page 66: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Possibili miglioramenti

Ivan

Page 67: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Static data spriting

Page 68: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

SVG spriting

Page 69: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

SSSSVG 2000Supercool Server-side Scalable Vector Graphics 2000

Page 70: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Offloading della creazione

delle SVG sul server

Page 71: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Conclusioni

Page 72: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Premature optimisation

!==

Unnecessary optimisation

Nicola

Page 73: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Refactor often, refactor early

Ivan

Page 74: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

CACHE

ALL THE THINGS

Page 75: Scale-proof Frontend: a real world architecture - Ivan Prignano, Nicola Racco - Codemotion Milan 2014

Domande?