50
Architecture of Ajax Applications Alois Reitbauer, dynaTrace Software

Architecture in Ajax Applications

Embed Size (px)

DESCRIPTION

Ajax applications are different to classical web applications. This presentation covers performance relevant aspects architectures should consider when building ajax applications

Citation preview

Page 1: Architecture in Ajax Applications

Architecture of Ajax ApplicationsAlois Reitbauer, dynaTrace Software

Page 2: Architecture in Ajax Applications

Intro

Page 3: Architecture in Ajax Applications

Architecture?

Page 4: Architecture in Ajax Applications

The Last7 months

Page 5: Architecture in Ajax Applications

Ajax Paradigm

Page 6: Architecture in Ajax Applications

past now

Browser

Server

Where is our code running?

Page 7: Architecture in Ajax Applications

New application runtimes …

Page 8: Architecture in Ajax Applications

Architectures are changing …

vs.

Page 9: Architecture in Ajax Applications

MVC revisited

Model

View

Controller

Web 1.0 Web 2.0

Model

View

Controller

Page 10: Architecture in Ajax Applications

MVC and deployment

Load Time(Static)

Runtime(Dynamic)

Model

View

Controller

Page 11: Architecture in Ajax Applications

Performance

Page 12: Architecture in Ajax Applications

Google includes speed in ranking …

Source: http://googlewebmastercentral.blogspot.com/2010/04/using-site-speed-in-web-search-ranking.html

Page 13: Architecture in Ajax Applications

The impact of performance …

Source: Jake Brutlag and Eric Schuman at Velocity 2009

… on business

Page 14: Architecture in Ajax Applications

Web Performance is defined by …

• Proper Markup• Understanding Browser Specifics

• Markup Processing• DOM• Network• Connectivity

• WAN Remote Communication• Writing proper code

Page 15: Architecture in Ajax Applications

What typically goes wrong

Browser ServerJavaScriptPerformance

HTML Rendering

AJAX/HTTPcalls

Latency

Datavolume Contentconstruction

Network

Caching

Server

Page 16: Architecture in Ajax Applications

The Browser

Page 17: Architecture in Ajax Applications

Co

nn

ecti

on

Po

ol

Browser under the hood

JavaScriptEngine

DOM

Renderer

.html..png

.js

.css.css

Daten

XHR

Co

nn

ecti

on

Mg

r

Page 18: Architecture in Ajax Applications

Network

Page 19: Architecture in Ajax Applications

HTTP is the language of the web …

Browser Server

GET /index.html HTTP/1.1

Host: www.example.net

HTTP/1.1 200 OKContent-Type: text/html

….

Page 20: Architecture in Ajax Applications

… and a cool protocol

• Simple Request/Response• Caching Support• Compression• Secure Communication• State Tracking• Multipart Response• Connection Management• …

Page 21: Architecture in Ajax Applications

Understanding a browser download …

Page 22: Architecture in Ajax Applications

Be aware of the connection limit

Page 23: Architecture in Ajax Applications

Many downloads increase wait time

Page 24: Architecture in Ajax Applications

Core principles of network usage …

• # Requests increases Wait/Connect Time• Merge Resources• Use Spriting

• Content Size increases Transfer/Wait Time• Minify Content• Compress Content• Payload vs. Markup

Page 25: Architecture in Ajax Applications

Redirects and how to avoid them …

ApplicationPage

SecurityService

SecurityLogin

ApplicationPage

First time the usersees something

ApplicationPage

SecurityService

First time the usersees something

AJAX Calls

Page 26: Architecture in Ajax Applications

Clients

Server

Cache per Client

Server providingCaching Information

Proxy Cachefor Many Clients

Serverside Data Cache

Caching in Web Applications

Page 27: Architecture in Ajax Applications

• Date• Defines the timestamp of the resource download

• Expires• Defines how long this resource is valid

• E-Tag• Uniquely identifies a resource

• Cache-Control• Defines caching strategy

• Last-Modified• Defines when the resource was lost modifed

Understanding HTTP headers

Page 28: Architecture in Ajax Applications

E-Tag and Last-Modified

Browser Server

If-Modified-SinceIf-None-Match

200 OK

Content Download

Browser Server

If-Modified-SinceIf-None-Match

304 Not Modified

Resource Changed Resource Not Changed

… we still have wait/DNS/connect/request/server time

Page 29: Architecture in Ajax Applications

max-age and Expires

Browser Server

GET …

max-age:10

Content Download

No download for 10 seconds

* Combine with E-Tag for even more performance

Page 30: Architecture in Ajax Applications

• Request State– State from one request to the next

• Conversational State– State across several requests– Needed for a unit of work

• Session State– State valid and REQUIRED for the whole user session

Types of state

Page 31: Architecture in Ajax Applications

Where to put the state?• HTTP Session

• Consumes server memory• Beware of replication (if possible)

• Cookie• Put small content here• Can be encrypted

• The Browser• Specifically in AJAX applications• Think Rich Client

Page 32: Architecture in Ajax Applications

Rendering

Page 33: Architecture in Ajax Applications

Layouting in the browser

Page 34: Architecture in Ajax Applications

Layouting is done asynchronously

Page 35: Architecture in Ajax Applications

… but not always

Page 36: Architecture in Ajax Applications

DOM

Page 37: Architecture in Ajax Applications

The DOM is slow…

… it is always two components you are working with

Page 38: Architecture in Ajax Applications

Writing to the DOM

• Work with a live DOM element• Bad – very bad - idea

• Fragments, disconnected/hidden nodes• Fast, but not necesssarily fastest

• Directly insert HTML• Adequately fast• Reliably fast in all browsers• Be aware of IE memory leaks• String performance

Page 39: Architecture in Ajax Applications

Building Strings …var html = ‘‘;for (var i = 0; i < persons.length; i++){ var p = persons[i]; html += ‘<tr><td>‘ + p.first + ‘</td><td>‘ + p.last + ‘</td></tr>‘;}$(‘#myTable‘).append(html);

var html = [];var length = persons.lengthfor (var i = 0; i < length; i++){ var p = persons[i]; html.push(‘<tr><td>‘ + p.first + ‘</td><td>‘ + p.last + ‘</td></tr>‘);}$(‘#myTable‘).append(html.join(‘‘));

Page 40: Architecture in Ajax Applications

Reading from the DOM

• $(.funkyClass > *)• Be aware of complex selectors

• Avoid unnecessary lookups• Cache values in variables

• Emulated vs. Native lookups• E.g. getElementByClassName in IE <8

• DOM access causing rendering• calculated styles, …

Page 41: Architecture in Ajax Applications

Markup

Page 42: Architecture in Ajax Applications

Your HTML decides how fast you are<html><head><link rel=‘stylesheet‘ src=‘style.css‘><script src=‘script1.js‘ defer></script><script> // do something that just blocks downloads</script></head><body>Hello World!<script src=‘script2.js‘ defer></script></body></html>

Page 43: Architecture in Ajax Applications

<td style="padding-left:10px; overflow: hidden; white-space: nowrap;">

Inline CSS - A real world sample

* 9000= 2 MB

Page 44: Architecture in Ajax Applications

AJAX

Page 45: Architecture in Ajax Applications

Types of AJAX Interactions

• XMLHttpRequest• Retrieve an type of content

• <script src=‘app.mydomain …• Enable domain sharding• returns JSONP

• <img src=‘beacon.mydomain ….• Write-only communication

Page 46: Architecture in Ajax Applications

Content Formats

• XML• Universal but slow

• JSON• Native to JavaScript

• JSONP• When using script tags

• HTML• Directly insertable

• Custom Format• Array structures – easy to process

Page 47: Architecture in Ajax Applications

Wrap Up

Page 48: Architecture in Ajax Applications

Performance Checklist

• Caching• All headers properly set?

• Network Usage• Number/size of Downloads• Compression• Persistent Connections

• HTML Structure• Selector Performance• Unwanted Layouting• Server Times

Page 49: Architecture in Ajax Applications

Books you should read

Page 50: Architecture in Ajax Applications

[email protected] Mailblog.dynatrace.com Blog

AloisReitbauer Twitter

http://ajax.dynatrace.com