This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
add my blog #20
Open
duchh-manabie
wants to merge
1
commit into
main
Choose a base branch
from
removing-dependency-between-microservices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add my blog #20
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+127 KB
content/posts/removing-dependency-between-microservices/images/new-system.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+134 KB
content/posts/removing-dependency-between-microservices/images/old-system.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions
72
content/posts/removing-dependency-between-microservices/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| +++ | ||
| date = "2022-08-23T10:00:00+07:00" | ||
| author = "duchh" | ||
| description = "A small approach we learned when migrating from monolithic to microservice" | ||
| title = "Removing dependency between microservices" | ||
| categories = ["Helm", "NATS-Jetstream"] | ||
| tags = ["Helm", "NATS-Jetstream"] | ||
| slug = "removing-dependency-between-microservices" | ||
| +++ | ||
|
|
||
| ***In this blog post, I will show for you guys solution to solve the problem we have met when deploying in microservices*** | ||
|
|
||
| #### Problem | ||
|
|
||
| A little bit information about our system: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a little bit of |
||
|
|
||
| **Helm**: It helps us install, upgrade, manage Kubernetes applications. | ||
|
|
||
| **Postgres**: We are using postgres version 13.1 for our database. | ||
|
|
||
| **Nats-jetstream**: We are using it for our message broker system. We also have a blog about | ||
| [it](https://blog.manabie.io/2022/03/set-up-nats-multi-tenant-in-golang/). | ||
|
|
||
| Currently, our system is in process migrate from monolithic to microservice. We deploy our all services by one helm chart, their | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the process of doing sth |
||
| deploy process are parallel. In order to start a service, it requires some logics like: `connect DB success, connect Nats success, | ||
| register logic, upsert streams of nats-jetstream, create subscriber of nats-jetstream,...` if one of these things fail then the deploy | ||
| process fail too. So what is `upsert streams of nats-jetstream, create subscriber of nats-jetstream` means :thinking:? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does ... mean |
||
|
|
||
| 1. **Upsert streams of nats-jetstream**: We write a library to check the `stream` by golang code. Is it exist? -> create a new one. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it exist |
||
| Is it have any update? -> update it. Is it need to delete? -> delete it. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it have, does it need |
||
|
|
||
| 2. **Create subscriber of nats-jetstream**: We need a `subscriber` to listen messages are published from publisher then it will | ||
| process messages base on our logic. | ||
|
|
||
|  | ||
|
|
||
|
|
||
| Scenario: This is a simulation about our system. We have two services `Teacher and Student`, **Teacher** service has a `teacher-stream` | ||
| stream, **Student** service has a `student-sub` subscriber and this subscriber will subscribe `teacher-stream` to receive messages. | ||
| If `teacher-stream` is not exist then `student-sub` is never be created -> **Student** service is depend on **Teacher** service, | ||
| this architecture is really bad. So we decide need to refactor this architecture before it makes problem. As you can see the image | ||
| above, when we deploy our application but `Teacher` service is fail then `Student` is service fail too. | ||
|
|
||
| #### Approach | ||
| At the beginning, we have some solutions: | ||
| 1. We think we will create streams, consumers of nats-jetstream in one place, we can use [nack](https://github.com/nats-io/nack) | ||
| to do that, but we have some custom logic when we want to `create, update, delete` stream or consumer. So this tool isn't enough | ||
| useful with our use case. | ||
|
|
||
| 2. We try to add logic `upsert teacher-stream stream` into both **Teacher** and **Student** services. Whether `teacher-stream` | ||
| isn't created by **Teacher**, it can be created by **Student**. So we can guarantee **Student** is never crash | ||
| due to `teacher-stream` isn't exist. But reality, we have a lot of subscribers subscribe to `teacher-stream` from other services. | ||
| Another thing, we want only one service own that stream, because that stream should be only managed by one team, it has many configs, | ||
| if other can modify its config not owner, this thing can make another problem. | ||
|
|
||
| 3. In this approach, we think about write a `job` to upsert stream, it solves two issues: we can use our library to work with | ||
| nats-jetstream when we upsert stream, we can do that logic in one place. But the question is how/when we run this `job` :thinking:? | ||
| The answer is we can use [helm hook](https://helm.sh/docs/topics/charts_hooks/). This approach is the best so we choose this | ||
| solution for our system. | ||
|
|
||
| #### New architecture | ||
|
|
||
|  | ||
|
|
||
| As you can see the image above, we use `pre-hook` to run the `job` upsert stream first, if the hook runs fail then helm will fail | ||
| and stop before deploying new version. On the order hand, the hook runs success then helm continue deploy services. As you can see, | ||
| **Student** service is deployed success whether **Teacher** service is crashing. They are not dependent on together anymore | ||
| :dancers: :dancers: :dancers: . | ||
|
|
||
| #### Conclusion | ||
| We have many other ways to solve this problem as long as it suits your system. At Manabie, we still have a lot of things need to do to | ||
| migrate our system from monolithic to microservices. Thanks all of you read!!! | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
show you guys