towards the cloud: event-driven architectures in php

Post on 11-May-2015

10.707 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Are you fed up with all the interdependencies in your applications? Want to be able to scale your business application across multiple servers without hassle? Event-driven architectures to the rescue! In this talk I show how to divide your tasks into small, perfectly scalable parts, empowering you to either scale with your own servers or benefit from cloud computing technologies.

TRANSCRIPT

Event-Driven Architectures

Benjamin Eberlei

direkt effekt GmbH

IPC 09, Karlsruhe

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 1 / 42

About Me

I Benjamin Eberlei

I direkt effekt GmBH

(digital marketing)

I Open Source contributor

(Zend Framework and Doctrine 2)

I Twitter @beberlei

I Blog: www.whitewashing.de

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 2 / 42

First: A typical monolithic PHPapplication

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 3 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

I Fowler, PoEAA: “Transaction Scripts”

I Retrieve Required Data from Database

I Process Data

I Make changes persistent

I Show the user some output

I Repeat.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42

A Simple Application: PhotoCommunity

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 5 / 42

Photo

class Photo{

public $id;public $name;public $tags = array ();public $user = null;public $metadata = array ();public $originalFile = "";public $thumbnailFile = "";public $created = 0;

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 6 / 42

Tags and User

class User{

public $id;public $username;public $friends = array ();public $photos = array ();

}

class Tag{

public $id;public $name;public $photos = array ();

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 7 / 42

Photo Service

interface PhotoService{

function uploadRequestIsValid($post , $files);

function findOrCreateTags(array $tagNames);

function createPhoto($name , array $tags , User $user);

function saveToDb(Photo $photo);

function extractExifMetadata(Photo $photo);

function performResizes(Photo $photo);

function moveUploadedPhotoFile(Photo $photo , $file);

function notifyFriends(Photo $photo);}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 8 / 42

Upload new Photo

$user = getSessionUser ();$service = new PhotoServiceImpl ();

if($service ->uploadRequestIsValid($_POST , $_FILES)) {// assign $tagNames , $name , $tempFile from Request

$tags = $service ->findOrCreateTags($tagNames);$photo = $service ->createPhoto($name , $tags , $user);

$service ->saveToDb($photo);

$service ->moveUploadedPhotoFile($photo , $tempFile);$service ->extractExifMetadata($photo);$service ->performResizes($photo);$service ->notifyFriends($photo);

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 9 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

Possible Problems

I Thumbnail generation

I View Caching

I CDN Distribution

I Payments/Credits

I Statistical Analysis

I Different Configurable Platforms

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42

You are always required to change thecore application!

1. Lots of Coupling.

2. Potential for great complexity.

3. Hard to maintain and test.

4. Not easily scaleable.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42

You are always required to change thecore application!

1. Lots of Coupling.

2. Potential for great complexity.

3. Hard to maintain and test.

4. Not easily scaleable.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42

You are always required to change thecore application!

1. Lots of Coupling.

2. Potential for great complexity.

3. Hard to maintain and test.

4. Not easily scaleable.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42

You are always required to change thecore application!

1. Lots of Coupling.

2. Potential for great complexity.

3. Hard to maintain and test.

4. Not easily scaleable.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42

You are always required to change thecore application!

1. Lots of Coupling.

2. Potential for great complexity.

3. Hard to maintain and test.

4. Not easily scaleable.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Cronjobs to the Rescue?

I Put work in a queue

I Let a cronjob pick it up

I Or find work with a query

But:

I Not necessarily scaleable or decoupling.

I Single-Threaded

I Delayed by up to a minute

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42

Event-Driven Architectures

A software architecture where looslycoupled components communicate with

each other by triggering events.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 13 / 42

Event-Driven Architectures

1. Decouple responsibilities.

2. Communicate by triggering events.

3. Scale out components.

4. Higher responsiveness (Request MS

decrease).

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42

Event-Driven Architectures

1. Decouple responsibilities.

2. Communicate by triggering events.

3. Scale out components.

4. Higher responsiveness (Request MS

decrease).

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42

Event-Driven Architectures

1. Decouple responsibilities.

2. Communicate by triggering events.

3. Scale out components.

4. Higher responsiveness (Request MS

decrease).

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42

Event-Driven Architectures

1. Decouple responsibilities.

2. Communicate by triggering events.

3. Scale out components.

4. Higher responsiveness (Request MS

decrease).

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42

What is an Event?

“An event is a significantchange in state, which can betriggered from inside or outsidethe application.”

Wikipedia

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 15 / 42

What is an Event?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 16 / 42

Uploading an Image

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 17 / 42

Patterns for Event-Driven Architectures

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 18 / 42

Subject-Observer Pattern #1

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 19 / 42

Subject-Observer Pattern #2

class PhotoUploader implements SplObserver{

function update(SplSubject $photo) {$this ->moveUploadedFile($photo);

}//...

}

class PhotoThumbGenerator implements SplObserver{

function update(SplSubject $photo) {$this ->performResizes($photo);

}//...

}

class PhotoMetadataExtractor implements SplObserver { }

class NewPhotoFriendsNotifier implements SplObserver { }

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 20 / 42

Subject-Observer Pattern #3

class Photo implements SplSubject{

private $observers = null;

function __construct () {$this ->observers = new SplObjectStorage ();$this ->attach(new PhotoUploader ());$this ->attach(new PhotoThumbGenerator ());$this ->attach(new PhotoMetadataExtractor ());$this ->attach(new NewPhotoFriendsNotifier ());

}

function notify () {foreach($this ->observers AS $observer) {

$observer ->update($this);}

}}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 21 / 42

Subject-Observer Pattern #4

// $service instanceof PhotoServiceif($service ->uploadRequestIsValid($_POST , $_FILES)) {

// assign $tagNames , $name , $tempFile from Request

$tags = $service ->findOrCreateTags($tagNames);$photo = $service ->createPhoto($name , $tags , $user);

$service ->moveUploadedPhotoFile($photo , $tempFile);$service ->extractExifMetadata($photo);$service ->performResizes($photo);$service ->notifyFriends($photo);

$service ->saveToDb($photo);}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 22 / 42

Subject-Observer Pattern #5

// $service instanceof PhotoServiceif($service ->uploadRequestIsValid($_POST , $_FILES)) {

// assign $tagNames , $name , $tempFile from Request

$tags = $service ->findOrCreateTags($tagNames);$photo = $service ->createPhoto($name , $tags , $user);

$photo ->notify ();

$service ->saveToDb($photo);}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 23 / 42

Still too much coupling!

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 24 / 42

Event-Dispatcher Pattern #1

I Decouple Subject-Observer

I Introduce Event-Dispatcher Object

I Acts as Mediator between Subjects and

Observers

I Introduces Notion of a Channel

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42

Event-Dispatcher Pattern #1

I Decouple Subject-Observer

I Introduce Event-Dispatcher Object

I Acts as Mediator between Subjects and

Observers

I Introduces Notion of a Channel

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42

Event-Dispatcher Pattern #1

I Decouple Subject-Observer

I Introduce Event-Dispatcher Object

I Acts as Mediator between Subjects and

Observers

I Introduces Notion of a Channel

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42

Event-Dispatcher Pattern #1

I Decouple Subject-Observer

I Introduce Event-Dispatcher Object

I Acts as Mediator between Subjects and

Observers

I Introduces Notion of a Channel

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42

Event-Dispatcher Pattern #2

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 26 / 42

Event-Dispatcher Pattern #3

$dispatcher = new EventDispatcher ();$dispatcher ->attach("pre_save_photo", array(

new PhotoVirusScanner (),new PhotoIsValidImage (),

));$dispatcher ->attach("post_save_photo", array(

new PhotoThumbGenerator (),new PhotoCdnUploader (),new PhotoMetadataExtractor (),new NewPhotoFriendsNotifier ()

));

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 27 / 42

Event-Dispatcher Pattern #4

$user = getSessionUser ();$service = new PhotoServiceImpl ();// $service instanceof PhotoServiceif($service ->uploadRequestIsValid($_POST , $_FILES)) {

// assign $tagNames , $name , $tempFile from Request$tags = $service ->findOrCreateTags($tagNames);$photo = $service ->createPhoto($name , $tags , $user);

$dispatcher ->notify("pre_save_photo", $photo);

$service ->saveToDb($photo);

$dispatcher ->notify("post_save_photo", $photo);

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 28 / 42

Decoupled, Easily Maintainable andRe-Usable Components.

1. Photo, User, Tag Management

2. Photo Validator/Processor

3. Notification Mailer

4. Uploaded File Virus Scanning

5. File CDN Management

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 29 / 42

Attention!

Avoid using a shared Database

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 30 / 42

Open Questions

And how is this gonnasave me Request Time?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 31 / 42

Open Questions

I know all this,what has this to do with the Cloud?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 32 / 42

Asynchronous Processing

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 33 / 42

Message Systems for PHP

I XMPP

I dropr - https://www.dropr.org/

I Gearman - http://gearman.org/

I + lots of Java based ones

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 34 / 42

Gearman #1

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 35 / 42

Gearman #2

Installing on Linux is fairly easy already:root@benny -pc:~$ wget http:// launchpad.net/gearmand/trunk

/0.10/+ download/gearmand -0.10. tar.gzroot@benny -pc:~$ tar xzvf gearmand -0.10. tar.gzroot@benny -pc:~$ cd gearmand -0.10. tar.gzroot@benny -pc:~/ gearmand -0.10$ ./ configureroot@benny -pc:~/ gearmand -0.10$ makeroot@benny -pc:~/ gearmand -0.10$ make installroot@benny -pc:~/ gearmand -0.10$ ldconfigroot@benny -pc:~/ gearmand -0.10$ pecl install gearman -beta

Use packages daemonstools and

start-stop-daemon to manage workers and server

respectively.

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 36 / 42

PHP Gearman-Dispatcher

class GearmanEventDispatcher{

private $client;private $list = array();

function __construct () {$this ->client = new GearmanClient ();$this ->client ->addServer("127.0.0.1");

}

function notify($channel , $context) { }}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 37 / 42

PHP Gearman-Dispatcher

class GearmanEventDispatcher{

function notify($channel , $context) {$contextData = serialize($context);foreach($this ->list[$channel] AS $listener) {

$listenerTaskName = $listener ->getTaskName ();if($listener ->isAsync ()) {

$this ->client ->doBackground($listenerTaskName , $contextData);

} else {$this ->client ->do(

$listenerTaskName , $contextData);}

}}

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 38 / 42

Gearman Worker

$uploader = new LoadUnserializer(new PhotoUploader ());$thumbGenerator = new LoadUnserializer(

new PhotoThumbGenerator ());

$worker= new GearmanWorker ();$worker ->addServer("127.0.0.1");$worker ->addFunction("upload_photo",

array($uploader , ’update ’));$worker ->addFunction("generate_thumb",

array($thumbGenerator , ’update ’));

while($worker ->work()) {// handle errors and return values here..

}

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 39 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

No need to stop here!

I We only looked at the model.

I Controller/View can also be an

Event-Dispatcher.

I Call several independant model

I Pre-calculate Ajax calls in main controller.

I Run your Test-Suite on several machines, in

parallel.

I Any more ideas?

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42

Further Readings

I “Enterprise Integration Patterns”, Hohpe, Woolf

I http://gearman.org/index.php?id=presentations

I http://www.php.net/gearman

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 41 / 42

Thank you!

Please rate this talk at joind.in:

http://joind.in/talk/view/1057

Twitter: @beberlei

Blog: www.whitewashing.de

Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 42 / 42

top related