Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swoole 4: Introducing the New Coroutine Design Pattern in PHP

84 views

Published on

Swoole is an event-driven asynchronous and concurrent networking communication engine for writing high-performance HTTP and WebSocket services in PHP. It changes the way how we write and build PHP applications like before. With supports of coroutine (like goroutine in Go), non-blocking IO, connection pool, and many more, we can build better scalable and fast web applications. In this talk, I will dig into the latest changes and technical details in Swoole 4 to demonstrate how it breaks the limit of PHP, using a variety of examples.

  • Be the first to comment

Swoole 4: Introducing the New Coroutine Design Pattern in PHP

  1. 1. Demin Yin 4: Introducing the New Coroutine Design Pattern in PHP 1 https://joind.in/talk/902ff
  2. 2. About Me • Staff Engineer and Technical Lead on Glu Mobile’s Design Home team • Playing with PHP since 2000. • Worked for a diverse group of companies in China and US, including eBay, Visa and Glu • Focus on building high performance, secure web services • Born in China, worked in U.S., live in Canada • On twitter: @deminy • On Github: https://github.com/deminy 2
  3. 3. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 3
  4. 4. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 4
  5. 5. What Is Swoole: How Does Swoole Work? under PHP-FPM under Swoole 5
  6. 6. • Event-driven; asynchronous I/O. • Coroutines, channels, defer, and more. • Connection pool. • Build high performance, concurrent HTTP, WebSocket, TCP, UDP, Unix socket services. What Is Swoole: Enrich PHP with Some Amazing Features 6
  7. 7. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 7
  8. 8. Benchmark Results: Single Query Source: the TechEmpower Framework Benchmarks project best database-access responses per second, single query 8
  9. 9. Benchmark Results: Multiple Queries Source: the TechEmpower Framework Benchmarks project responses per second at 20 queries per request, 256 requests concurrently 9
  10. 10. Benchmark Results: Fortunes Source: the TechEmpower Framework Benchmarks project best fortunes responses per second 10
  11. 11. Benchmark Results: Why Faster? Why is Swoole faster? • Connection pool. • Extra operations when using PDO: • Message sent to MySQL server to deallocate the prepared statement when destroying a PDOStatement object. • Message sent to MySQL server to close the MySQL connection when destroying a PDO object. • …… 11
  12. 12. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 12
  13. 13. Coroutine in Swoole 4: an example under PHP under Swoole 4 13
  14. 14. Coroutine in Swoole 4: an example (cont.) under PHP under Swoole 4 14
  15. 15. output: 123456 15 Coroutine in Swoole 4: an example (cont.)
  16. 16. Processes, Threads, and Coroutines Multiprocessing Multithreading Coroutine Creation fork() pthread_create() go() Scheduling expensive cheap extremely cheap Concurrency hundreds thousands hundreds of thousands Programming difficult very difficult easy 16
  17. 17. Create a Server Socket in PHP under PHP 17
  18. 18. Create a Server Socket in Swoole 4 under Swoole 4 18
  19. 19. Coroutine: What’s the benefits? • A coroutine can be paused when IO happens, and resumed after IO finishes. • PHP code still executed in sequential manner. • No new processes or threads created. Low costs on “scheduling”. • No locks needed when updating shared resources from different coroutines. • High concurrency. • Easier to use, especially when comparing to asynchronous callbacks. 19
  20. 20. Coroutines in For Loop 20
  21. 21. Nested Coroutines 21
  22. 22. Nested Coroutines (cont.) 22
  23. 23. Nested Coroutines (cont.) output: 127983465 output: 123456789 23
  24. 24. Blocking I/O in PHP 24 • PHP extension MySQLi, PDO: SwooleCoroutineMySQL • PHP extension Redis: SwooleCoroutineRedis • sleep(), usleep() : … • sockets and streams: … • file_get_contents(), fread(), fwrite() : … • …… • PHP extension SOAP. • PHP extension MongoDB.
  25. 25. Swoole 4: Channels 25
  26. 26. Swoole 4: Channels (cont.) SplQueue Channels push() SplQueue>push() Always successful until memory exhausted. chan::push() Blocks if running out of allocated space. pop() SplQueue>pop() Throws an exception when the queue is empty. chan::pop() Blocks if no data available. 26
  27. 27. Swoole 4: Channels (cont.) 27
  28. 28. Swoole 4: defer 28
  29. 29. Swoole 4: defer (cont.) Golang (pseudocode) PHP (pseudocode) 29
  30. 30. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 30
  31. 31. Top Swoole Use Cases • Crawlers. • HTTP/RPC. • IM, online chat room. • Internet of things. • Gaming servers. 31
  32. 32. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 32
  33. 33. History of Swoole • 2012-07: First Git commit. • 2013-04: Swoole 1 released. • 2016-12: Swoole 2 released. • 2018-06: Swoole 4 released. PHP 7.1+ only. 33
  34. 34. Swoole 4.1 • 4.1.0: Added stream hook: support asynchronous IO on Redis, PDO, MySQLi. • 4.1.1: Added socket hook: allow building coroutine versions of PHP extensions for MongoDB, ZooKeeper and more. 34
  35. 35. Swoole 4.1: Stream Hook 35
  36. 36. Swoole 4.1: Stream Hook (cont.) 36
  37. 37. Swoole 4.1: Stream Hook (cont.) ≈ 37
  38. 38. Agenda • What Is Swoole? • Benchmark Results • Swoole 4: Asynchronous Programming with Coroutines • Top Swoole Use Cases • History of Swoole, and What's New in Swoole 4 • The Future of Swoole • Q & A 38
  39. 39. The Future of Swoole • Latest stable release: 4.2.13 • Swoole 4.3 • Add the ZooKeeper component and client. • Swoole 5.0 • Coroutines only. • Clean up legacy code. 39
  40. 40. The Future of Swoole (cont.) lots of changes in Swoole since 2018 40 Source: https://github.com/swoole/swoole-src/graphs/contributors
  41. 41. The Future of Swoole (cont.) 41 Swoole Inc was founded in 2017 by the author of Swoole. The company dedicates to the Swoole ecosystem, with more supports to the open source community: • SDLC process improvements. • Improved documentation. • Lots of refactors. • More components like ZooKeeper, Kafka, Hadoop, HBase, etcd, grpc, AMQP, MQTT, etc.
  42. 42. Questions? 42 https://joind.in/talk/902ff
  43. 43. Demin Yin Email: demin.yin@glu.com Twitter: @deminy GitHub: https://github.com/deminy Thanks 43

×