DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • From Monolith to Containers: Real-World Migration Blueprint
  • Automating Kubernetes RBAC Sync With LDAP Entitlements Using Python
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice

Trending

  • Chaos Engineering for Microservices
  • Turbocharge Load Testing: Yandex.Tank + ghz Combo for Lightning-Fast Code Checks
  • Designing Embedded Web Device Dashboards
  • Managing Encrypted Aurora DAS Over Kinesis With AWS SDK
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Share and Sync Code Between Microservices

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.

By 
Joni Sar user avatar
Joni Sar
·
Updated May. 20, 19 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
139.2K Views

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 microservicesSharing Code Between Microservices

Before explaining how Bit can help solve this problem, let's set the main goals we want to achieve. 

  1. Sharing common code between our microservices while keeping our code DRY.

  2. Avoiding coupling through shared-libs which eliminates the advantages of separating development process. 

  3. 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
Image title

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.

Example Workflow

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 left-pad, some-logic, and 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.

Let’s add build and test environments. 

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

Now let’s share the components to a remote Scope.

$ 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 package.json file.

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.

Conclusion

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!

microservice Sync (Unix)

Opinions expressed by DZone contributors are their own.

Related

  • From Monolith to Containers: Real-World Migration Blueprint
  • Automating Kubernetes RBAC Sync With LDAP Entitlements Using Python
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: