Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add my blog #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 content/posts/removing-dependency-between-microservices/index.md
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***
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show you guys


#### Problem

A little bit information about our system:
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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:?
Copy link

Choose a reason for hiding this comment

The 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.
Copy link

Choose a reason for hiding this comment

The 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.
Copy link

Choose a reason for hiding this comment

The 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.

![alt](./images/old-system.png)


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

![alt](./images/new-system.png)

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!!!