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

Services, Architecture and Channels

Services, Architecture and Channels

A talk I gave at DjangoCon Europe 2017 in Florence, Italy

Andrew Godwin

April 03, 2017
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. Andrew Godwin @andrewgodwin

    View full-size slide

  2. Andrew Godwin
    Hi, I'm
    Django core developer
    Senior Software Engineer at
    Apparently now does software architecture

    View full-size slide

  3. The Monolith

    View full-size slide

  4. The Monolith
    Only one version of any dependency
    Everything can import everything
    Deployed all at once
    No separation
    Side-effects from other code

    View full-size slide

  5. Services
    Code split up by purpose/team
    Defined cross-boundary API
    Deployed separately
    Can use different versions of dependencies
    Isolated from each other

    View full-size slide

  6. Why services?

    View full-size slide

  7. Easier to manage
    Smaller, self-contained problems

    View full-size slide

  8. Independent Scaling
    And easier performance analysis

    View full-size slide

  9. Faster Individual Deployment
    Less to deploy at once

    View full-size slide

  10. Complex Interdependencies
    Harder to deploy & track bugs

    View full-size slide

  11. Requires great communication
    Teams need calling contracts and APIs

    View full-size slide

  12. More points of failure
    Not just one set of homogenous servers

    View full-size slide

  13. No more quick hacks
    Separation forces a level of code design

    View full-size slide

  14. Switching To Services
    Or: How I Learned To Stop Worrying
    And Love The Monolith

    View full-size slide

  15. Identify the "cut points"
    You might need to make some

    View full-size slide

  16. Allocate inventory
    Calculate Price &
    Charge card
    Finalise order
    Make Order Row &

    View full-size slide

  17. Make Order Row
    Allocate inventory
    Calculate Price
    Charge card
    Finalise inventory
    Finalise order

    View full-size slide

  18. Make Order Row
    Allocate inventory
    Calculate Price
    Charge card
    Finalise inventory
    Finalise order

    View full-size slide

  19. Define APIs between services
    Behave like all other teams are third-party

    View full-size slide

  20. Separate Datastores & Servers
    Make them as separate as possible

    View full-size slide

  21. Communication & Transport

    View full-size slide

  22. Service 2 Service 3
    Service 1

    View full-size slide

  23. Service 2 Service 3
    Service 1
    Direct Communication
    (20 services? 190 connections!)

    View full-size slide

  24. Service 2 Service 3
    Service 1
    Direct with discovery
    Orchestrator

    View full-size slide

  25. Service 2 Service 3
    Service 1
    Centralised Routing
    Router

    View full-size slide

  26. Service 2 Service 3
    Service 1
    Message Bus

    View full-size slide

  27. Centralised Comms
    Tradeoffs
    Distributed Comms
    Single point of failure Nasty partial failures

    View full-size slide

  28. At-least-once delivery
    Tradeoffs
    At-most-once delivery
    Some messages duplicated Some messages lost

    View full-size slide

  29. First-In-First-Out
    Tradeoffs
    First-In-Last-Out
    Easily backlogged Wide range of latencies

    View full-size slide

  30. Channels & ASGI

    View full-size slide

  31. Channel
    Layer
    Interface
    Server
    Worker
    Server
    Process 1
    ASGI ASGI
    Asynchronous
    socket handling
    Synchronous
    Django project
    Interface
    Server
    Worker
    Server
    ASGI ASGI
    Worker
    Server
    ASGI
    Process 2 Process 3
    Process 4

    View full-size slide

  32. Service 2 Service 3
    Service 1
    Channel Layer

    View full-size slide

  33. Service
    Client
    inventory.request
    response.aF53Vds21

    View full-size slide

  34. At-most-once delivery
    ASGI's Tradeoffs
    You have to design for potential loss
    Low-latency but non-persistent
    Good for protocols, bad for important task queues
    Capacity, Backpressure and FIFO
    Informs producers quickly about pileups in the queue

    View full-size slide

  35. Top Service-Oriented Architecture Tips

    View full-size slide

  36. Per-request "correlation IDs"
    Track a set of service calls through the stack

    View full-size slide

  37. Feature Flag message headers
    Bundle them in, don't have every service query them

    View full-size slide

  38. Source Of Truth
    Each data model has a service that owns (& caches) it

    View full-size slide

  39. Metrics. Metrics everywhere.
    Both performance and network health

    View full-size slide

  40. Design for failure
    Don't assume two things will both succeed

    View full-size slide

  41. DO NOT START OFF WITH SERVICES
    Write separate Python libraries instead

    View full-size slide

  42. Thanks.
    Andrew Godwin
    @andrewgodwin

    View full-size slide