• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Principles of Microservices: Hide Implementation Details

September 2, 2020 by varunshrivastava Leave a Comment

Hiding the implementation of your service is a very important and crucial part of your overall architecture. It means protecting your service, data and implementation from the external world.

It is so important that it has become a principle in itself. It is the 3rd principle of Microservice.

I recommend you to read about the other two principles if you have not read it already –

  • Principles of Microservices – Architect a solution
  • Principles of Microservices – Culture of Automation

Domain-Driven Design has brought a new perspective in the way we design and architect our solutions. DDD has taught us one thing cleary that our services should be built around domains; they should be bounded in their context.

I’ve also talked a bit about the bounded context in the first principle but I want to talk more on its importance in the context of hiding implementation details.

Table of Contents

  • Bounded Context
  • Hide Your Database
  • Think About The Service Protocols
    • Prefer JSON over SOAP
  • Be Aware Of The Client Libraries
  • Conclusion

Bounded Context

When we think of Bounded Context, we should think of the bounded context as a separate entity that deals with one department very well.

For example – In an organization, a bounded context could be a Finance department. Everything related to finance should be looked after by that department. It is like one place for all your finance-related queries.

This department can be big in itself; it can have different sub-departments like Payroll or Invoicing etc… but it will have a defined context. A boundary that will separate it clearly from other departments.

Let’s take another example from Martin Fowler’s blog on Bounded Context.

taken from Martin Fowler’s article

This is indeed a very good example to learn about the bounded context.

Here you see two different domains clearly defined within a boundary. The Sales department and the Support department.

The sales depart have its own entities, models and data and similarly Support domain deals in its own world. However, in large infrastructures, there are chances that an entity will be shared between two different contexts.

As you can see in the above diagram. The Customer and Product entity seems to be sharing the same information. But they are different in both the context.

Like in the sales context, the customer entity will work in a different way and will behave differently. It will be focused more on selling the product to the person. It might have Customer geolocation for better targetting of product. It will have the entire pipeline information so that customers can reach out to their sales executives anytime they want.

Whereas, in the support context, customer information is used differently. Mostly limited to the identification of the ticket number.

It is very important for each domain to have a single unified model. This model should be enclosed and protected from the external world.

In earlier days we were advised to maintain a unified model for the entire business. But time has taught us that same entity behaves and acts differently in a different context and it is very important to limit the model to its domain.

Hide Your Database

This is another common pattern across multiple organizations. They work on a single database philosophy. And this philosophy is good only until your organization is small. But once it grows bigger and spawns more departments, slowly and steadily it increases the coupling between the consumers and small changes becomes difficult.

Tight coupling means less cohesion means a single change can break multiple parts of the same system.

Coupling and cohesion are terms which occur together very frequently and sounds confusing. Let me straighten this out a little bit. 

Coupling refers to the interdependencies between modules, while cohesion describes how related the functions within a single module are.

Coupling is bad but on the other hand, cohesion is good. With better cohesion, the information can flow easily through different parts of your entire system.

The single database philosophy breaks this cohesion.

Let me explain you by giving an example.

Suppose there is one Service A that talks to the database. Everything is good and works fine.

Service A talks to Database

But after some time there is another service that starts talking to the same database. This is the point where things start to break. Now, Service B has broken the boundaries of Service A.

Service B also starts talking to the same database

It can come anytime and grab whatever data it wants. The data is no more protected. And over time more and more consumers start talking to the database directly.

Now imagine if one service wants to perform some cleanup or optimization on the database. It can no longer do that safely. Because there are multiple consumers dependent on the same schema that you want to optimize and are expecting the data to be there.

So, what should be the ideal way…

Service B ask Service A to provide the required data
Service B ask Service A to provide the required data

The best approach would be to respect the service boundary. If Service B needs some data then it should communicate with Service A and ask for the same. It is then Service A responsibility to provide the required data.

By making this simple change, the data is better protected and is controlled from one place. So, if any changes need to be made in the schema, it would be much safer and easier to make without breaking any other consumer.

Think About The Service Protocols

This is another decision that is directly related to the productivity and connectivity of different services in the system.

A protocol is a set of defined rules which in this context decides how your service should communicate with each other.

There are different protocols for communication. Like in the younger days, people use to communicate using JAVA RMI (Remote Method Invocation). This method was the implementation of the idea of Remote Procedure Call. It is tightly coupled with the JVM. It invokes the methods in another JVM in order to communicate with different services.

I was not part of that era but the closest thing I’ve worked to that is SOAP. SOAP is a communication protocol which is based on XML-RPC and usually used over HTTP (this over TCP/IP); it is actually transport-neutral, which means it can be done over other transports, such as SMTP.

I’m strongly against this protocol if you have to start today because it tightly couples the client to the server. Client needs to know about all the details about the method it wants to invoke. The input parameters the output parameters etc… even though the client is only concerned with a single value in the output it will have to make a contract for everything that service has to offer.

Even if the data which is not used by the client changes in some way, the call to the service starts to fail.

Prefer JSON over SOAP

JSON is a very simple communication protocol. It is very lightweight, decoupled and simple to use protocol.

The services can send back only the limited data serialized in the JSON format which can then be used independently by the service. The consumer can choose to read them all the data or a part of data as per its use.

The consumer of the service is least interested in the implementation details of the calling service. A perfect way to hide the implementation details and send only the required data over the network.

Also helps in saving the network bandwidth;

Be Aware Of The Client Libraries

Bounded context and data protection don’t mean that you should break the DRY principle.

Instead of having similar code in different consumers. The Services can provide their own client libraries which can be used by consumers to easily talk to their services.

Client calling Service A directly
Client calling Service A directly
Client calling Service A via Client Libary
Client calling Service A via Client Libary

The above model respects the DRY principle. And not only that it also enables the Service A to have full control over the way it wants consumers to communicate with it.

It can also implement further enhancements to optimize network bandwidths and other stuff.

This is a good way to have control over every aspect of your service.

Conclusion

When thinking about Hiding Implementation Details we have talked about

Bounded Context – Be careful about what is shared and what is hidden.

Hide your database – Very very important to maintain the flexibility and protecting data

Think about the protocols – There are some which are better than others.

Use Client Libraries – helps in reducing code duplication but be extra careful what you put in your client library. It should not leak the implementation details of your service.

I hope you enjoyed this article. Let me know your thoughts in the comment below.

Related

Filed Under: Programming Tagged With: bounded context, client libraries, hide database, hide implementation details, microservices, principles of microservices, protocols

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version