SlideShare a Scribd company logo
1 of 72
Download to read offline
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

          Benjamin Eberlei
          direkt effekt GmBH
          (digital marketing)
          Open Source contributor
          (Zend Framework and Doctrine 2)
          Twitter @beberlei
          Blog: www.whitewashing.de
Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   2 / 42
First: A typical monolithic PHP
                      application




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   3 / 42
Fowler, PoEAA: “Transaction Scripts”




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
Fowler, PoEAA: “Transaction Scripts”
          Retrieve Required Data from Database




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
Fowler, PoEAA: “Transaction Scripts”
          Retrieve Required Data from Database
          Process Data




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
Fowler, PoEAA: “Transaction Scripts”
          Retrieve Required Data from Database
          Process Data
          Make changes persistent




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
Fowler, PoEAA: “Transaction Scripts”
          Retrieve Required Data from Database
          Process Data
          Make changes persistent
          Show the user some output




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
Fowler, PoEAA: “Transaction Scripts”
          Retrieve Required Data from Database
          Process Data
          Make changes persistent
          Show the user some output
          Repeat.



Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   4 / 42
A Simple Application: Photo
Community




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 u p l o a d RequestIsValid ( $post , $files ) ;

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

        function saveToDb ( Photo $photo ) ;

        function e x t ra c tExifMetadata ( Photo $photo ) ;

        function performResizes ( Photo $photo ) ;
        function m o v e U p lo aded Photo File ( 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 - > u p l o a d RequestIsValid ( $_POST , $_FILES ) ) {
         // assign $tagNames , $name , $tempFile from Request

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

        $service - > saveToDb ( $photo ) ;
        $service - > m o v e U p loade dPho toFil e ( $photo , $tempFile ) ;
        $service - > ex t r actExifMetadata ( $photo ) ;
        $service - > performResizes ( $photo ) ;
        $service - > notifyFriends ( $photo ) ;
   }




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

          Thumbnail generation




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

          Thumbnail generation
          View Caching




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

          Thumbnail generation
          View Caching
          CDN Distribution




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

          Thumbnail generation
          View Caching
          CDN Distribution
          Payments/Credits




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

          Thumbnail generation
          View Caching
          CDN Distribution
          Payments/Credits
          Statistical Analysis



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

          Thumbnail generation
          View Caching
          CDN Distribution
          Payments/Credits
          Statistical Analysis
          Different Configurable Platforms


Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   10 / 42
You are always required to change the
               core application!




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   11 / 42
You are always required to change the
               core application!

    1. Lots of Coupling.




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   11 / 42
You are always required to change the
               core application!

    1. Lots of Coupling.
    2. Potential for great complexity.




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   11 / 42
You are always required to change the
               core application!

    1. Lots of Coupling.
    2. Potential for great complexity.
    3. Hard to maintain and test.



Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   11 / 42
You are always required to change the
               core 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?
          Put work in a queue




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   12 / 42
Cronjobs to the Rescue?
          Put work in a queue
          Let a cronjob pick it up




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   12 / 42
Cronjobs to the Rescue?
          Put work in a queue
          Let a cronjob pick it up
          Or find work with a query




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   12 / 42
Cronjobs to the Rescue?
          Put work in a queue
          Let a cronjob pick it up
          Or find work with a query
  But:
          Not necessarily scaleable or decoupling.



Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   12 / 42
Cronjobs to the Rescue?
          Put work in a queue
          Let a cronjob pick it up
          Or find work with a query
  But:
          Not necessarily scaleable or decoupling.
          Single-Threaded


Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   12 / 42
Cronjobs to the Rescue?
          Put work in a queue
          Let a cronjob pick it up
          Or find work with a query
  But:
          Not necessarily scaleable or decoupling.
          Single-Threaded
          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 loosly
   coupled 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.




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

    1. Decouple responsibilities.
    2. Communicate by triggering events.




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.




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 significant
          change in state, which can be
          triggered from inside or outside
          the 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 Pho to T hu m b Ge n erator implements SplObserver
   {
       function update ( SplSubject $photo ) {
            $this - > performResizes ( $photo ) ;
       }
       // ...
   }

   class Ph o t o M e t a d a t a Ext ra ct or implements SplObserver { }
   class N ew P h o t o F r i e n d sN o ti f ie r 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 P ho to Met ad at aEx tr ac tor () ) ;
            $this - > attach ( new Ne wP h ot o Fr ie n ds No t if i er () ) ;
        }

        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 PhotoService
   if ( $service - > u p l o a d RequestIsValid ( $_POST , $_FILES ) ) {
         // assign $tagNames , $name , $tempFile from Request

        $tags = $service - > findOrCreateTags ( $tagNames ) ;
        $photo = $service - > createPhoto ( $name , $tags , $user ) ;
        $service - > m o v e U p loade dPho toFil e ( $photo , $tempFile ) ;
        $service - > ex t r actExifMetadata ( $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 PhotoService
   if ( $service - > u p l o a d RequestIsValid ( $_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

          Decouple Subject-Observer




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

          Decouple Subject-Observer
          Introduce Event-Dispatcher Object




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

          Decouple Subject-Observer
          Introduce Event-Dispatcher Object
          Acts as Mediator between Subjects and
          Observers




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

          Decouple Subject-Observer
          Introduce Event-Dispatcher Object
          Acts as Mediator between Subjects and
          Observers
          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 P hotoVi rusScanner () ,
       new P hotoIs ValidImage () ,
   ));
   $dispatcher - > attach ( " post_save_photo " , array (
       new P ho t o Th u m bG enerator () ,
       new PhotoCdnUploader () ,
       new P h o t o M e t a d a t aE xt rac to r () ,
       new N e w P h o t o F r i en d sN ot i fi e r ()
   ));




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

   $user = getSessionUser () ;
   $service = new PhotoServiceImpl () ;
   // $service instanceof PhotoService
   if ( $service - > u p l o a d RequestIsValid ( $_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 and
            Re-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 gonna
                  save 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


          XMPP
          dropr - https://www.dropr.org/
          Gearman - http://gearman.org/
          + 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 . gz
   root@benny - pc :~ $ tar xzvf gearmand -0.10. tar . gz
   root@benny - pc :~ $ cd gearmand -0.10. tar . gz
   root@benny - pc :~/ gearmand -0.10 $ ./ configure
   root@benny - pc :~/ gearmand -0.10 $ make
   root@benny - pc :~/ gearmand -0.10 $ make install
   root@benny - pc :~/ gearmand -0.10 $ ldconfig
   root@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 Ge a r m a n E v e n t D isp at ch er
   {
       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 Ge a r m a n E v e n t D isp at ch er
   {
         function notify ( $channel , $context ) {
             $contextData = serialize ( $context ) ;
             foreach ( $this - > list [ $channel ] AS $listener ) {
                 $li stenerTaskName = $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 P ho t o Th u m bG enerator ()
   );

   $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!
          We only looked at the model.




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
No need to stop here!
          We only looked at the model.
          Controller/View can also be an
          Event-Dispatcher.




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
No need to stop here!
          We only looked at the model.
          Controller/View can also be an
          Event-Dispatcher.
          Call several independant model




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
No need to stop here!
          We only looked at the model.
          Controller/View can also be an
          Event-Dispatcher.
          Call several independant model
          Pre-calculate Ajax calls in main controller.




Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
No need to stop here!
          We only looked at the model.
          Controller/View can also be an
          Event-Dispatcher.
          Call several independant model
          Pre-calculate Ajax calls in main controller.
          Run your Test-Suite on several machines, in
          parallel.


Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
No need to stop here!
          We only looked at the model.
          Controller/View can also be an
          Event-Dispatcher.
          Call several independant model
          Pre-calculate Ajax calls in main controller.
          Run your Test-Suite on several machines, in
          parallel.
          Any more ideas?
Eberlei (direkt effekt GmbH)   Event-Driven Architectures   IPC 09, Karlsruhe   40 / 42
Further Readings


          “Enterprise Integration Patterns”, Hohpe, Woolf
          http://gearman.org/index.php?id=presentations
          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

More Related Content

What's hot

Python harness fundamental
Python harness fundamentalPython harness fundamental
Python harness fundamental
Robin0590
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
Jeffrey Kemp
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
Alberto Souza
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
Ray Ploski
 

What's hot (20)

Python harness fundamental
Python harness fundamentalPython harness fundamental
Python harness fundamental
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
 
Bowtie: Interactive Dashboards
Bowtie: Interactive DashboardsBowtie: Interactive Dashboards
Bowtie: Interactive Dashboards
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
 
Testing the frontend
Testing the frontendTesting the frontend
Testing the frontend
 
Building Maintainable Applications in Apex
Building Maintainable Applications in ApexBuilding Maintainable Applications in Apex
Building Maintainable Applications in Apex
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
 
Post Sharp Talk
Post Sharp TalkPost Sharp Talk
Post Sharp Talk
 
ISSTA 2010 Presentation
ISSTA 2010 PresentationISSTA 2010 Presentation
ISSTA 2010 Presentation
 
Spring AOP in Nutshell
Spring AOP in Nutshell Spring AOP in Nutshell
Spring AOP in Nutshell
 
Android TDD
Android TDDAndroid TDD
Android TDD
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
Refactoring
RefactoringRefactoring
Refactoring
 
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
 
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
Kotlin After 9 Months
Kotlin After 9 MonthsKotlin After 9 Months
Kotlin After 9 Months
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
 
Dependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutesDependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutes
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 

Viewers also liked

Viewers also liked (11)

Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven Architecture
 
Die Kunst des Software Design - Java
Die Kunst des Software Design - JavaDie Kunst des Software Design - Java
Die Kunst des Software Design - Java
 
Die Kunst Des Software Design
Die Kunst Des Software DesignDie Kunst Des Software Design
Die Kunst Des Software Design
 
Dropping ACID - Building Scalable Systems That Work
Dropping ACID - Building Scalable Systems That WorkDropping ACID - Building Scalable Systems That Work
Dropping ACID - Building Scalable Systems That Work
 
Event Driven Architecture at NDDNUG
Event Driven Architecture at NDDNUGEvent Driven Architecture at NDDNUG
Event Driven Architecture at NDDNUG
 
Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven Architecture
 
Distributing Transactions using MassTransit
Distributing Transactions using MassTransitDistributing Transactions using MassTransit
Distributing Transactions using MassTransit
 
Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven Architecture
 
Event Driven Architecture with Apache Camel
Event Driven Architecture with Apache CamelEvent Driven Architecture with Apache Camel
Event Driven Architecture with Apache Camel
 
Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven Architecture
 
Event-Driven Architecture (EDA)
Event-Driven Architecture (EDA)Event-Driven Architecture (EDA)
Event-Driven Architecture (EDA)
 

Similar to Towards the Cloud: Event-driven Architectures in PHP

BlueData Isilon Validation Brief
BlueData Isilon Validation BriefBlueData Isilon Validation Brief
BlueData Isilon Validation Brief
Boni Bruno
 

Similar to Towards the Cloud: Event-driven Architectures in PHP (20)

UML as a Programming Language
UML as a Programming LanguageUML as a Programming Language
UML as a Programming Language
 
BlueData Isilon Validation Brief
BlueData Isilon Validation BriefBlueData Isilon Validation Brief
BlueData Isilon Validation Brief
 
FaaS or not to FaaS. Visible and invisible benefits of the Serverless paradig...
FaaS or not to FaaS. Visible and invisible benefits of the Serverless paradig...FaaS or not to FaaS. Visible and invisible benefits of the Serverless paradig...
FaaS or not to FaaS. Visible and invisible benefits of the Serverless paradig...
 
Test driven Infrastructure development with Ansible and Molecule
Test driven Infrastructure development with Ansible and MoleculeTest driven Infrastructure development with Ansible and Molecule
Test driven Infrastructure development with Ansible and Molecule
 
Blasting Through the Clouds - Automating Cloud Foundry with Concourse CI
Blasting Through the Clouds - Automating Cloud Foundry with Concourse CIBlasting Through the Clouds - Automating Cloud Foundry with Concourse CI
Blasting Through the Clouds - Automating Cloud Foundry with Concourse CI
 
[SiriusCon 2018] Extensive Use of Custom Properties Views in a Banking DSL Wo...
[SiriusCon 2018] Extensive Use of Custom Properties Views in a Banking DSL Wo...[SiriusCon 2018] Extensive Use of Custom Properties Views in a Banking DSL Wo...
[SiriusCon 2018] Extensive Use of Custom Properties Views in a Banking DSL Wo...
 
DevOps - How to get technical buy in
DevOps - How to get technical buy inDevOps - How to get technical buy in
DevOps - How to get technical buy in
 
Jenkins X - automated CI/CD solution for cloud native applications on Kubernetes
Jenkins X - automated CI/CD solution for cloud native applications on KubernetesJenkins X - automated CI/CD solution for cloud native applications on Kubernetes
Jenkins X - automated CI/CD solution for cloud native applications on Kubernetes
 
White Paper: EMC Greenplum Data Computing Appliance Enhances EMC IT's Global ...
White Paper: EMC Greenplum Data Computing Appliance Enhances EMC IT's Global ...White Paper: EMC Greenplum Data Computing Appliance Enhances EMC IT's Global ...
White Paper: EMC Greenplum Data Computing Appliance Enhances EMC IT's Global ...
 
Fast Data: A Customer’s Journey to Delivering a Compelling Real-Time Solution
Fast Data: A Customer’s Journey to Delivering a Compelling Real-Time SolutionFast Data: A Customer’s Journey to Delivering a Compelling Real-Time Solution
Fast Data: A Customer’s Journey to Delivering a Compelling Real-Time Solution
 
Kubernetes and the 12 factor cloud apps
Kubernetes and the 12 factor cloud appsKubernetes and the 12 factor cloud apps
Kubernetes and the 12 factor cloud apps
 
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
 
Oracle Database In-Memory
Oracle Database In-MemoryOracle Database In-Memory
Oracle Database In-Memory
 
Oracle Database In_Memory Christian Antognini
Oracle Database In_Memory Christian AntogniniOracle Database In_Memory Christian Antognini
Oracle Database In_Memory Christian Antognini
 
Turnkey Continuous Delivery
Turnkey Continuous DeliveryTurnkey Continuous Delivery
Turnkey Continuous Delivery
 
Lean Engineering: How to make Engineering a full Lean UX partner
Lean Engineering: How to make Engineering a full Lean UX partnerLean Engineering: How to make Engineering a full Lean UX partner
Lean Engineering: How to make Engineering a full Lean UX partner
 
Enabling Lean at Enterprise Scale: Lean Engineering in Action
Enabling Lean at Enterprise Scale: Lean Engineering in ActionEnabling Lean at Enterprise Scale: Lean Engineering in Action
Enabling Lean at Enterprise Scale: Lean Engineering in Action
 
SRECon 18 Immutable Infrastructure
SRECon 18 Immutable InfrastructureSRECon 18 Immutable Infrastructure
SRECon 18 Immutable Infrastructure
 
#SREcon Immutable Infrastructure: rethinking configuration mgmt
#SREcon Immutable Infrastructure: rethinking configuration mgmt#SREcon Immutable Infrastructure: rethinking configuration mgmt
#SREcon Immutable Infrastructure: rethinking configuration mgmt
 
Continuous Delivery of Agile Architecture
Continuous Delivery of Agile ArchitectureContinuous Delivery of Agile Architecture
Continuous Delivery of Agile Architecture
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 

Towards the Cloud: Event-driven Architectures in PHP

  • 1. Event-Driven Architectures Benjamin Eberlei direkt effekt GmbH IPC 09, Karlsruhe Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 1 / 42
  • 2. About Me Benjamin Eberlei direkt effekt GmBH (digital marketing) Open Source contributor (Zend Framework and Doctrine 2) Twitter @beberlei Blog: www.whitewashing.de Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 2 / 42
  • 3. First: A typical monolithic PHP application Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 3 / 42
  • 4. Fowler, PoEAA: “Transaction Scripts” Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 5. Fowler, PoEAA: “Transaction Scripts” Retrieve Required Data from Database Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 6. Fowler, PoEAA: “Transaction Scripts” Retrieve Required Data from Database Process Data Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 7. Fowler, PoEAA: “Transaction Scripts” Retrieve Required Data from Database Process Data Make changes persistent Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 8. Fowler, PoEAA: “Transaction Scripts” Retrieve Required Data from Database Process Data Make changes persistent Show the user some output Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 9. Fowler, PoEAA: “Transaction Scripts” Retrieve Required Data from Database Process Data Make changes persistent Show the user some output Repeat. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 4 / 42
  • 10. A Simple Application: Photo Community Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 5 / 42
  • 11. 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
  • 12. 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
  • 13. Photo Service interface PhotoService { function u p l o a d RequestIsValid ( $post , $files ) ; function findOrCreateTags ( array $tagNames ) ; function createPhoto ( $name , array $tags , User $user ) ; function saveToDb ( Photo $photo ) ; function e x t ra c tExifMetadata ( Photo $photo ) ; function performResizes ( Photo $photo ) ; function m o v e U p lo aded Photo File ( Photo $photo , $file ) ; function notifyFriends ( Photo $photo ) ; } Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 8 / 42
  • 14. Upload new Photo $user = getSessionUser () ; $service = new PhotoServiceImpl () ; if ( $service - > u p l o a d RequestIsValid ( $_POST , $_FILES ) ) { // assign $tagNames , $name , $tempFile from Request $tags = $service - > findOrCreateTags ( $tagNames ) ; $photo = $service - > createPhoto ( $name , $tags , $user ) ; $service - > saveToDb ( $photo ) ; $service - > m o v e U p loade dPho toFil e ( $photo , $tempFile ) ; $service - > ex t r actExifMetadata ( $photo ) ; $service - > performResizes ( $photo ) ; $service - > notifyFriends ( $photo ) ; } Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 9 / 42
  • 15. Possible Problems Thumbnail generation Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 16. Possible Problems Thumbnail generation View Caching Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 17. Possible Problems Thumbnail generation View Caching CDN Distribution Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 18. Possible Problems Thumbnail generation View Caching CDN Distribution Payments/Credits Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 19. Possible Problems Thumbnail generation View Caching CDN Distribution Payments/Credits Statistical Analysis Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 20. Possible Problems Thumbnail generation View Caching CDN Distribution Payments/Credits Statistical Analysis Different Configurable Platforms Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 10 / 42
  • 21. You are always required to change the core application! Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42
  • 22. You are always required to change the core application! 1. Lots of Coupling. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42
  • 23. You are always required to change the core application! 1. Lots of Coupling. 2. Potential for great complexity. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42
  • 24. You are always required to change the core application! 1. Lots of Coupling. 2. Potential for great complexity. 3. Hard to maintain and test. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 11 / 42
  • 25. You are always required to change the core 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
  • 26. Cronjobs to the Rescue? Put work in a queue Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 27. Cronjobs to the Rescue? Put work in a queue Let a cronjob pick it up Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 28. Cronjobs to the Rescue? Put work in a queue Let a cronjob pick it up Or find work with a query Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 29. Cronjobs to the Rescue? Put work in a queue Let a cronjob pick it up Or find work with a query But: Not necessarily scaleable or decoupling. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 30. Cronjobs to the Rescue? Put work in a queue Let a cronjob pick it up Or find work with a query But: Not necessarily scaleable or decoupling. Single-Threaded Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 31. Cronjobs to the Rescue? Put work in a queue Let a cronjob pick it up Or find work with a query But: Not necessarily scaleable or decoupling. Single-Threaded Delayed by up to a minute Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 12 / 42
  • 32. Event-Driven Architectures A software architecture where loosly coupled components communicate with each other by triggering events. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 13 / 42
  • 33. Event-Driven Architectures 1. Decouple responsibilities. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42
  • 34. Event-Driven Architectures 1. Decouple responsibilities. 2. Communicate by triggering events. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42
  • 35. Event-Driven Architectures 1. Decouple responsibilities. 2. Communicate by triggering events. 3. Scale out components. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 14 / 42
  • 36. 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
  • 37. What is an Event? “An event is a significant change in state, which can be triggered from inside or outside the application.” Wikipedia Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 15 / 42
  • 38. What is an Event? Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 16 / 42
  • 39. Uploading an Image Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 17 / 42
  • 40. Patterns for Event-Driven Architectures Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 18 / 42
  • 41. Subject-Observer Pattern #1 Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 19 / 42
  • 42. Subject-Observer Pattern #2 class PhotoUploader implements SplObserver { function update ( SplSubject $photo ) { $this - > moveUploadedFile ( $photo ) ; } // ... } class Pho to T hu m b Ge n erator implements SplObserver { function update ( SplSubject $photo ) { $this - > performResizes ( $photo ) ; } // ... } class Ph o t o M e t a d a t a Ext ra ct or implements SplObserver { } class N ew P h o t o F r i e n d sN o ti f ie r implements SplObserver { } Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 20 / 42
  • 43. 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 P ho to Met ad at aEx tr ac tor () ) ; $this - > attach ( new Ne wP h ot o Fr ie n ds No t if i er () ) ; } function notify () { foreach ( $this - > observers AS $observer ) { $observer - > update ( $this ) ; } } } Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 21 / 42
  • 44. Subject-Observer Pattern #4 // $service instanceof PhotoService if ( $service - > u p l o a d RequestIsValid ( $_POST , $_FILES ) ) { // assign $tagNames , $name , $tempFile from Request $tags = $service - > findOrCreateTags ( $tagNames ) ; $photo = $service - > createPhoto ( $name , $tags , $user ) ; $service - > m o v e U p loade dPho toFil e ( $photo , $tempFile ) ; $service - > ex t r actExifMetadata ( $photo ) ; $service - > performResizes ( $photo ) ; $service - > notifyFriends ( $photo ) ; $service - > saveToDb ( $photo ) ; } Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 22 / 42
  • 45. Subject-Observer Pattern #5 // $service instanceof PhotoService if ( $service - > u p l o a d RequestIsValid ( $_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
  • 46. Still too much coupling! Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 24 / 42
  • 47. Event-Dispatcher Pattern #1 Decouple Subject-Observer Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42
  • 48. Event-Dispatcher Pattern #1 Decouple Subject-Observer Introduce Event-Dispatcher Object Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42
  • 49. Event-Dispatcher Pattern #1 Decouple Subject-Observer Introduce Event-Dispatcher Object Acts as Mediator between Subjects and Observers Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42
  • 50. Event-Dispatcher Pattern #1 Decouple Subject-Observer Introduce Event-Dispatcher Object Acts as Mediator between Subjects and Observers Introduces Notion of a Channel Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 25 / 42
  • 51. Event-Dispatcher Pattern #2 Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 26 / 42
  • 52. Event-Dispatcher Pattern #3 $dispatcher = new EventDispatcher () ; $dispatcher - > attach ( " pre_save_photo " , array ( new P hotoVi rusScanner () , new P hotoIs ValidImage () , )); $dispatcher - > attach ( " post_save_photo " , array ( new P ho t o Th u m bG enerator () , new PhotoCdnUploader () , new P h o t o M e t a d a t aE xt rac to r () , new N e w P h o t o F r i en d sN ot i fi e r () )); Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 27 / 42
  • 53. Event-Dispatcher Pattern #4 $user = getSessionUser () ; $service = new PhotoServiceImpl () ; // $service instanceof PhotoService if ( $service - > u p l o a d RequestIsValid ( $_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
  • 54. Decoupled, Easily Maintainable and Re-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
  • 55. Attention! Avoid using a shared Database Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 30 / 42
  • 56. Open Questions And how is this gonna save me Request Time? Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 31 / 42
  • 57. 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
  • 58. Asynchronous Processing Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 33 / 42
  • 59. Message Systems for PHP XMPP dropr - https://www.dropr.org/ Gearman - http://gearman.org/ + lots of Java based ones Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 34 / 42
  • 60. Gearman #1 Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 35 / 42
  • 61. 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 . gz root@benny - pc :~ $ tar xzvf gearmand -0.10. tar . gz root@benny - pc :~ $ cd gearmand -0.10. tar . gz root@benny - pc :~/ gearmand -0.10 $ ./ configure root@benny - pc :~/ gearmand -0.10 $ make root@benny - pc :~/ gearmand -0.10 $ make install root@benny - pc :~/ gearmand -0.10 $ ldconfig root@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
  • 62. PHP Gearman-Dispatcher class Ge a r m a n E v e n t D isp at ch er { 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
  • 63. PHP Gearman-Dispatcher class Ge a r m a n E v e n t D isp at ch er { function notify ( $channel , $context ) { $contextData = serialize ( $context ) ; foreach ( $this - > list [ $channel ] AS $listener ) { $li stenerTaskName = $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
  • 64. Gearman Worker $uploader = new LoadUnserializer ( new PhotoUploader () ) ; $thumbGenerator = new LoadUnserializer ( new P ho t o Th u m bG enerator () ); $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
  • 65. No need to stop here! We only looked at the model. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 66. No need to stop here! We only looked at the model. Controller/View can also be an Event-Dispatcher. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 67. No need to stop here! We only looked at the model. Controller/View can also be an Event-Dispatcher. Call several independant model Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 68. No need to stop here! We only looked at the model. Controller/View can also be an Event-Dispatcher. Call several independant model Pre-calculate Ajax calls in main controller. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 69. No need to stop here! We only looked at the model. Controller/View can also be an Event-Dispatcher. Call several independant model Pre-calculate Ajax calls in main controller. Run your Test-Suite on several machines, in parallel. Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 70. No need to stop here! We only looked at the model. Controller/View can also be an Event-Dispatcher. Call several independant model Pre-calculate Ajax calls in main controller. Run your Test-Suite on several machines, in parallel. Any more ideas? Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 40 / 42
  • 71. Further Readings “Enterprise Integration Patterns”, Hohpe, Woolf http://gearman.org/index.php?id=presentations http://www.php.net/gearman Eberlei (direkt effekt GmbH) Event-Driven Architectures IPC 09, Karlsruhe 41 / 42
  • 72. 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