- what is microservices
- A microservice should be self contained
- To reduce the crashes chance brought by dependencies between services, we introduced the event bus.
- Brought data duplication and understanding obstacle
It contains all the stuff that works one feature correctly.
- Data in microservice
- Each service gets its database(if it needs one) for the following reasons[Database-Per-Service pattern]
- We want every service to be able to act independently without depending on any service
- If each service has its database, we can optimize what type of database we pick
- A single database shared between many services would be a single point of failure
- The data schema of a database might be changed unexpectly
- Services will never, never reach into another service's database
- Each service gets its database(if it needs one) for the following reasons[Database-Per-Service pattern]
Managing data between different services is a challenge.
-
Big problem with data in services
- We don't allow services to interact with databases that not owned by it directly
-
communication strategies between services to solve data management in microservices
- Sync
Services communicate with each other using direct requests
- Upside:
- easy to understand
- don't need a database
- downside
- Introduce an dependency between services
- If any inter-service request fails, the overall request fails
- The entire request is only as fast as the slowest request
- can easily introduce webs of request
- Async Services communicate with each other using events.
- Sync
Services communicate with each other using direct requests
-
Communiation using Event bus
- The event flow:
- Service D emit event data {type: UserQuery, data: {id}}
- Service A(the UserService) emit data {type: UserQueryResult, data: {id, name: 'jill'}}
- The order and the product purchase services handle the purchase and order information based on user id
- Still has the downside mentioned in synchronouse communication way
- The event flow:
-
A crazy way of storing data
Define the service's goal ==> Define the service's data schema ==> define the event flow for services
- For service D, the goal is
Given the ID of a user, show the title and image for every product they have erver ordered
- pros and cons Nowdays, data storage is cheaper
Define the service's goal ==> Define the service's data schema ==> define the event flow for services
- Define the goal for services
- Reduce dependency chances by data duplication and using event bus
- microservice should be self contained and never, never reach database of another service.