用于存储用户会话的 Load Balancer cookie 粘性和 ElastiCache 之间有什么区别?

What is the difference between Load Balancer cookies stickiness and ElastiCache for storing user session?

我听说过两种在 Amazon AWS 中存储用户会话的方法。一种方法是使用 Load Balancer 的 cookie 粘性,另一种方法是将用户会话存储到 ElastiCache。如果我想同时使用 EC2 负载均衡器和 ElastiCache,各有什么优点和缺点?我应该在哪里存储用户会话?

AWS LB粘性是另外一回事,你不能在LB粘性中存储东西,这是由AWS底层服务控制的。负载均衡器使用一个特殊的 cookie 来跟踪每个侦听器的每个请求的实例。当负载均衡器收到请求时,它首先检查请求中是否存在此 cookie。如果是,则将请求发送到 cookie 中指定的实例。如果没有cookie,负载均衡器根据现有的负载均衡算法选择一个实例。

you can use the sticky session feature (also known as session affinity), which enables the load balancer to bind a user's session to a specific instance. This ensures that all requests from the user during the session are sent to the same instance.

LB 粘性会话只是路由来自同一用户的相同 ec2 实例的后续请求,这将有助于像 WebSocket 这样的应用程序。

lb-sticky-sessions

因此,如果您正在寻找一种方法来管理和存储敏感数据,并且该数据应该可以跨多个节点使用,那么您需要 Distributed Session Management 使用 Redis 或 Memcached。如果您的用例只是将后续请求粘贴到同一个 EC2 实例,那么 LB 粘性就足够了。

There are many ways of managing user sessions in web applications, ranging from cookies-only to distributed key/value databases, including server-local caching. Storing session data in the web server responding to a given request may seem convenient, as accessing the data incurs no network latency. The main drawback is that requests have to be routed carefully so that each user interacts with one server and one server only. Another drawback is that once a server goes down, all the session data is gone as well. A distributed, in-memory key/value database can solve both issues by paying the small price of a tiny network latency. Storing all the session data in cookies is good enough most of the time; if you plan to store sensitive data, then using server-side sessions is preferable.

building-fast-session-caching-with-amazon-elasticache-for-redis