模块化单体
在一个部署单元内按功能划分模块,且各模块具有明确内部边界的架构。
结构与工作原理
按步骤查看箭头所示的流程。
模块的边界是逻辑边界。在一个部署单元内管理内部 API 和数据所有权。
预期收益
保留单一部署的运维简洁性,同时强制模块边界,支持模块独立开发、测试与使用本地事务。
权衡
默认整个应用共同扩容和部署,单一发布可能限制团队自主性。
并非微服务的对立面,而是推迟分布式处理,直至收益明确。如果已需要独立部署或扩容,可能不适用。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
领域边界逐渐明确,但仍需由一个团队通过一次发布来快速开发和运维时适用。将复杂业务划分为模块,在独立部署或扩容需求确认前避免承担分布式复杂性。
应用场景
一家有 30 名开发人员的中型零售电商公司将订单、支付、库存和配送作为一个部署单元运行。初期使用一个数据库,模块各自拥有模式。系统每月需承接数百万订单和峰值流量,但首先要确保领域边界与运维稳定。订单突增时先整体水平扩容,维护好 API 契约和内部事件,便于以后逐步拆出库存或支付模块。
部署原则
- 按订单、支付、库存、配送等业务能力划分模块,各模块拥有自己的模式与数据访问
- 跨模块仅使用公开 API 契约和内部事件,禁止循环依赖
- 作为一个制品和进程部署,同时分离各模块的配置与迁移
- 通用基础设施、可观测性和认证留在平台层,不直接侵入领域模块
- 明确包和数据边界,以便未来需要时通过网络边界拆出模块。
本地部署
使用一个制品和数据库降低本地部署运维负担,在构建、测试和代码审查中强制边界。开发环境保持单进程,同时按模块管理数据库模式所有权与迁移顺序。在本地约束下优先垂直扩容或整体水平扩容,只在确认瓶颈后逐步拆分模块。
云环境
将一个容器镜像部署到托管运行时、Kubernetes 或无服务器容器,利用托管数据库、消息与观测服务。默认对整个应用自动伸缩,并为高流量模块准备独立工作负载。使用蓝绿或金丝雀发布,维持数据库迁移兼容性,并收集模块级指标以评估成本和隔离。
Kubernetes
通过一个 Deployment 部署,设置 readiness/liveness 探针、资源 requests/limits 与 HPA。以包和 API 契约保持模块边界,仅拆分时升格为独立 Deployment 与 Service。用 ConfigMap、Secret、网络策略、滚动更新和 PodDisruptionBudget 保证稳定性,数据库迁移用 initContainer 或单独 Job 控制。
故障应对
典型故障包括共享数据库或连接池耗尽,某模块慢查询或内存泄漏拖慢整个进程,部署时模式不兼容,以及循环依赖触发级联故障。用告警、链路追踪和模块指标尽早定位范围,通过流量阻断、功能开关、回滚、连接池隔离或终止异常查询阻止扩散,再隔离故障模块或热修复。加强边界、超时、断路器与负载测试以防复发。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
在一个部署单元内按功能划分模块,且各模块具有明确内部边界的架构。
A modular monolith is a single deployable system with clear internal boundaries between modules.解决的问题有证据
不通过网络分布就能分离职责,从而推迟分布式系统中的协调、部署、集成与运维复杂性。
It separates responsibilities without immediately taking on network-level coordination, deployment, integration, and operational complexity.关键组件与资源有证据
一个部署单元,内部模块各自拥有数据,只暴露范围有限的接口。
A modular monolith is one deployable unit composed of modules that own their data and expose narrow interfaces.代表性使用场景有证据
仍在理解业务领域、主要由一个团队负责且分布式运维能力尚不成熟时,可作为实际起点。
practical starting point when the domain is still being understood, one team owns most of the work应用场景示例未确认
未记录已确认的原文证据,这不代表模式不适用。
不适用情形与注意事项有证据
并非微服务的对立面,而是推迟分布式处理,直至收益明确。如果已需要独立部署或扩容,可能不适用。
The pattern is not an anti-microservices position. It is a choice to delay distribution until its benefits are real:本地部署:开发、发布与运维有证据
单一制品、进程和数据库简化本地部署与运维。
It is monolithic (one artefact, one process, usually one database server)云环境:开发、发布与运维有证据
在云中可兼顾运维简洁性、模块化与可维护性。
combines operational simplicity with modularity and maintainabilityKubernetes:开发、发布与运维有证据
在 Kubernetes 中作为单一工作负载部署,配置健康检查、自动扩缩容与滚动更新,仅拆分有实际需要的模块。
It manages deployments, scaling, networking and everything else that goes into running a distributed application.故障应对有证据
共享进程与数据会使某个模块的故障传给其他模块,因此需要尽早检测、隔离、切断并恢复。
A failure in one part of the system can cascade into failures in other parts of the system.优势有证据
保留单一部署的运维简洁性,同时强制模块边界,支持模块独立开发、测试与使用本地事务。
You keep operational simplicity of a monolith while enforcing the boundaries that microservices try代表性技术与工具有证据
代表手法是按 DDD 子域与限界上下文划分领域模块,而不是按技术层划分。
based on DDD subdomains/bounded context rather than technical layers更多参考资料 (23)
- When (modular) monolith is the better way to build software ↗
- Modular monolith in Software Architecture Decision-Making · liveBook ↗
- Understanding the modular monolith and its ideal use cases | TechTarget ↗
- Modular Monolith (/docs/architecture-patterns/modular-monoliths) ↗
- Architectural patterns for modular monoliths that enable fast flow ↗
- Lesson 20.3 — Modular monolith | System Design From First Principles ↗
- Modular Monolith Architecture ↗
- The Right Number of Services Is Often One. | Microservices Recipes: The Architect’s Field Guide ↗
- Reference Architecture: GKE Enterprise hybrid environment - part 2 - Dec '24 - Google Docs ↗
- From Monoliths to Microservices: Application Modernization With Kubernetes ↗
- Modular Monolith Architecture in Cloud Environments: A Systematic Literature Review ↗
- Engineering Principles for Building a Successful Cloud-Prem Solution - InfoQ ↗
- How to Run Kubernetes Across On-Premises and Public Cloud: Four Hybrid Architecture Models and the Seven Layers to Standardize ↗
- Learning Path: Transform a monolith into a GKE app - Modularize the monolith ↗
- Devnexus 2027 - Migrating a Mission Critical Monolith to a Cloud Native Architecture with Julian Ortiz ↗
- A Private Cloud "Kubernetes Service" deployable anywhere. | CNCF End User Community ↗
- REL03-BP01 Choose how to segment your workload - Reliability Pillar ↗
- Monoliths, services and the cost of a network boundary · Cloud Sandbox ↗
- Tutorial on systems with antifragility to downtime | Computing | Springer Nature Link ↗
- Inside Shopify’s Modular Monolith - by Dr Milan Milanović ↗
- Google SRE - Cascading Failures: Reducing System Outage ↗
- Resilient Microservices: A Systematic Review of Recovery Patterns, Strategies, and Evaluation Frameworks ↗
- Modular Monolith Explained: Boundaries and Trade-offs ↗