+ All Categories
Home > Documents > Zotonic - Make it Faster

Zotonic - Make it Faster

Date post: 01-Nov-2014
Category:
Upload: marc-worrell
View: 356 times
Download: 0 times
Share this document with a friend
Description:
Presentation at the Cross Functional Amsterdam meetup Jan 15th, 2013.Performance optimizations and considerations for the Erlang CMS and Framework Zotonic.
44
ZOTONIC Make it fast(er) Marc Worrell — WhatWebWhat / Maximonster Cross Functional Amsterdam — 2013, Jan 15th.
Transcript
Page 1: Zotonic - Make it Faster

ZOTONICMake it fast(er)

Marc Worrell — WhatWebWhat / Maximonster

Cross Functional Amsterdam — 2013, Jan 15th.

Page 2: Zotonic - Make it Faster

Let’s make a website

(Just because I need one)

Page 3: Zotonic - Make it Faster

I have <? PHP ?>

It is on this machine.

Everyone uses it.

So it must be good.

Let’s use it... (and think later)

Page 4: Zotonic - Make it Faster

I use <? PHP ?>

Done!

Page 5: Zotonic - Make it Faster

I use <? PHP ?>

Hurray visitors!

Page 6: Zotonic - Make it Faster

I use <? PHP ?>

Oh no! visitors!

Page 7: Zotonic - Make it Faster

I used <? PHP ?>

•What happened?

• I got mentioned on popular blog

• Too many PHP+Apache processes

•Melt down

Page 8: Zotonic - Make it Faster

I can use <? PHP ?>

•Of course you can

• Use more hardware

• Use caching proxy

• Use xyz and a bit of abc

• Add complexity

• And keep it all running, all the time

Page 9: Zotonic - Make it Faster

I can use ... (fill in)

• Same for RoR, Django, ...

• Problem is not that you can’t scale

• The problem is that you need to scale immediately

Page 10: Zotonic - Make it Faster

What happened?

•Many people followed popular link

• A process per request

• Death by too many processes

• ... doing the same thing!

• Known as the “Slash Dot Effect”

Page 11: Zotonic - Make it Faster

Is this typical?

•Web sites are quite small (less than a million pages)

• Except for a couple of huge ones

• Not visited that much (less than 10 pages per second)

• Unless linked from popular place

• Relative small set of “hot data”

Page 12: Zotonic - Make it Faster

Then we just cache those pages!?

•Modern web, realtime

• Push, Web Sockets, Personalization

• That is open connections

•More xyz and abc needed

• And... cache invalidation

Page 13: Zotonic - Make it Faster

That is why we made Zotonic

Page 14: Zotonic - Make it Faster

With the help of Open Source, a great community, and ... you?

That is why we made Zotonic

Page 15: Zotonic - Make it Faster

Zotonic because ...

• A web server should easily handle the load of 99.9999% of all web sites.

• You shouldn’t be bothering xyz and abc to make your site real time

• The front end developer needs to be happy and in the driver’s seat

• Do more with less hardware (and watts)

Page 16: Zotonic - Make it Faster

Erlang because ...

•Many parallel connections

• Share everything (more later)

•Modern CPUs are multi core, your software should be using them

• Fault tolerance makes a developer’s life easier

• It is stable, very stable

Page 17: Zotonic - Make it Faster

Make it efficient

• Start with a monorail solution

• Fast enough for that 99.9999% of sites

• Rethink the way requests are handled

Page 18: Zotonic - Make it Faster

The stack

'DWDEDVH

+773�VHUYHU

&RQWUROOHUV

7HPSODWHV

6HUYLFHV���$

3,

:HEVRFNHWV

0RGHOV

0LGGOHZDUH�SURFHVVHV

�FDFKLQJ��VHVVLRQV��UHQGHUHU��LPDJH�UHVL]HU��PRGXOH�

PDQDJHU��WUDQVODWLRQ�WDEOHV������

85/�GLVSDWFKHU

Page 19: Zotonic - Make it Faster

Steps for a request

• Accept

• Parse request

• Dispatch (match host, controller)

• Render template (fetch data)

• Serve result

Page 20: Zotonic - Make it Faster

Where is time spent?

• Simple request: 6000/sec

• Lots of content: 10/sec (or less)

• Fetching data and rendering should be optimized

Page 21: Zotonic - Make it Faster

What takes time?

• Fetch data from database

• Simple query round trip is 1 to 10 msec

• Fetch data from a caching server

• Network round trip is 0.5 msec

• Don’t hit the network or the database

Page 22: Zotonic - Make it Faster

What saves time?

• Don’t repeat things that you can do once a long time ago

• Like HTML escaping and content filtering

• Zotonic stores sanitized content

Page 23: Zotonic - Make it Faster

What saves ...?

• Combine similar actions into one

• Especially when they are happening at the same time

• Think of requests, db results, calculations etc. etc.

• Share database connections, protect your database

Page 24: Zotonic - Make it Faster

Life of a request

• Client side

• Server side

• Templates

• In memory caching

Page 25: Zotonic - Make it Faster

Client side

• Let client (and proxies) cache css, javascript, images etc.

• Combine css and javascript requests:http://example.org/lib/bootstrap/css/bootstrap~bootstrap-responsive~bootstrap-base-site~/css/jquery.loadmask~z.growl~z.modal~site~63523081976.css

Page 26: Zotonic - Make it Faster

Server side

• File requests are easily cached

• Checks on timestamp, modification dates

• Cache both compressed and un-­compressed version

• Still access control checks for content (images, pdfs etc.)

Page 27: Zotonic - Make it Faster

Templates

• Do all the model interactions

• Drive page rendering

• Compiled into Erlang byte code

•Whole and partial caching possible

• Cached results are still dynamic

Page 28: Zotonic - Make it Faster

In memory caching

• Two layers

•Memo cache in process dictionary of request handler

• Central shared cache for whole site: depcache

Page 29: Zotonic - Make it Faster

Memo cache

• In process heap of request handler

•Quick access to often used values

• Resources, ACL checks etc.

• Flushed on writes and when growing too big

• Always enabled when rendering templates

Page 30: Zotonic - Make it Faster

Depcache

• Central cache per site

• Key dependencies for consistency

• Garbage collector thread

•Memo functionality for sharing results between processes

Page 31: Zotonic - Make it Faster

Erlang VM considerations

• Cheap processes

• Expensive data copying on messages

• Binaries have their own heap

• String processing is expensive (as in any language)

Page 32: Zotonic - Make it Faster

Erlang VM and Zotonic

• Big data structure #context{}

• Do most work in a single process

• Prune when messaging

• Don’t pass #context{} when messaging (use interface functions)

•Messaging binaries is ok

Page 33: Zotonic - Make it Faster

Aside: webmachine

• HTTP protocol abstraction

• Great for writing controllers

• Needed some work for Zotonic:

• Dispatch list copying

• Memo of some lookups

• Optimizations (process dictionary removal, combine data structures)

Page 34: Zotonic - Make it Faster

Slam dunk protection

• Happens on startup, change of images, templates, cache flush etc.

• Let individual requests fail but system continue

• Methods:

• Single template compiler

• Single image resize process

• Memo cache (share computation)

Page 35: Zotonic - Make it Faster

Database

• Complaint: “It is SQL, it can’t scale”

• Answer: “Wrong question”

Page 36: Zotonic - Make it Faster

Why PostgreSQL?

• Great stability

• Scales good with multi core

•Mature driver

• Full text indexing

• SQL gives good query support

• Known problems and performance

Page 37: Zotonic - Make it Faster

Why PostgreSQL?

(And we serialize most data into a blob anyway)

Page 38: Zotonic - Make it Faster

NoSQL?

• Tooling (backup, command line)

• Indexing has unproven performance

•Moving target

• Vendor lock in -­ too big differences

• Unproven disk stores

Page 39: Zotonic - Make it Faster

(No)SQL

• Stop worrying

•Optimize the layers above your data store

• Think how you query your data

• Think how you protect your data

Page 40: Zotonic - Make it Faster

Future

• Use more binaries (exchange MochiWeb?)

• Faster template compilation and startup

• Pluggable databases

• Better resource pooling

• Circuit breakers for stability

• Add options to use stale data

Page 41: Zotonic - Make it Faster

Future: zynamo

• Distributed version of Zotonic

• For better availability

• ... and scale to more data

• Each node is created equal

• Still SQL stores

•Ongoing work

Page 42: Zotonic - Make it Faster

It works

Page 43: Zotonic - Make it Faster

Come and join us!

• See us at http://zotonic.com/ (we have a movie)

• Follow us at @zotonic

• Join the community, we have exciting stuff to do

Page 44: Zotonic - Make it Faster

¿Questions?


Recommended