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

Real-time Services with Silex and Symfony – deSymfony 2016

Ronny López
September 16, 2016

Real-time Services with Silex and Symfony – deSymfony 2016

En los micro-servicios modernos, los cuales se basan en eventos, comunicación bidireccional, stream multiplexing y otras formas de sincronización de los datos, la arquitectura clásica basada en HTTP/REST no siempre es la mejor opción.

Algunas veces las aplicaciones requieren servicios donde es necesario mantener una comunicación asíncrona de eventos fluyendo en ambas direcciones, por ejemplo, un cliente conectado por Websocket.

¿Qué hacer en estos casos si tenemos la gran mayoría de nuestros servicios implementados con Symfony o Silex y queremos tener comunicación en real time con un cliente? ¿Qué opciones tenemos, qué soluciones podemos aplicar y con qué trade-offs?

El objetivo de la charla es dar respuestas a estas preguntas, basándonos en casos de usos reales en producción, comparar las distintas alternativas existentes y ver ejemplos prácticos de integración Silex/Symfony.

Ronny López

September 16, 2016
Tweet

More Decks by Ronny López

Other Decks in Programming

Transcript

  1. deSymfony 16-17 septiembre 2016
    Madrid
    MICROSERVICIOS EN
    TIEMPO REAL CON
    SYMFONY Y SILEX
    Ronny López

    View Slide

  2. deSymfony
    ¡Muchas gracias a nuestros
    patrocinadores!

    View Slide

  3. About me
    @ronnylt
    Ronny López
    Technical Lead
    Opinions are my own

    View Slide

  4. Agenda
    • Concepts and foundations
    • Real-time communication patterns
    • Implementations
    • Examples

    View Slide

  5. Real-time
    A system is said to be real-time if the total
    correctness of an operation depends not only upon
    its logical correctness, but also upon the time in
    which it is performed

    View Slide

  6. Real-time
    Applications must guarantee response within
    specified time constraints, often referred to as
    “deadlines”

    View Slide

  7. Real-time
    Applications in which the computer must
    respond as rapidly as required by the user

    View Slide

  8. Criteria for real-time
    • Hard – missing a deadline is a total system failure

    • Firm – infrequent deadline misses are tolerable, but
    may degrade the system's QoS. Results are NOT
    usefulness after its deadline

    • Soft – the usefulness of a result degrades after its
    deadline, thereby degrading the system’s QoS

    View Slide

  9. Real-time
    Soft real-time

    View Slide

  10. Soft real-time
    Typically used to solve issues of concurrent
    access and the need to keep a number of
    connected systems up-to-date through
    changing situations

    View Slide

  11. Soft real-time use cases
    • Live audio-video systems
    • Users collaboration
    • Messaging applications, etc…
    • Real-time analytics
    • Gaming
    • Etc…

    View Slide

  12. The road to 500 million
    Symfony downloads
    https://symfony.com/500million

    View Slide

  13. Why adding a soft

    real-time feature?
    • To improve end-user experience
    • Due to insufficient scaling capacity

    View Slide

  14. Real-time
    Communication on the
    Web

    View Slide

  15. A Bit of History
    • Flash
    • Ajax (XMLHttpRequest)
    • Comet (reverse Ajax)
    • WebSocket
    • Polyfills

    View Slide

  16. The Modern Web
    • WebSockets
    • HTTP/2

    View Slide

  17. WebSockets
    • Full-duplex communication channels over a
    single TCP connection
    • Currently supported in most major
    browsers
    • Can be used by any client or server
    application

    View Slide

  18. HTTP/2
    • Major revision of the HTTP
    • Replacement for how HTTP is expressed
    “on the wire”
    • Focus on performance, end-user perceived
    latency, network and server resource usage

    View Slide

  19. The Mobile Internet
    • Inestable connections
    • HTTP & TCP slow-start are usually not a
    good match for constantly dropped
    connections
    • Network interface kills battery
    • Large responses + periodic polling = bad

    View Slide

  20. What Every Web
    Developer Should Know
    About Networking and
    Browser Performance

    View Slide

  21. Real-time
    Communication
    Patterns

    View Slide

  22. The “actors”
    Client Server
    Peer Peer

    View Slide

  23. Basic Patterns
    • Remote Procedure Call (RPC)
    • PUB/SUB

    View Slide

  24. RPC
    • Allows to call a procedure (function)
    remotely
    • Involves peers of these three roles
    Caller Callee
    Dealer

    View Slide

  25. RPC – Example 1
    Something you usually do with Ajax
    Browser Server
    GetConference(123)
    {name:deSymfony,where: Madrid}

    View Slide

  26. RPC – Example 2
    Push data to the browser
    Browser
    Server DisplayOffer(offer)

    View Slide

  27. RPC – Example 3
    Communication between micro-services
    Auth
    Server
    login(user)
    Analytics Payments
    track(user) process(order)

    View Slide

  28. Publisher/Subscriber
    • A peer subscribe to a topic
    • Another peer publish a message about this
    topic
    • All publishers interested in the topic
    receives the message

    View Slide

  29. PUB/SUB – Example 1
    Notifications
    Browser
    Server
    updateStats(stats)
    Browser Browser
    Subscribed To: StatsUpdate

    View Slide

  30. PUB/SUB – Example 2
    Micro-services Synchronization
    Auth
    Admin
    Service
    updateSettings(freshSettings)
    Encoding Payments
    Subscribed To: SettingsChanges
    Analytics

    View Slide

  31. Web Application Messaging Protocol

    http://wamp-proto.org/

    View Slide

  32. Not to be confused with WAMP:
    ”Windows + Apache + MySQL + PHP"

    View Slide

  33. WAMP
    • Open standard WebSocket subprotocol
    • Provides two application messaging
    patterns in one unified protocol
    • Remote Procedure Calls
    • Publish & Subscribe

    View Slide

  34. WAMP Features
    • Enables different technologies, processes,
    machines, etc… to communicate with each
    other, in soft real-time

    View Slide

  35. WAMP Features
    • Based on modern Web standards:
    WebSocket, JSON and URIs
    • Designed with first-class support for
    different languages in mind (Polyglot)

    View Slide

  36. Unified Application Routing
    Routing of events (for PUB/SUB) and routing of
    calls (for RPC) in one unified protocol
    Caller Callee
    Dealer
    Publisher Subscriber
    Broker

    View Slide

  37. Dealer
    • Routes calls from the caller to the callee
    and routes back results or errors
    • Callers and callee don’t know about each
    other
    • Applications using RPC benefit from these
    loose coupling
    Caller Callee
    Dealer

    View Slide

  38. Broker
    • Keeps a book of subscriptions
    • Forward the events (messages) to all
    subscribers
    • Publisher are subscribers are loosely
    coupled
    Publisher Subscriber
    Broker

    View Slide

  39. Unified Protocol
    • When you combine a Broker and a Dealer
    you get what WAMP calls a Router
    Router Broker Dealer
    = +

    View Slide

  40. The Big Picture
    WAMP Router
    (Dealer + Broker)
    Browser
    Browser
    Browser
    Browser
    Mobile
    Clients
    Services

    View Slide

  41. Do we really need
    another wheel?
    How does WAMMP compare to other technologies

    View Slide

  42. Criteria
    • Pub/Sub
    • RPC
    • Routed RPC (not only point-to-point)
    • Web native: run natively on the Web (without
    tunneling or bridging)
    • Cross Language
    • Open Standard

    View Slide

  43. Tech PubSub RPC
    Routed
    RPC
    Web native
    Cross
    Language
    Open
    Standard
    WAMP ✔ ✔ ✔ ✔ ✔ ✔
    Ajax ✔ ✔ ✔
    Comet ✔ ✔
    JSON-RPC ✔ ✔ ✔ ✔
    socket.io ✔ ✔
    ZMQ ✔ ✔
    XMPP ✔ ✔ ✔ ✔

    View Slide

  44. Implementations
    • Client libraries for most popular languages
    • Full featured router implementations in
    several languages

    View Slide

  45. WAMP Routers
    • crossbar.io – Advanced, open-source, full
    featured, supported by the creators of
    WAMP

    • Your own… It’s an open protocol

    View Slide

  46. WAMP Ecosystem
    • Thruway – library built in PHP that provides
    both a client and a router
    • Turnpike – router implemented in Go

    • wamp.rt – router for NodeJS

    • Erwa – router implemented in Erlang


    View Slide

  47. Choosing an
    implementation
    Let’s talk about trade-offs

    View Slide

  48. Is PHP suitable for soft
    real-time applications?

    View Slide

  49. Is PHP the right tool
    for the job?

    View Slide

  50. No.

    View Slide

  51. No?

    View Slide

  52. Why not?

    View Slide

  53. Let’s use
    Node.js ! It’s an opportunity to
    deploy Erlang or
    Golang, or …

    View Slide

  54. Languages War

    View Slide

  55. Conflicts Everywhere

    View Slide

  56. Conflicts everywhere
    Trade-offs everywhere

    View Slide

  57. Trade-off
    • Situation that involves losing one
    quality or aspect of something in
    return for gaining another quality
    or aspect

    • It often implies a decision to be
    made with full comprehension of
    both the upside and downside of
    a particular choice

    View Slide

  58. Is PHP the right tool
    for the job?
    There is not simple answer

    View Slide

  59. The simpler answer I know is:
    “I don’t care”

    View Slide

  60. PHP Codebase

    (Symfony, Silex, Laravel, Drupal, etc…)
    Clients
    Web, Mobile, etc…
    Request/Response

    View Slide

  61. Request/Response
    Real-time API

    Pub/Sub, RPC, etc..
    RPC

    View Slide

  62. • A big ecosystem of thousands of useful
    libraries and components easily installable
    thanks to Composer
    • Very powerful template engines, ORMs,
    etc…

    View Slide

  63. • We have implemented very powerful design
    patters in PHP coming from Java and other
    languages
    • We have several thousands of high quality code
    running on production
    • We have invested multiple hours testing,
    refactoring and improving the codebase

    View Slide

  64. It’s here to stay

    View Slide

  65. View Slide

  66. Remember
    Full comprehension of both the upsides and downsides
    of a particular choice

    View Slide

  67. Downsides
    • We have to introduce a new stack to
    provide real-time features

    View Slide

  68. Upsides
    • Possibility to introduce real-time features
    without deep modifications in the current
    codebase
    • No need to learn a whole new language/
    stack, with the implications it has
    • Loosely coupled systems

    View Slide

  69. Upsides cont…
    • Opens the door to write reactive, event-
    based, distributed architectures
    • Scalability is easier to achieve by
    distributing messages to multiple systems

    View Slide

  70. Examples

    View Slide

  71. The Stack
    • crossbar.io used as the router (dealer+broker)
    • PHP client gathers and publish events
    • Silex/Symfony backend serve the data and
    contains the biz logic

    View Slide

  72. crossbar.io
    • Networking platform for distributed and
    micro-services applications
    • Full implementation of the WAMP protocol
    • Feature rich, scalable, robust and secure
    • It takes care of the hard parts of messaging
    so you can focus on your app's features

    View Slide

  73. View Slide

  74. Tips and Tricks
    • WSS
    • Deadlines
    • Timeouts
    • Retries with back-off, etc…
    • Idempotency

    View Slide

  75. Opinionated
    Conclusion
    • Understand core concepts and patterns,
    technology is volatile
    • Question everything: Why?, why not?
    • Decide based on several factors: user
    experience, scalability, feasibility, developer
    experience, maintenance costs/debt, etc…

    View Slide

  76. Don’t Stop Here
    • gRPC – A high performance, open-source
    universal RPC framework from Google


    http://www.grpc.io/

    • IoT, WoT, etc… – real world objects
    connected to the wider internet

    View Slide

  77. References
    • WAMP Proto – http://wamp-proto.org/
    • https://github.com/voryx/ThruwayBundle
    • https://www.infoq.com/articles/websocket-
    and-http2-coexist

    View Slide

  78. Questions
    • What about React PHP?
    • Multi-Threading in PHP with pthreads?

    https://gist.github.com/krakjoe/6437782/
    • Micro-services what?

    View Slide

  79. Gracias!
    @ronnylt
    Work with us!

    View Slide