什么时候使用微服务,什么时候不用

In this article, we are going to learn When to Use and When NOT to Use Microservices Architecture.

Microservices Architecture

By this article, we are going to understand the best use cases of Microservices Architecture and Design our E-Commerce application with Microservices Architecture.

Microservices Architecture

Microservices are an architectural approach to building and deploying software applications that involves breaking down an application into smaller , independent servicesthat can be developed, deployed, and maintained separately. While there are many benefits to using microservices, there are also some challenges that need to be considered.

  • So When to Use Microservices Architecture ?

When to Use Microservices Architecture

Here you can find list of reasons about using Microservices Architectures:

Make Sure You Have a "Really Good Reason" for Implementing Microservices

Before starting with Microservices, you should check if your application can do without microservices. When your application requires high level of independent scalability and requires agility to time-to-market with zero-down time deployments and updated independently that needs more flexibility, then you should embrace the Microservices Architecture.

But the idea is that you have to Make Sure You Have a "Really Good Reason" for Implementing Microservices. Developers tend to focus too much on the latest technology tool for example microservices. Instead of that, Developers and Architects should consider the reasons why for implementation. And they should also focus on the result that the technology tools drive to achieve that results.

Iterate with Small Changes and Keep the Single-Process Monolith as Your "Default"

When we talked about Modular Monolithic Architecture, we saw that Sam Newman and Martin Fowler offers Monolithic-First approach. Because Modular Monolithic architecture also cover a lot of benefits of Microservices with avoiding lots of complexity of Microservices. Because a single-process monolithic application comes with a simple deployment topology.

Sam Newman and Martin Fowler offers Start With Small Changes and Keep the Single-Process Monolith as Your "Default". They recommends that even they go to microservices, they should start with modular monolithic and simply refactor with turning a single module from the monolith into a microservice one by one and see how it works.

Required to Independently Deploy New Functionality with Zero Downtime

Then Microservices are extremely useful when an organization needs to make a change to functionality --- and deploy that functionality without affecting rest of the system. This is the power of microservices and its allow to deploy new functionality without any downtime.

Independent Services: Each service can be deployed and updated independently, providing more flexibility. An error in a microservice, only has an effect on a specific service and does not affect the entire application. Also, adding new features to a microservice application is easier than a monolithic one. Whether the application is very large or very small, adding new features and maintaining existing code is easy. Because it is sufficient to make changes only within the relevant service.

Required to Independently Scale a Portion of Application

Also if your application is Required to Independently Scale a Portion of Application, then microservices are excellent choose for this kind of operations.

**Better scalability:**Each service can be scaled independently. Therefore, the entire application does not need to be scaled. This saves a lot of time and money. Additionally, every monolithic application has limits in terms of scalability. However, multiplexing a service with traffic on the microservice is less inconvenient and able to handle the whole load. This is why most projects that appeal to a large user base, have begun to adopt the microservice architecture.

Required to Data Partitioning with different Database Technologies

Also if your application is Required to Data Partitioning with different Database Technologies, Then Microservices are extremely useful when an organization needs to store and scale data with different use cases.

**Technology Diversity:**Teams do not have to completely choose the technologies on which the services will be developed, they can choose the appropriate technology for the services they will develop over time. For example, a service can be developed with the python programming language in order to use "machine learning" features next to microservices developed on .Net. The desired technology or database can be used for each microservice.

Required to Autonomous Teams with Organizational Upgrade

Also if your company Required to Autonomous Teams with Organizational Upgrade then microservices will help to evolve and upgrade your teams and organizations.

Organizations need to distribute responsibility into teams, where each team makes decisions and develops software autonomously. When teams need to work independently without close coordination, a microservices-based architecture provide that pushes this kind of autonomy within the organization.

Microservices Architecture

When Not to Use Microservices --- Anti-Patterns of Microservices

Here you can find list of reasons that you shouldn't use Microservices Architectures: That means Anti-Patterns of Microservices:

Don't do Distributed Monolith

Make sure that you decompose your services properly and respecting the decoupling rule like applying bounded context and business capabilities principles. Otherwise, you are losing the benefits microservices. Also make sure that services should be loosely coupling. If your services has chatty communication with strong coupling, you will end up with highly-coupled service architecture that means, you have developed a distributed monolith, which comes with none of the benefits microservices should bring.

Distributed Monolith is the worst case because you increase complexity of your architecture without getting any benefit of microservices. Microservices are all about autonomy. When you lost autonomy, teams must coordinate during development and deployment and teams will highly dependent each other. By time you will loose your agility of new feature deployments.

So if you end up with Distributed Monolith, application has a higher latency, can have synchronization issues, and is a lot harder to manage and debug. When something breaks, coupled services are hell to debug.

Don't do microservices without DevOps or cloud services

Microservices are embrace the distributed cloud-native approaches. And you can only maximize benefits of microservices with following these cloud-native principles.

Some of these principles are

  • CI/CD pipeline with devops automations
  • Proper deployment and monitoring tools
  • Managed cloud services to support your infrastructure
  • Follow Key enabling technologies and tools like Containers, Docker, and Kubernetes.
  • Broke dependencies with following async communications using Messaging and event streaming services.

If you don't follow these principles in microservices architecture, you will again end-up with Distributed Monolith which is the worst case because you increase complexity of your architecture without getting any benefit of microservices.

Limited Team sizes, Small Teams

If you don't have a team size that cannot handle the microservice workloads, This will only result in the delay of delivery.

For a small team, a microservice architecture can be hard to justify, because team is required just to handle the deployment and management of the microservices themselves.

If your team size is good enough for those tasks, then it's easier to justify but if one person out of your five-person team is spending their time on these issues, that's a lot of valuable time not being spent building your product.

Brand new products or startups

If you are working on a new startup or brand new product which require significant change when developing and iterating your product, then you should not start with microservices. This kind of projects which's are pivot domain models so much, this will become to changes across service boundaries is an expensive way.

In general, I feel it's more appropriate to wait until enough of the domain model has stabilized before moving the microservices. Because microservices are so expensive when you re-design your business domains. Thats why for startups its good to start with modular monolithic and when domain is stabilialized then go microservices step by step. For startups, it is not clear that even if you do become successful enough to require a highly scalable architecture.

The Shared Database anti-pattern

Yes, it's possible to integrate a database for microservices. We can create a single shared database with each service accessing data using local ACID transactions. But if you're seriously considering this, stop right there and think twice.

Here you can see that Kelsey go one more step and offers that if we follow Modular Monolithic Architecture and build on Serverless and fully managed components , this would be prefect architecture most of software projects.

I also strongly agree with Kelsey for most of cases, but of course in some cases Microservices are un-avoidable chooses for large-scaled applications. The idea from Kelsey is that if you start with that setup, it can evolve and scale very easily for future requirements.

It is important to note that a Modular monolithic architecture is not a replacement for microservices architecture . Both approaches have their strengths and weaknesses , and the choice of architecture depends on the specific requirements of the system being built. A modular monolithic architecture can be a good starting point for a system that may eventually need to evolve into a microservices architecture as the system grows and complexity increases.

Design Modular Monolithic Architecture --- E-Commerce App

If we designe-commerce application with Modular Monolithic architecture, you can see the image below:

Modular Monolithic Architecture

According to architecture, we can encapsulate new feature developments into a module. And every module can implement its own architecture like Layered, Onion, Clean and so on.

Design Microservice Architecture --- E-Commerce App

If we designe-commerce application with Microservice architecture, you can see the image below:

Microservices Architecture

Product microservice can use NoSQL document database Shopping Cart microservice can use NoSQL key-value pair database and Order microservice can use Relational database as per microservice data storage requirements.

相关推荐
高松燈2 小时前
若伊项目学习 后端分页源码分析
后端·架构
维基框架4 小时前
Spring Boot 项目整合Spring Security 进行身份验证
java·架构
水泥工boss5 小时前
🚀微前端与模块联邦的深度结合(基于vue+vite)
前端·架构
在未来等你7 小时前
互联网大厂Java求职面试:云原生架构与微服务设计中的复杂挑战
java·微服务·ai·云原生·秒杀系统·rag·分布式系统
程序猿DD8 小时前
告别微服务,迎接SCS(Self-Contained Systems)?新概念还是炒冷饭?
后端·微服务·架构
曼岛_8 小时前
[架构之美]解决Windows 10主机与Windows 10虚拟机之间无法拖拽复制问题
windows·架构
腾讯云中间件9 小时前
限流系列之一:微服务常见限流方案及TSF限流原理
微服务·腾讯
season_zhu9 小时前
RxSwift:这可能是Notification最优雅的封装方式之一了
ios·架构·rxswift
前端付豪10 小时前
微信朋友圈推荐系统揭秘:内容去重 + 人设建模 + 多级缓存架构实战
前端·后端·架构