Bounded Context is a very important term in Microservices. If you get the hang of it, then design of microservice becomes a bit clearer.
I would try to explain Bounded Concept in a very simple and clear manner. Then I would provide an example.
First let’s understand the meaning of the word context. Context means “a set of facts the surround a language, circumstance or event that determines it’s interpretation”. This means that the same entity could be interpreted differently depending on the specific concept.
So we can now understand the term bounded context as “the boundary within with an entity made certain sense”.
- Application to Microservices
- Example of Bounded Context
- More Technical Explanation of Bounded Context
1. Application to Microservices
Now in case of microservices, remember we need to split the application into services. Each service would have its own persistent data store which cannot be directly accessed by other services. Therefore, we could have the same entity duplicated in different services. Hence, this entity could mean different this depending on the context(the service within which it is used).
2. Example of Bounded Context
I found this example in the book “Designing a Microservice Oriented Application Running on a Serverless Architecture” by Enrico Balsamo.
Assuming we have an application that maintains records of books. This books could be ordered. Let’s say a user select some book(or books) into his online shopping cart. At the end he completes the order and pays.
With respect to bounded context, a book can be any of the following:
- something written by an author and with a title
- an item sold by a seller (from the concept of the seller)
- just an item in the cart (from the context of a shopping cart)
- goods dispatched by a courier
- and so on
While we are describing the same entity, we want to ensure that they are not tied together in any way. This would be in line with the principle of loose-coupling in microservices.
I hope the explanation above will suffice. But if you want to go a bit more in-depth, then you can read on!
3. More Technical Explanation of Bounded Concept
Now bounded-context closely relates to Domain-Driven-Design(DDD). DDD is explained in another article. This is where an application is partitioned into services where each aligns with a particular business domain.
According to Vadim Samokhin, bounded context is a logical boundary. It defines some tangible boundary between sub-domains. He came up with three different perspectives for bounded contexts:
domain expert perspective: It is the bounds within which certain business processes are implemented and certain terms/languages are applied.
software developer perspective: the way models are designed around certain sub-domain. For example, from identity management perspective, a person would be defined as a user with certain roles/privileges while from an organization’s HR perspective, a person is modeled as an employee.
run-time perspective: bounded context represents logical boundaries which is defined by contract of service where the model is implemented.
I hope this clarifies for you, the concept of Bounded Context in Microservices.