Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Fast APIs and Middlewares: Mezzio + Swoole

Babarinde Odewumi
November 18, 2023
28

Building Fast APIs and Middlewares: Mezzio + Swoole

We'll be discussing Mezzio: A PSR-15 Middleware framework, allowing you to build elegant middlewares for your applications and also Swoole: a PHP extension that allows you to build high performance applications. We'll also look at how to combine both to get the best of both worlds and highlight how this differs from the traditional Apache/Nginx PHP approaches and pitfalls to be aware of when taking the dive.

Babarinde Odewumi

November 18, 2023
Tweet

Transcript

  1. Who am I I’m Babarinde Odewumi I’m from Nigeria I’m

    Founder and Senior Software Architect @ abbaandking.com I’m a Husband of One and Father of Two I’m a Believer
  2. My Work Lead a fantastic team of Designers, Frontend &

    Backend Devs Build Systems that make peoples lives a bit easier Design End to End Software for Digital Insurance Companies in Nigeria Some open source contributions to Dokku Kubernetes Scheduler
  3. Why do APIs need to be fast Everyone likes fast

    things Modern Frontend Frameworks Impatient Customers
  4. What is Mezzio Mezzio allows you to write PSR-15 middleware

    applications Create middleware applications, using as many layers as you want, and the architecture your project needs. Used to be Zend Expressive https://docs.mezzio.dev/mezzio/
  5. Mezzio Features PSR-15 Middlewares Consume PSR-7 Messages Route requests to

    Middleware (Fastroute, Aura.Router, Laminas-router) Dependency Injection PSR-11 compatible (Laminas- ServiceManager, Pimple, Aura.DI) Templating (Twig, Plates, Laminas-View) Error Handling (Whoops, etc.)
  6. Middlewares • Middleware is code that exists between the request

    and response, and which can take the incoming request, perform actions based on it, and either complete the response or pass delegation on to the next middleware in the queue.
  7. Middlewares • With this paradigm, you can build a workflow

    engine for handling requests — for instance, you could have middleware perform the following • Handle Authentication • Manage Access Control • Logging, etc.
  8. The Value of PRS-15 Middlewares • Building blocks • Lightweight

    • Organization • Reusability • Interoperability
  9. What is Swoole • Swoole is an event-driven, asynchronous, coroutine-based

    concurrency library with high performance for PHP. • HTTP Server • Web Sockets • TCP Server • UDP Server • Coroutines • Swoole or Openswoole
  10. Traditional PHP For every HTTP Request • Request hits Separate

    Web Server • PHP process is triggered • Application is bootstrapped and loaded • Request is processed • Response is returned • Dispose everything and start again for new request
  11. PHP + Swoole On First request • Request hits set

    up webserver • Application is bootstrapped and loaded • Dispatch to web worker • Request is processed • Response is returned • Keep Application state in memory for subsequent HTTP requests
  12. Swoole In Details Swoole runs a main process which bootstraps

    the PHP application once, and then keeps it in memory so that it can continue dispatching requests without having to load resources every time. This makes it really fast. HTTP requests are handled by a set of child processes called “web workers”. Swoole forks the contents in memory for the main process to each one of the workers, so each worker has a “copy” of the application in memory. Each worker is independent, but each one of them can only handle one request at a time, so you have to increase the number of workers if you expect a lot of concurrent traffic.
  13. Potential Issues Long Running processes exhausting web workers Avoid using

    sessions Ensure stateless services: impact on DIC for concurrent or subsequent requests