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

Advanced Dependency Management in Go using Fx

Preslav Mihaylov
October 19, 2020
540

Advanced Dependency Management in Go using Fx

These are the slides from the talk I gave at Golab Conf 2020 - https://golab.io/

The links from the slides:
1. Personal Site - https://pmihaylov.com/
2. Github Profile - https://github.com/preslavmihaylov
3. Fx Framework - https://github.com/uber-go/fx
4. Fx Framework Tutorial (Part 1) - https://pmihaylov.com/dependency-injection-go-fx/
5. Fx Framework Tutorial (Part 2) - https://pmihaylov.com/shared-components-go-microservices/
6. Introduction to Guice from Google (Java) - https://www.youtube.com/watch?v=hBVJbzAagfs&ab_channel=GoogleDevelopers

Talk Abstract follows...

In this talk, I will show you how to handle complex dependency graphs in Go using the Fx framework. It can make your life easier in many regards, but its greatest value is in the way it enables you to modularize & share your infrastructure code across multiple microservices.

The talk focuses on introducing the concept of dependency injection & its implementation in Go using the Fx framework (https://github.com/uber-go/fx). Apart from providing a dependency injection mechanism, this framework enables you to separate your application into distinct, independent modules, which can be shared across multiple codebases. This is especially valuable in a microservice environment, where every service has a lot of "boilerplate" infrastructure code which has to be present on any service regardless of its business logic.

In a typical microservice architecture, every service needs some kind of a healthcheck, an ELK/monitoring client, tracing clients & various other integrations specific to your server environment. By extracting this common piece of software in a module, which can be "plugged in" and reused across services using Fx, one can greatly reduce development time & maintenance cost as you'd need to deal with "wiring infra" only once & reuse that across your codebase.

Preslav Mihaylov

October 19, 2020
Tweet

Transcript

  1. Advanced Dependency
    Management in
    Go using Fx
    Preslav Mihaylov

    View full-size slide


  2. Software Engineer @ Uber
    Tech. Trainer & Speaker
    ✍ Blogging at pmihaylov.com
    ‍ Open-sourcing at
    github.com/preslavmihaylov

    View full-size slide

  3. 3
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Here’s what’s coming...
    Managing Component Dependencies via Manual Wiring
    Using Dependency Injection and Fx Framework
    Structuring code into reusable Fx modules

    View full-size slide

  4. Managing
    Component Dependencies
    via Manual Wiring

    View full-size slide

  5. 5
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Manual Wiring == Manually Injecting Dependencies

    View full-size slide

  6. 6
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    An Example Hello World Web Application
    DEMO

    View full-size slide

  7. 7
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Manual Wiring - Pros & Cons

    Code is easy to understand
    No magic behind the scenes

    Hard to maintain for large
    projects
    Every project has to deal with the
    “things every service needs”

    View full-size slide

  8. Using
    Dependency Injection and
    Fx Framework

    View full-size slide

  9. 9
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Fx == Dependency Injection App Framework

    View full-size slide

  10. 10
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Fx == DI Application Framework
    DI Framework?
    Component wiring is done
    automatically for you
    App Framework?
    The entire app lifecycle is
    managed rather than it being a
    plug-n-use library

    View full-size slide

  11. 11
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    So, what does it do?

    View full-size slide

  12. 12
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    At its core,
    it simply manages providers and receivers

    View full-size slide

  13. 13
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Providers say
    “Here’s an instance of this component.
    Use it as you please…”

    View full-size slide

  14. 14
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Receivers say
    “I need an instance of components X, Y, Z.
    Please provide them to me…”

    View full-size slide

  15. 15
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    What Fx does is to
    connect providers to receivers

    View full-size slide

  16. 16
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Refactoring our Web App
    DEMO

    View full-size slide

  17. 17
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Using a DI Framework - Pros & Cons

    Component Wiring is MUCH
    easier
    Less code => Less work
    Enables reusable modules

    Project onboarding is tougher
    Harder to debug & trace apps

    View full-size slide

  18. Structuring Your Code
    Into Reusable Fx Modules

    View full-size slide

  19. 19
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Fx Modules
    allow you to extract code into
    separate independent layers/packages

    View full-size slide

  20. 20
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    However, they are especially useful
    in a microservice environment

    View full-size slide

  21. 21
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    All services need some baseline infra
    health checks, logging, tracing, metrics, configs...

    View full-size slide

  22. 22
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Accounting Tax Calculations Trip Dispatcher
    Infrastructure
    Module
    Infrastructure
    Module
    Infrastructure
    Module

    View full-size slide

  23. 23
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Modularizing our Web App
    DEMO

    View full-size slide

  24. 25
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Manual wiring
    works well for small to medium-size apps

    View full-size slide

  25. 26
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    A Dependency Injection Framework (Fx)
    allows your servicce to scale \w more components

    View full-size slide

  26. 27
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Modularizing your Application enables
    your services to reuse common dependencies
    you to create boundaries across your application

    View full-size slide

  27. 28
    Advanced Dependency Management in Go using Fx
    Preslav Mihaylov
    00/00/2020
    Wanna go the extra mile?
    Practice setting up & using Fx here and here
    See how it’s done in Java by Google

    View full-size slide

  28. 29
    Talk’s name - Speaker
    00/00/2020
    Powered by
    Preslav Mihaylov
    [email protected]

    View full-size slide