Advanced Dependency Management in Go using Fx

Af78adc6caf494f74c1e4fb10a15c74a?s=47 Preslav Mihaylov
October 19, 2020
27

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.

Af78adc6caf494f74c1e4fb10a15c74a?s=128

Preslav Mihaylov

October 19, 2020
Tweet

Transcript

  1. Advanced Dependency Management in Go using Fx Preslav Mihaylov

  2. Software Engineer @ Uber Tech. Trainer & Speaker ✍ Blogging

    at pmihaylov.com ‍ Open-sourcing at github.com/preslavmihaylov
  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
  4. Managing Component Dependencies via Manual Wiring

  5. 5 Advanced Dependency Management in Go using Fx Preslav Mihaylov

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

    00/00/2020 An Example Hello World Web Application DEMO
  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”
  8. Using Dependency Injection and Fx Framework

  9. 9 Advanced Dependency Management in Go using Fx Preslav Mihaylov

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

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

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

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

    00/00/2020 Refactoring our Web App DEMO
  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
  18. Structuring Your Code Into Reusable Fx Modules

  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
  20. 20 Advanced Dependency Management in Go using Fx Preslav Mihaylov

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

    00/00/2020 Modularizing our Web App DEMO
  24. Conclusion

  25. 25 Advanced Dependency Management in Go using Fx Preslav Mihaylov

    00/00/2020 Manual wiring works well for small to medium-size apps
  26. 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
  27. 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
  28. 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
  29. 29 Talk’s name - Speaker 00/00/2020 Powered by Preslav Mihaylov

    preslav@pmihaylov.com