2. Scaling Web Applications
Mihai Oaida
Senior Web Developer @ www.imobiliare.ro
Student , Politehnica Timișoara
Web technologies instructor
www.tehnologii-web.ro
Mihai Oaida <mihai.oaida@gmail.com> 00
3. Scaling Web Applications
We will talk about ...
Facts
Theory + Myths
Architecture
Static content
Dynamic content
Caching
Mihai Oaida <mihai.oaida@gmail.com> 01
4. Scaling Web Applications
Web application facts
Mostly stateless
Very read intensive ( > 90% )
Not so dynamic
Can be fine grained
Highly scalable, if designed to :)
Mihai Oaida <mihai.oaida@gmail.com> 02
5. Scaling Web Applications
Hardware facts
Limited context switching
Limited opened files
Very limited random IO
Limited sequential IO
Limited RAM
Mihai Oaida <mihai.oaida@gmail.com> 03
6. Scaling Web Applications
Scalability vs Performance
Scalability
how many
Performance
how fast
Scalability + Performance
how many and how fast
Mihai Oaida <mihai.oaida@gmail.com> 04
7. Scaling Web Applications
Vertical vs Horizontal
Vertical Scaling
Bigger server
2X capacity != 2x price
Horizontal Scaling
More identical servers
2X capacity ≈ 2x price
Mihai Oaida <mihai.oaida@gmail.com> 05
8. Scaling Web Applications
Benchmarking and Profiling
Benchmarking
How fast a page loads, under certain
conditions
Profiling
In a page execution, how much time each
component consumes
Mihai Oaida <mihai.oaida@gmail.com> 06
9. Scaling Web Applications
Benchmarking and Profiling
Benchmarking
Page / site performance?
Page / site scalability ?
Profiling
What is the bottleneck?
Mihai Oaida <mihai.oaida@gmail.com> 07
10. Scaling Web Applications
Benchmarking and Profiling
Prepare tests
Expect results
Run repeatable tests
Analyse data
Interpret data correctly !!!
Mihai Oaida <mihai.oaida@gmail.com> 08
11. Scaling Web Applications
Architecture
Web server – dynamic, static content
Database server
Caching server
+ load balancer
+ network equipment
Mihai Oaida <mihai.oaida@gmail.com> 09
12. Scaling Web Applications
Static content - HTTP optimization
Use Expires and Last-Modified headers
Use sprites for images
Merge css files
Merge js files
Use compression – gzip
Use multiple subdomains
img1.tehnologii-web.ro
img2.tehnologii-web.ro
Mihai Oaida <mihai.oaida@gmail.com> 10
13. Scaling Web Applications
Static content - Server
Use dedicated server(s) just for static
Use asynchronous web servers
Lighthttpd, nginx
Or,
Just use a CDN ( Content delivery network )
Mihai Oaida <mihai.oaida@gmail.com> 11
14. Scaling Web Applications
Dynamic content
Usually web servers are not the bottleneck
Code optimization
Use less RAM and less CPU
good performance!= good scalability
For PHP
Opcode cache : xCache, apc, eAccelarator
Mihai Oaida <mihai.oaida@gmail.com> 12
15. Scaling Web Applications
Dynamic content
Dynamic = 0.x% static + 0.y% dynamic ?
Can real-time data have a 5 minute delay ?
Batch writes
Pre-cache
Mihai Oaida <mihai.oaida@gmail.com> 13
16. Scaling Web Applications
Database - general
Choose an open-source one that has proven
to scale: MySQL :)
Use Master – Slave architecture
Write on master(s)
Read from slaves
Mihai Oaida <mihai.oaida@gmail.com> 14
17. Scaling Web Applications
Database
Don't run on the default configuration
key_buffer, query cache, sort buffer, etc
All need tuning as data changes
Mihai Oaida <mihai.oaida@gmail.com> 15
18. Scaling Web Applications
Database
Use indexes – don't index everything
Create very selective indexes, high
cardinality
Join tables very carefully
Denormalize
Mihai Oaida <mihai.oaida@gmail.com> 16
19. Scaling Web Applications
Database
Optimize queries
Use DESCRIBE command
Activate slow query log and monitor it!
Mihai Oaida <mihai.oaida@gmail.com> 17
20. Scaling Web Applications
Cache - Memcache
Asynchronous server
No authentication
Key value database
In memory
Very scalable
Has api in many languages
Small limitations
Mihai Oaida <mihai.oaida@gmail.com> 18
21. Scaling Web Applications
Conclusions
There is no secret recipe, no easy solution
It is very application specific
To scale you need not only the right tools
but the right thinking!
Mihai Oaida <mihai.oaida@gmail.com> 19