apache / apache/shenyu

[BUG] HashLoadBalancer rebuilds consistent-hash ring + allocates MD5 MessageDigest per virtual node per request

Open
#6,571 1 comment 0 reactions 0 assignees View on GitHub
plugin: divide priority: high type: performance
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

## Description
`doSelect` allocates a fresh `ConcurrentSkipListMap` and, for every upstream x 5 virtual nodes, calls `hash("SHENYU-" + upstream.getUrl() + "-HASH-" + i)` which does `MessageDigest.getInstance("MD5")` (JCA lookup + new instance), `key.getBytes(UTF_8)`, and `md5.digest()` (new `byte[]`). The ring is never cached; it is fully rebuilt on every selection even though the upstream set only changes on health-check/admin events.

## Location
```
shenyu-loadbalancer/.../spi/HashLoadBalancer.java:44-56 (doSelect), 58-77 (hash)
```

## Impact
Per hash-LB request: 1 SkipListMap + 5*N MD5 instances + 5*N String concatenations + 5*N `byte[]` digests. For N=10 upstreams that's 50 MD5 hashes/request. Very high CPU + allocation on the sticky/consistent-hash path; scales linearly with RPS and upstream count.

## Suggested fix
Build and cache the ring (e.g. `ConcurrentHashMap>` keyed by selectorId, invalidated when `UpstreamCacheManager.submit`/health-check mutates the list). Use a `ThreadLocal` or a faster non-crypto hash (murmur3/xxHash). Precompute virtual-node keys when the upstream list changes.

## Related existing issue(s)
None

_Identified during the 2026-08-02 audit; full list in [`docs/issue-candidates-2026-08-02.md`](docs/issue-candidates-2026-08-02.md)._

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with shenyu-loadbalancer/.../spi/HashLoadBalancer.java:44-77, reading doSelect and hash to measure the current ring and digest allocations. Trace UpstreamCacheManager.submit and health-check list mutations to determine invalidation points. Done means the ring and hash-related work are not rebuilt for every selection, while selection behavior remains consistent when upstreams change.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.