Download - Zotonic - Make it Faster
![Page 1: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/1.jpg)
ZOTONICMake it fast(er)
Marc Worrell — WhatWebWhat / Maximonster
Cross Functional Amsterdam — 2013, Jan 15th.
![Page 2: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/2.jpg)
Let’s make a website
(Just because I need one)
![Page 3: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/4.jpg)
I use <? PHP ?>
Done!
![Page 5: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/5.jpg)
I use <? PHP ?>
Hurray visitors!
![Page 6: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/6.jpg)
I use <? PHP ?>
Oh no! visitors!
![Page 7: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/7.jpg)
I used <? PHP ?>
•What happened?
• I got mentioned on popular blog
• Too many PHP+Apache processes
•Melt down
![Page 8: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/13.jpg)
That is why we made Zotonic
![Page 14: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/14.jpg)
With the help of Open Source, a great community, and ... you?
That is why we made Zotonic
![Page 15: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/19.jpg)
Steps for a request
• Accept
• Parse request
• Dispatch (match host, controller)
• Render template (fetch data)
• Serve result
![Page 20: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/21.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/24.jpg)
Life of a request
• Client side
• Server side
• Templates
• In memory caching
![Page 25: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/30.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/32.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/33.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/34.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/35.jpg)
Database
• Complaint: “It is SQL, it can’t scale”
• Answer: “Wrong question”
![Page 36: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/36.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/37.jpg)
Why PostgreSQL?
(And we serialize most data into a blob anyway)
![Page 38: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/38.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/39.jpg)
(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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/40.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/41.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/42.jpg)
It works
![Page 43: Zotonic - Make it Faster](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/43.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051208/5459f8c2af79590b088b58ef/html5/thumbnails/44.jpg)
¿Questions?