6tips web-perf

33
1 6 tips to build 6 tips to build Fast Web Fast Web Applications Applications - Luciano Mammino (@loige) PHP Dublin 22/03/2016

Upload: luciano-mammino

Post on 13-Apr-2017

299 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: 6tips web-perf

1

6 tips to build6 tips to buildFast WebFast Web

ApplicationsApplications-

Luciano Mammino (@loige)

PHP Dublin 22/03/2016

Page 2: 6tips web-perf

2

About me

Integrations Engineer at Smartbox

Php developer since *.php3

I Tweet as @loige

I Blog on loige.co

Page 3: 6tips web-perf

3

PreamblePreambleOnly 6 tips (not exhaustive)

Focused on backend

Many generic advices (not just Php)

... Tools and examples (mostly) for Php :)

Page 4: 6tips web-perf

4

Tip 1.Tip 1.Avoid prematureAvoid premature

optimisation!optimisation!

Page 5: 6tips web-perf

5

"Premature"Prematureoptimisation is theoptimisation is the

root of all evil"root of all evil"— Donald Knuth

(not an excuse to write 💩 code!)

Page 6: 6tips web-perf

6

Do you really have a problem?

How big the problem is?

Measure before questioning

Set (realistic) goals

Improve only where needed!

The right mindsetThe right mindset

Page 7: 6tips web-perf

7

The performancelifecycle

Page 8: 6tips web-perf

8

BenchmarkingBenchmarking

Command line: (ab)

Graphical:

(for APIs)

Apache BenchSiege

Apache JMeterSoap UI

Page 9: 6tips web-perf

9

Page 10: 6tips web-perf

10

ProfilingProfilingActive or Passive?

(Active)

Gives a lot of dataSlows down the executionGood in developmentEasy to integrate with IDEsAlso a debugger

Xdebug (Passive)

Minimal impactLess dataGood in productionDedicated Web GUI ( )Supported by Facebook...

xhprof

xhgui

Page 11: 6tips web-perf

11

LibrariesLibrariesSymfony componentstopwatch

<?php

use Symfony\Component\Stopwatch\Stopwatch;

$stopwatch = new Stopwatch();

// Start event named 'eventName'

$stopwatch->start('eventName');

// ... some code goes here

$event = $stopwatch->stop('eventName');

echo $event->getDuration(); // xxx milliseconds

Page 12: 6tips web-perf

12

Cloud toolsCloud tools & Blackfire.io Tideways

Page 13: 6tips web-perf

13

Tip 2.Tip 2.Do just what you need toDo just what you need to

Page 14: 6tips web-perf

14

Page 15: 6tips web-perf

15

A.k.a.A.k.a.

"Don't do things that you"Don't do things that youdon't need to"don't need to"

E.g.

Load classes that you are not usingOpen a connection to a database for everyrequestParse a huge XML config file for everyrequest

Page 16: 6tips web-perf

16

AutoloadingAutoloadingSyntetic Namespaces, Classmaps, PSR-0, PSR-4

You get it for free with !Composer

<?php

require __DIR__ . '/vendor/autoload.php'; // generated by Composer

use Mario\Characters\Mario;use Mario\Powerups\Mushroom;

// Mario & Mushroom classes not loaded yet...

$mario = new Mario();

// Mario class is loaded!

$mushroom = new Mushroom();

// Mushroom class is loaded!

$mario->eat($mushroom); // Tasty!

Page 17: 6tips web-perf

17

Other patternsOther patterns

(& Dependency Injection Container)

Simplifies the loading of classes and the resolution ofdependencies

& & Access resources only when you are about to use them

Dependency InjectionDependency Injection

Lazy loadingLazy loading ProxyProxy

Page 18: 6tips web-perf

18

Tip 3.Tip 3.If you really need to do it,If you really need to do it,

do it do it tomorrowtomorrow!!

Page 19: 6tips web-perf

19

Page 20: 6tips web-perf

20

Defer tasks when possibleDefer tasks when possible

E.g.

Send an email to a user to confirm an orderResize an image after the uploadAggregate metrics

(Serve the response as soon as it's ready!)

Page 21: 6tips web-perf

21

Queues to the rescueQueues to the rescue

A PHP library (Redis as data store).

Laravel/Lumen built-in solution (multiple data storages).

Generic job server that supports many languages.

Work queue originally written to speed up Facebook

Resque

Laravel Queues

Gearman

Beanstalkd

Page 22: 6tips web-perf

22

Tip 4.Tip 4.

Page 23: 6tips web-perf

23

Use cache to avoid repetitiveUse cache to avoid repetitivecomputations and roundtripscomputations and roundtrips

Different Caching layers:

Byte code Cache (APC, OpCache)

Application Cache (Redis, Memcache, )

HTTP Cache (E-tag, Cache-control headers)

Proxy Cache (Varnish, Squid, Nginx)

Gibson

Page 24: 6tips web-perf

24

"There are only two hard"There are only two hardthings in Computerthings in Computer

Science: cacheScience: cacheinvalidation and naminginvalidation and naming

things"things"— Phil Karlton— Phil Karlton

Page 25: 6tips web-perf

25

Tip 5.Tip 5.Beware of the N+1 queryBeware of the N+1 query

problem!problem!

Page 26: 6tips web-perf

26

<?php

function getUsers() { //... retrieve the users from the database (1 query) return $users;}

function loadLastLoginsForUsers($users) { foreach ($users as $user) { $lastLogins = ... // load the last logins // for the user (1 query, executed n times) $user->setLastLogins($lastLogins); }

return $users;}

$users = getUsers();loadLastLoginsForUsers($users);

Page 27: 6tips web-perf

27

SELECT id FROM Users; -- ids: 1, 2, 3, 4, 5, 6... SELECT * FROM Logins WHERE user_id = 1; SELECT * FROM Logins WHERE user_id = 2; SELECT * FROM Logins WHERE user_id = 3; SELECT * FROM Logins WHERE user_id = 4; SELECT * FROM Logins WHERE user_id = 5; SELECT * FROM Logins WHERE user_id = 6; -- ...

N+1 Queries! 😓😓

Page 28: 6tips web-perf

28

Better solutionBetter solution

or use JOINS...or use JOINS...

SELECT id FROM Users; -- ids: 1, 2, 3, 4, 5, 6... SELECT * FROM Logins WHERE user_id IN (1, 2, 3, 4, 5, 6, ...);

Don't trust the ORMCheck the queries generated by your code!

Page 29: 6tips web-perf

29

Tip 6.Tip 6.Plan for horizontalPlan for horizontal

scalabilityscalability

Page 30: 6tips web-perf

30

Build your app to beBuild your app to bereplicated across manyreplicated across many

serversserversSessions:

Don't use the default file based engineBe as much stateless as possible

User files:

CDN, Cloud Storage (S3, Cloudfiles), Sync (NFS, Gluster)

Consider Microservices...

Page 31: 6tips web-perf

31

BONUS Tip.BONUS Tip.Update to PHP7Update to PHP7

(Rasmus approves)

php.net/migration70

Page 32: 6tips web-perf

32

Let's RecapLet's Recap1. Avoid premature optimisation!

2. Do just what you need to

3. If you really need to do it, do it tomorrow!

4. Cache me if you can

5. Beware of the N+1 query problem!

6. Plan for horizontal scalability

7. Bonus: Update to Php 7

8. : Super Secret Bonus bit.ly/php-perf

Page 33: 6tips web-perf

33

Thank you!Thank you!

Let's keep in touch: - http://loige.co @loige