--- url: "http://microservices.io/patterns/cn/data/shared-database.html" titulek: "Shared database" h1: "Pattern: Shared database" popis: "" jazyk: "" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:12:31" znaku: 8339 slov: 704 kodovani: "iso-8859-2" sha256: "39412c0b25149161807d619f9edff3ac1d81992794f6827bc0b822bf9b3f4b7d" --- #### [Chris Richardson 的微服务架构网站](/index.html) **本站由[Kong](https://konghq.com/)提供支持** #### 微服务架构咨询和培训服务 本站点由 Chris Richardson 编写和维护,他是经典技术著作《POJOS IN ACTION》一书的作者,也是 cloudfoundry.com 最初的创始人。Chris çš„ç ”çŠśé˘†ĺŸŸĺŒ…ć‹Ź Spring、Scala、微服务架构设计、领域驱动设计、NoSQL 数据库、分布式数据管理、事件驱动的应用编程等。Chris 是一位连续创业者,[eventuate.io](http://eventuate.io) 是他的最新创业项目,一个微服务应用和数据服务平台。 Chris 定期为企业提供微服务设计培训和实战项目的架构咨询服务。近年来 Chris 多次访问中国,为包括华为、SAPă€ćƒ ć™Žă€ä¸œéŁŽćą˝č˝Śç­‰ĺ¤§ĺž‹äźä¸šćäž›ĺžŽćœĺŠĄćžśćž„ç›¸ĺ…łçš„ćŠ€ćœŻĺ’¨čŻ˘ćœĺŠĄă€‚ĺŚ‚ć‚¨ĺ¸Œćœ›ä¸Ž Chris 深入交流,建立合作,请点击下方按钮跟他取得联系。 [预约课程](http://www.chrisrichardson.net/contactus.html) #### ĺžŽćœĺŠĄĺş”ç”¨çš„ç¤şäž‹äťŁç  为了避免纸上谈兵,Chris ćäž›äş†ä¸€ĺĽ—ä¸Žčż™äş›ć¨Ąĺźç›¸ĺ…łçš„ç¤şäž‹äťŁç ă€‚čż™çť„äťŁç ä˝żç”¨[eventuate](http://eventuate.io)框架,实现了微服务架构下分布式数据的存取。请点击下方按钮访问。 [čŽżé—ŽäťŁç ](http://eventuate.io/exampleapps.html) --- ### 模式库 ć ¸ĺżƒć¨Ąĺź - [单体架构](/patterns/cn/monolithic.html) - [微服务架构](/patterns/cn/microservices.html) 服务拆分 - [ć šćŽä¸šĺŠĄčƒ˝ĺŠ›ć‹†ĺˆ†](/patterns/cn/decomposition/decompose-by-business-capability.html) new - [ć šćŽé˘†ĺŸŸçš„ĺ­ĺŸŸć‹†ĺˆ†](/patterns/cn/decomposition/decompose-by-subdomain.html) new 部署模式 - [单主机上部署服务的多个实例](/patterns/cn/deployment/multiple-services-per-host.html) - [单主机上部署服务的单个实例](/patterns/cn/deployment/single-service-per-host.html) - [服务实例与虚拟机一一对应](/patterns/cn/deployment/service-per-vm.html) - [服务实例与容器一一对应](/patterns/cn/deployment/service-per-container.html) - [ć— ćœĺŠĄĺ™¨éƒ¨ç˝˛](/patterns/cn/deployment/serverless-deployment.html) - [服务部署平台](/patterns/cn/deployment/service-deployment-platform.html) new 需要关注的边界问题 - [微服务的基底](/patterns/cn/microservice-chassis.html) - [配置信息外部化](/patterns/cn/externalized-configuration.html) new 通讯模式 - [远程过程调用](/patterns/cn/communication-style/rpi.html) - [月息](/patterns/cn/communication-style/messaging.html) - [领域独用协议](/patterns/cn/communication-style/domain-specific.html) new - [API 网关](/patterns/cn/apigateway.html) - [服务前端的后端](/patterns/cn/apigateway.html) - [客户端服务发现](/patterns/cn/client-side-discovery.html) - [服务器端服务发现](/patterns/cn/server-side-discovery.html) - [服务注册表](/patterns/cn/service-registry.html) - [自注册](/patterns/cn/self-registration.html) - [第三方注册](/patterns/cn/3rd-party-registration.html) - [断路器](/patterns/cn/reliability/circuit-breaker.html) new 数据管理 - [每服务数据库](/patterns/cn/data/database-per-service.html) - [共享数据库](/patterns/cn/data/shared-database.html) - [事件驱动架构](/patterns/cn/data/event-driven-architecture.html) - [事件溯源](/patterns/cn/data/event-sourcing.html) - [CQRS](/patterns/cn/data/cqrs.html) - [事务日志跟踪](/patterns/cn/data/transaction-log-tailing.html) - [数据库触发器](/patterns/cn/data/database-triggers.html) - [应用程序事件](/patterns/cn/data/transactional-outbox.html) 安全模式 - [访问令牌](/patterns/cn/security/access-token.html) new 可测试性 - [服务组件测试](/patterns/cn/testing/service-component-test.html) new - [服务集成协议测试](/patterns/cn/testing/service-integration-contract-test.html) new 可观测性 - [应用日志](/patterns/cn/observability/application-logging.html) new - [ĺş”ç”¨ćŒ‡ć ‡](/patterns/cn/observability/application-metrics.html) new - [审计日志](/patterns/cn/observability/audit-logging.html) new - [分布式追踪](/patterns/cn/observability/distributed-tracing.html) new - [异常追踪](/patterns/cn/observability/exception-tracking.html) new - [健康检查](/patterns/cn/observability/health-check-api.html) new UI 模式 - [ćœĺŠĄĺ™¨çŤŻéĄľé˘ç˘Žç‰‡ĺŒ–ĺ…ƒç´ ćž„ĺťş](/patterns/cn/ui/server-side-page-fragment-composition.html) new - [厢戡獯 UI 构建](/patterns/cn/ui/client-side-ui-composition.html) new --- [订阅微服务邮件列表](http://visitor.r20.constantcontact.com/d.jsp?llr=ula8akwab&p=oi&m=1123470377332&sit=l6ktajjkb&f=15d9bba9-b33d-491f-b874-73a41bba8a76) --- 全新的微服务应用支撑平台,成功解决微服务架构下分布式数据管理的难题。 [了解更多](http://eventuate.io/) ĺŠ ĺ…Ľ [微服务架构的 Google 讨论组](https://groups.google.com/forum/#!forum/microservices)(需要翻墙) # Pattern: Shared database ## Context Let’s imagine you are developing an online store application using the [Microservice architecture pattern](/patterns/microservices.html). Most services need to persist data in some kind of database. For example, the `Order Service` stores information about orders and the `Customer Service` stores information about customers. ## Problem What’s the database architecture in a microservices application? ## Forces - Services must be loosely coupled so that they can be developed, deployed and scaled independently - Some business transactions must enforce invariants that span multiple services. For example, the `Place Order` use case must verify that a new Order will not exceed the customer’s credit limit. Other business transactions, must update data owned by multiple services. - Some business transactions need to query data that is owned by multiple services. For example, the `View Available Credit` use must query the Customer to find the `creditLimit` and Orders to calculate the total amount of the open orders. - Some queries must join data that is owned by multiple services. For example, finding customers in a particular region and their recent orders requires a join between customers and orders. - Databases must sometimes be replicated and sharded in order to scale. See the [Scale Cube](/articles/scalecube.html). - Different services have different data storage requirements. For some services, a relational database is the best choice. Other services might need a NoSQL database such as MongoDB, which is good at storing complex, unstructured data, or Neo4J, which is designed to efficiently store and query graph data. # Solution Use a (single) database that is shared by multiple services. Each service freely accesses data owned by other services using local ACID transactions. # Example The `OrderService` and `CustomerService` freely access each other’s tables. For example, the `OrderService` can use the following ACID transaction ensure that a new order will not violate the customer’s credit limit. ``` BEGIN TRANSACTION … SELECT ORDER_TOTAL FROM ORDERS WHERE CUSTOMER_ID = ? … SELECT CREDIT_LIMIT FROM CUSTOMERS WHERE CUSTOMER_ID = ? … INSERT INTO ORDERS … … COMMIT TRANSACTION ``` The database will guarantee that the credit limit will not be exceeded even when simultaneous transactions attempt to create orders for the same customer. # Resulting context The benefits of this pattern are: - A developer uses familiar and straightforward ACID transactions to enforce data consistency - A single database is simpler to operate The drawbacks of this pattern are: - Development time coupling - a developer working on, for example, the `OrderService` will need to coordinate schema changes with the developers of other services that access the same tables. This coupling and additional coordination will slow down development. - Runtime coupling - because all services access the same database they can potentially interfere with one another. For example, if long running `CustomerService` transaction holds a lock on the `ORDER` table then the `OrderService` will be blocked. - Single database might not satisfy the data storage and access requirements of all services. - [Database per Service](database-per-service.html) is an alternative approach --- Tweet [关注本站推特账号 @crichardson](https://twitter.com/crichardson) Š 2017 Chris Richardson 版权所有 • 保留一切权利 • 本站由 [Kong](https://konghq.com/) 提供支持.