Event-driven architecture: Getting around code duplication for common logic

This is originally from a Slack thread. Copied here to make it more available (to non-members of the community slack and permanently.)
[You can join the community Slack here]

Nhilistic Kabali
Great community here. I am experimenting with event driven architecture with kafka as the event bus and all small services are dockerized and k8s is the hosting platform atm. My question is, how do you get around code duplication for common logic if code sharing is an anti pattern? Insights much appreciated! Will return the favor when I find an opportunity to give back.

dmartines
That’s a huge topic. My small opinion:

  1. First, don’t worry about it. Duplication is better than dependency!

  2. For really generalized logic, like serialization, date-parsing, etc., you can put that code into a library/package and be sure to use a proper versioning strategy that won’t require all services to be on the same version of the lib.

dmartines
Another interesting idea is a reusable side-car pattern (since you mention k8s) for common functionality, especially if the services are possibly written in different languages, where the main container can use an API from the sidecar over http or some language-agnostic protocol. This can help put cross-cutting concern like logging, authz/n, etc into a single codebase that other polyglot services can make use of, leveraging this specific k8s capability.

I would also say avoid the function-as-a-service (SOA) pattern - which you may have already concluded if you’ve made the choice for event-driven architecture.

Nhilistic Kabali
@-dmartines That’s gold! Thanks a mil for the response, I love the side car idea.

dmartines
@Nhilistic Kabali This is a great book: Building Event-Driven Microservices [Book]. focused more on the “event-driven” part than the general “microservices” aspect, although the topic is covered.

1 Like