API应该将所有工作委派给其他服务吗?

发布于 2025-01-25 21:20:24 字数 222 浏览 1 评论 0原文

假设我们有一个具有多个端点的API服务器,可为用户请求服务。我一直想知道在API服务器中实施逻辑与委派其他微服务之间的逻辑是什么。

例如,假设我们想在API调用时从数据库中获取数据。应该查询数据库:

  1. 由API本身执行吗?
  2. 委派给处理数据库查询的单独的微服务?
  3. 委托书比微服务更简单,例如云中的lambda功能?

感谢您的帮助。

Suppose we have an API server with several endpoints that serves user requests. I have been wondering what is a good trade-off between implementing logic in the API server vs. delegating to other microservices.

For example, suppose we want to fetch data from a database upon an API call. Should database query:

  1. Be performed by the API itself?
  2. Delegated to a separate microservice that handles database queries?
  3. Delegated to something even simpler than a microservice, say a lambda function in the cloud?

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2025-02-01 21:20:24

如果是微服务,服务自己的数据。这意味着两件事:

  1. 此服务是唯一可以直接访问其数据的应用程序,
  2. 如果ServiceB希望对此数据执行任何操作,则必须通过Servicea的API(而不是直接通过数据库执行此操作),

如果ServiceB需要检索数据经常且数据相当静态,那么ServiceB可以通过复制实现本地缓存。但是真理的根源仍然留在Servicea。

In case of Microservices the service itself is the owner of its own data. This implies two things:

  1. This service is the only application which has direct access to its own data
  2. If ServiceB wants perform any operation on this data it has to do that via the ServiceA's API (not directly via the database)

If ServiceB needs to retrieve data frequently and the data is fairly static then the ServiceB could implement a local cache via replication. But the source of truth still remains at ServiceA.

随遇而安 2025-02-01 21:20:24

AFAIK。微服务水平分解了问题,您所说的是垂直分解问题。 OFC。可以将这两种方法结合起来,并具有多种微服务或其中的多个微服务,分为不同层的较小服务。这既可以是建筑又是一个规模的决定,因此最好检查数字您期望什么样的负载,需要什么响应时间以及您想在IT上花费多少。因此,最好不要解决不存在ATM的问题。但是也许将来您会拥有它们,或者没有...

Afaik. microservices split up the problem horizontally, and what you are talking about is splitting up the problem vertically. Ofc. it is possible to combine both approaches and having multiple microservices each or some of them split up to smaller services in different layers. This can be both an architectural and a scaling decision, so better to check the numbers what kind of load you expect, what response time you need, and how much you want to spend on it imho. So better not to solve problems which are non-existent atm. but maybe in the future you will have them, or not...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文