How to Share and Sync Code Between Microservices
Open source technologies make it easier and more effective to share and reuse code between microservices. Learn how to get started in this tutorial.
Join the DZone community and get the full member experience.Join For Free
A microservice architecture is great for building scalable codebases with less coupling, better separation of concerns, improved resilience, combining different technologies, and, most of all, better modularity and reusability for the components that build it.
However, modularity and reuse may often result in high-coupling or code duplications. Having different services tied to the same shared-lib undermines why we use services in the first place.
Using new open source technologies like Bit, it becomes easier and more effective than ever to share and reuse common code between our microservices. Let's see why and how.
Sharing Code Between Microservices
Before explaining how Bit can help solve this problem, let's set the main goals we want to achieve.
Sharing common code between our microservices while keeping our code DRY.
Avoiding coupling through shared-libs which eliminates the advantages of separating development process.
Enable simple changes and sync to the code we share between our microservices.
Microservices are prawned to code duplications. For example, any service which is used by other services will cause all these other services to duplicate the code needed in order to use that service's API.
Creating an npm package (with a new repo) for any such piece of code is highly impractical, and will generate a lot of overhead while making it harder to make changes to the code.
No Shared Libs, No Coupling
Bit is an open source project which brings a whole new approach to how we share and reuse code in our microservice architecture. Using Bit, you don't have to create a new repository or configure packages to share code instead of duplicating it.
Instead, you can simply define reusable parts of any existing repository and share to the other repositories — as packages or as tracked source code. This way you can make parts of any service reusable from other services without changing a single line of code in your codebase, creating more repos, or coupling your microservices together.
The best part is, Bit also lets you make changes to the code you share with any other service- so you can develop and modify that code from basically any repository.
Instead of coupling your microservices together via common libraries, you can simply isolate and sync any reusable code using the Bit's ability to isolate and track source code among projects. You can even still install this code with NPM in different repos, and still make changes from any end.
Let’s use Bit to isolate and share the reusable components
hello-world in the following project’s directory structure.
$ tree . ├── App.js ├── App.test.js ├── favicon.ico ├── index.js └── src └── common ├── left-pad ├── some-logic └── hello-world
First, let’s install Bit.
$ npm install bit-bin -g
Let’s initialize Bit for the project.
$ cd project-directory $ bit init
Let’s add the components to be tracked by Bit.
$ bit add src/common/* # use a glob pattern to track multiple components in the same path or a single path to track a single component.
Now let’s lock a version and isolate the components from the project.
$ bit tag --all 1.0.0 3 components tagged | 3 added, 0 changed, 0 auto-tagged
$ bit export username.scopename # Share components to this Scope exported 3 components to scope username.scopename
Note that using the
--eject flag you can also remove an exported component from your source code and add it as a package dependency in your project’s
That’s it. You can now install the components using your favorite package manager, or use
bit import to bring their source code into any repository, make changes, and sync them across your codebase. So, you eliminate the coupling between services — since you can make changes, update versions, and develop the code from each service — without compraminzing on being able to manage and sync them.
Microservices provide increased modularity and separation for your development process. Many services will use the same code, so sharing code between them is critical for your development and maintenance efforts.
However, coupling services through shared libs might ruin the point of having multiple different services. Creating different repos to publish every few code lines as a package to npm isn't practical.
Using new technologies like Bit we can have the best of both worlds: easily share common code between our microservices, make and sync changes from any end, and avoid the coupling created via adding third-party shared libraries.
Hope you found this useful!
Opinions expressed by DZone contributors are their own.