如何通过负载均衡策略更好地利用本地缓存?

How to better utilize local cache with load balancing strategies?

我有一个身份验证服务,我需要缓存一些用户信息以获得更好的性能。我选择使用本地缓存,因为身份验证服务可能会在每个请求上调用,所以我希望它非常快。与远程缓存选项相比,本地缓存要快得多(本地缓存访问低于 1 毫秒,而远程缓存访问约为 25 毫秒)。

问题是如果 运行 内存不足,我无法缓存与分布式缓存一样多的信息(涉及数百万用户)。我可以保持原样,当本地缓存达到内存限制时,它会驱逐一些其他数据,但这对缓存来说是不好的优化。或者我可以使用某种负载平衡器策略,根据用户的 IP 地址或其他条件将用户重定向到相同的身份验证服务实例,因此缓存命中率会高很多。

这有点违背了拥有无状态服务的目的,但是我认为如果我想要一致性和可用性,我可以在网络层稍微妥协一下这个原则。至于身份验证,两者对于完全安全都至关重要(用户信息必须始终是最新的和可用的)。

有哪些负载均衡技术可以解决这类问题?可以有其他解决方案吗?

注意:尽管这个问题是特定于身份验证的,但我认为许多其他经常访问且需要速度的服务可以从使用本地缓存中受益匪浅。

所以 - 在这里回答这个问题 - 负载平衡器可以用他们的哈希算法处理这个问题。

我经常使用 Azure,所以我以 Azure 负载均衡器为例:

来自文档:

Hash-based distribution mode

The default distribution mode for Azure Load Balancer is a five-tuple hash.

The tuple is composed of the:

  • Source IP
  • Source port
  • Destination IP
  • Destination port
  • Protocol type

The hash is used to map traffic to the available servers. The algorithm provides stickiness only within a transport session. Packets that are in the same session are directed to the same datacenter IP behind the load-balanced endpoint. When the client starts a new session from the same source IP, the source port changes and causes the traffic to go to a different datacenter endpoint.