Managing Shared Components in Go Microservices with Fx

Managing Shared Components in Go Microservices


This article is part of the series Dependency Injection in Go using Fx

In many companies nowadays, microservices is the de facto way of handling service architecture. Some do it out of necessity as their application has reached a scale where the monolith is a bottleneck. Others, simply like being onboard the hype train. Whatever the scenario, the decision is often backed by the classical case for adopting microservices, which every junior dev studies extensively before their system design interview. What gets often neglected, however, is the problems which come with such an approach. Each of these problems usually demands a sophisticated solution, which raises system complexity. One such problem is how to reuse the shared infrastructure components in your microservices. Each of your services will probably have a distinct business logic, but it will also come with a big baggage of infrastructure code. These components usually don't change too much between your services - healthchecks, monitoring configs, logging, standard service configurations, etc. Fortunately, there is a very elegant solution for this problem for your Go services, which utilises the Fx Framework. It helps you by automatically managing your dependencies, but it can do much more than that as I'll show you in the upcoming sections. In this article, I will show you how to effectively extract your components into reusable & independent modules which can easily be shared across your Go services.
Continue Reading

Dependency Injection in Go using Fx

Dependency Injection in Go


This article is part of the series Dependency Injection in Go using Fx

When you initially start a Go project, your main function typically has a bunch of wiring code - initialising your routes, plugging in middlewares, initialising your template engines, loggers, etc.

This is one of the great things about Go - you don't have any magic happening behind the scenes. The code is all there and you can read it and debug it.

But as your software grows, you start feeling the growing pains - your main function starts becoming more and more convoluted. You start having all sorts of small bits and pieces plugged in here and there - healthchecks, database setup code, metrics, tracers, external API connections, etc etc.

And what if your application grows into a microservice architecture? What do you do when you have five different microservices demanding the same bunch of setup code, specific to your environment?

In this article, I will introduce you to Fx. It's a Go framework which solves both problems outlined above using dependency injection.

Let's jump in.

All the code from this article is available in this repo.

Continue Reading

Site Footer

BulgariaEnglish