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

Advanced Dependency Management in Go using Fx

Preslav Mihaylov
October 19, 2020
620

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. Software Engineer @ Uber Tech. Trainer & Speaker ✍ Blogging

    at pmihaylov.com ‍ Open-sourcing at github.com/preslavmihaylov
  2. 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
  3. 5 Advanced Dependency Management in Go using Fx Preslav Mihaylov

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

    00/00/2020 An Example Hello World Web Application DEMO
  5. 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”
  6. 9 Advanced Dependency Management in Go using Fx Preslav Mihaylov

    00/00/2020 Fx == Dependency Injection App Framework
  7. 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
  8. 12 Advanced Dependency Management in Go using Fx Preslav Mihaylov

    00/00/2020 At its core, it simply manages providers and receivers
  9. 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…”
  10. 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…”
  11. 15 Advanced Dependency Management in Go using Fx Preslav Mihaylov

    00/00/2020 What Fx does is to connect providers to receivers
  12. 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
  13. 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
  14. 20 Advanced Dependency Management in Go using Fx Preslav Mihaylov

    00/00/2020 However, they are especially useful in a microservice environment
  15. 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...
  16. 22 Advanced Dependency Management in Go using Fx Preslav Mihaylov

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

    00/00/2020 Manual wiring works well for small to medium-size apps
  18. 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
  19. 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
  20. 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