简体   繁体   English

无状态微服务和数据库

[英]Stateless Micro services and database

We have a requirement of building stateless micro services which rely on a database cluster to persist data. 我们需要构建无状态微服务,这些服务依赖于数据库集群来持久化数据。

What is the approach that is recommended for redundant stateless micro services(for high availability and scalability) using the database cluster. 使用数据库集群建议的冗余无状态微服务(用于高可用性和可伸缩性)的方法是什么。 For example: Running multiple copies of version 1.0 Payment service. 例如:运行1.0版支付服务的多个副本。

Should all the redundant micro services use a common shared DB schema or they should have their own schema? 是否所有冗余微服务都使用通用的共享数据库模式,或者它们应该有自己的模式? In case of independent DB schema inconsistency among the redundant services may exist. 在独立DB模式的情况下,可能存在冗余服务之间的不一致。

Also how can the schema upgrade handled in case of common DB schema? 在常见的数据库模式的情况下,如何处理模式升级?

This is a super broad topic, and rather hard to answer in general terms. 这是一个超级广泛的主题,而且一般来说很难回答。

However... 然而...

A key requirement for a micro service architecture is that each service should be independent from the others. 微服务架构的关键要求是每个服务应独立于其他服务。 You should be able to deploy, modify, improve, scale your micro service independently from the others. 您应该能够独立于其他服务部署,修改,改进,扩展您的微服务。

This means you do not want to share anything other than API definitions. 这意味着您不希望共享API定义以外的任何内容。 You certainly don't want to share a schema; 你当然不想共享架构; each service should be able to define its own schema, release new versions, change data types etc. without having to check with the other services. 每个服务都应该能够定义自己的模式,发布新版本,更改数据类型等,而无需检查其他服务。 That's almost impossible with a shared schema. 使用共享模式几乎是不可能的。

You may not want to share a physical server. 您可能不想共享物理服务器。 Sharing a server means you cannot make independent promises on scalability and up-time; 共享服务器意味着您无法在可伸缩性和正常运行时间方面做出独立承诺; a big part of the micro service approach means that the team that builds it is also responsible for running it. 微服务方法的很大一部分意味着构建它的团队也负责运行它。 You really want to avoid the "well, it worked in dev, so if it doesn't scale on production, it's the operations team's problem" attitude. 你真的想避免“好吧,它在开发中工作,所以如果它不能在生产上扩展,那就是运营团队的问题”的态度。 Databases - especially clustered, redundant databases - can be expensive, so you might compromise on this if you really need this. 数据库 - 尤其是集群冗余数据库 - 可能很昂贵,因此如果您确实需要, 可能会对此有所妥协。

As most microservice solutions use containerization and cloud hosting, it's quite unlikely that you'd have the "one database server to rule them all" sitting around. 由于大多数微服务解决方案都使用容器化和云托管,因此您不太可能拥有“一个数据库服务器来统治它们”。 You may find it much better to have each micro service run its own persistence service, rather than sharing. 您可能会发现让每个微服务运行自己的持久性服务而不是共享更好。

The common approach to dealing with inconsistencies is to accept them - but to use CQRS to distribute data between microservices, and make sure the micro services deal with their internal consistency requirements. 处理不一致的常用方法是接受它们 - 但是使用CQRS在微服务之间分配数据,并确保微服务处理其内部一致性要求。

This also deals with the "should I upgrade my database when I release a new version?" 这也涉及“我在发布新版本时应该升级数据库吗?” question. 题。 If your observers understand the version for each message, they can make decisions on how to store them. 如果您的观察者了解每条消息的版本,他们可以决定如何存储它们。 For instance, if version 1.0 uses a different set of attributes to version 1.1, the listener can do the mapping. 例如,如果版本1.0使用1.1版的不同属性集,则侦听器可以执行映射。

In the comments, you ask about consistency. 在评论中,您询问一致性。 This is a super complex topic - especially in micro service architectures. 这是一个非常复杂的主题 - 尤其是在微服务架构中。

If you have, for instance, a "customers" service and an "orders" service, you must make sure that all orders have a valid customer. 例如,如果您有“客户”服务和“订单”服务,则必须确保所有订单都有有效客户。 In a monolithic application, with a single database, and exclusively synchronous interactions, that's easy to enforce at the database level. 在单个应用程序中,使用单个数据库和专门的同步交互,这很容易在数据库级别强制执行。

In a micro service architecture, where you might have lots of data stores, with no dependencies on each other, and a combination of synchronous and asynchronous calls, it's really hard. 在微服务架构中,您可能拥有大量数据存储,彼此之间没有依赖关系,以及同步和异步调用的组合,这真的很难。 This is an inevitable side effect of reducing dependencies between micro services. 这是减少微服务之间依赖性的不可避免的副作用。

The most common approach is " eventual consistency ". 最常见的方法是“ 最终一致性 ”。 This typically requires a slightly different application design. 这通常需要稍微不同的应用程序设计。 For instance, on the "orders" screen, you would invoke first the client microservice (to get client data), and then the "orders" service (to get order details), rather than have a single (large) service call to retrieve everything. 例如,在“订单”屏幕上,您将首先调用客户端微服务(以获取客户端数据),然后调用“订单”服务(以获取订单详细信息),而不是单个(大)服务调用来检索一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM