简体   繁体   English

无状态与有状态微服务

[英]Stateless vs Stateful Microservices

I am reading some information about stateless microservices.我正在阅读一些关于无状态微服务的信息。 And my question is simple.我的问题很简单。 Is it true that if a microservice is having some persistence storage - that makes it stateful microservice.如果一个微服务有一些持久性存储,这是否真的是有状态的微服务。 Is that always true?总是这样吗? Any opinions will be well appreciated.任何意见将不胜感激。

Statefullness and statelessness are not generally about DB persistence.有状态和无状态通常与数据库持久性无关。 Also, there are different levels of state(full)(less)ness, I will try to list some examples:此外,有不同级别的状态(完整)(更少),我将尝试列出一些示例:

We could say a microservice is stateless if it does not hold information in its internal storage that is critical to serving clients, instead it holds data in external stores (which can be stateful).如果微服务在其内部存储中不保存对服务客户端至关重要的信息,而是在外部存储中保存数据(可以是有状态的),我们可以说微服务是无状态的。 A good thought experiment is to imagine that your service restarts on a different node between each and every request.一个好的思想实验是想象您的服务在每个请求之间在不同的节点上重新启动。 If the service can fulfil its purpose this way, it can be usually considered stateless.如果服务可以通过这种方式实现其目的,通常可以认为它是无状态的。 Another example is that load-balancers can randomly balance requests without using sticky sessions for stateless services.另一个例子是负载平衡器可以随机平衡请求,而无需为无状态服务使用粘性会话。 That's said if your service persists data in a local store (filesystem, etc...), then restarts in another node, and this data was critical for well-functioning, then it is not stateless.也就是说,如果您的服务将数据保存在本地存储(文件系统等)中,然后在另一个节点中重新启动,并且这些数据对于正常运行至关重要,那么它就不是无状态的。 So statelessness is not strictly constrained to not holding data in memory.因此,无状态并不严格限制为不在 memory 中保存数据。

Stateful service can be anything that holds a state between client accesses, and given this state is destroyed, some requests will fail.有状态服务可以是在客户端访问之间持有 state 的任何东西,并且鉴于此 state 被破坏,一些请求将失败。

Things get complicated with applications that are stateful internally but externally have a stateless API.内部有状态但外部有无状态 API 的应用程序会使事情变得复杂。 For example, an actor-based system can be termed stateful (services know about each other), but automatic failover (actors from dead nodes are migrated to live nodes) can guarantee reliability if paired with persistent actor state storage.例如,基于参与者的系统可以称为有状态的(服务相互了解),但如果与持久参与者 state 存储配对,自动故障转移(从死节点迁移到活动节点)可以保证可靠性。 As you see, the overall service is stateful, but interactions are stateless through the API.如您所见,整体服务是有状态的,但通过 API 交互是无状态的。

What is more important than these buzzwords, is how your application behaves in some edge conditions:比这些流行语更重要的是您的应用程序在某些边缘条件下的行为方式:

  • Handling existing processes/sessions/requests during scaling (adding nodes)在扩展期间处理现有进程/会话/请求(添加节点)
  • Handling the unexpected restart or termination of a service or node处理服务或节点的意外重启或终止
  • Handling requests during network partitions在网络分区期间处理请求
  • and more like these...还有更多像这些......

If your service can remain consistent and performant during these conditions, you are all good.如果您的服务在这些情况下能够保持一致和高效,那么您就一切都好。

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

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