[PR #6758] [MERGED] perf(sub): cache each inbound's settings without clients for subscription bodies #6441

Closed
opened 2026-11-02 04:08:48 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6758
Author: @amirim1
Created: 10/7/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: feat/sub-data-cache


📝 Commits (10+)

  • 178c6c8 feat(sub): cache per-subscription data reads for subscription bodies
  • 9c6e4b7 feat(sub): invalidate subscription snapshots on panel mutations
  • 81c4e1b Merge remote-tracking branch 'origin/main' into feat/sub-data-cache
  • 17e43a5 Merge remote-tracking branch 'origin/main' into feat/sub-data-cache
  • ac6e928 Merge origin/main into feat/sub-data-cache
  • d2e3ab0 sub: fix shadowed inbounds in getInboundsBySubId else branch; gofmt
  • 945731c Merge remote-tracking branch 'origin/main' into pr/6758
  • efb05f0 fix(sub): release the snapshot cache with the subscription server
  • fc87225 style(sub): fit the snapshot cache comments in two lines
  • b9246e5 refactor(sub): cache each inbound's settings without its clients, not each subscriber

📊 Changes

13 files changed (+469 additions, -13 deletions)

View changed files

📝 go.mod (+1 -1)
📝 internal/sub/controller.go (+10 -1)
📝 internal/sub/service.go (+57 -11)
➕ internal/sub/settings_cache.go (+103 -0)
➕ internal/sub/settings_cache_test.go (+244 -0)
📝 internal/sub/sub.go (+3 -0)
📝 internal/web/service/client_bulk.go (+1 -0)
📝 internal/web/service/client_inbound_apply.go (+4 -0)
📝 internal/web/service/inbound.go (+6 -0)
📝 internal/web/service/inbound_disable.go (+2 -0)
📝 internal/web/service/inbound_node.go (+3 -0)
📝 internal/web/service/inbound_settings_commit.go (+1 -0)
➕ internal/web/service/sub_data_invalidation.go (+34 -0)

📄 Description

Summary

Subscription bodies (/sub/, /json/, /clash/) no longer read and scan each inbound's whole settings blob on every fetch. Each inbound's settings, minus its clients array, are cached in memory, keyed by inbound and shared by every subscriber of that inbound.

Why

On a large inbound the settings blob holds every client: about 25 MB at 100k clients. A profile of one subscription fetch at that size put about 57% of the time in reading that column and about 27% in scanning its clients array. Subscription bodies never use that array; they resolve their clients from the clients table (GetClientsBySubId). So every subscriber of the inbound paid for the whole blob on every poll.

Design

  • Keyed by inbound, not by subscriber. An entry is the inbound's top-level settings only (tens of bytes), so memory stays flat however many subscribers poll, and every subscriber of an inbound hits the same entry.
  • Only settings are cached. The inbound row (port, listen, stream settings, enable, remark, ...) is read on every fetch with the settings column omitted. The subscriber's clients, traffic stats and hosts are read fresh, as before.
  • WireGuard and AmneziaWG are not cached. They read their full settings as before, because their links need the peers.
  • Invalidation. Panel edits purge the cache through the invalidator registry (internal/web/service/sub_data_invalidation.go), which is hooked into the inbound and client mutations. A 10 s TTL bounds anything that bypasses those services. A load that overlaps a purge is served once but not kept.
  • Scope. Only the subscription server enables the cache (NewSUBController), and Server.Stop releases its invalidator. Panel link and QR rendering read fresh and register nothing.
  • Single-flight. Concurrent misses for one inbound share a single load.

Benchmark

The internal/sub scale dataset: a 3-client subscriber inside one inbound holding N clients. Each figure is the average of 20 fetches, all on the same machine.

main cache miss cache hit 30 distinct subscribers: main → cached
SQLite, 10k 9.2 ms 6.9 ms 1.2 ms 9.1 → 1.1 ms
SQLite, 100k 72.5 ms 55 ms 2.1 ms 72.7 → 3.6 ms
PostgreSQL 16, 10k 49.3 ms 50.4 ms 24.9 ms 48 → 20.4 ms
PostgreSQL 16, 100k 332 ms 330 ms 20.7 ms 320 → 31 ms

For this dataset the cache holds one 21-byte string per inbound.

An earlier revision of this PR cached each subscriber's full inbound rows, and this design replaces it. At 100k clients one of its entries was 25 MB, a hit was slower than no cache on SQLite (110 ms vs 72 ms), and 30 subscribers within the TTL held about 840 MB.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other (performance)

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

internal/sub/settings_cache_test.go covers the following:

  • Parity. Raw, JSON and Clash output is byte-identical with and without the cache, on a miss and on a hit. The subscriber has three inbounds:
    • VLESS;
    • Shadowsocks 2022, whose server key lives in settings;
    • WireGuard, whose peers live in settings.
  • Fresh inbound row. A port change and a disabled inbound reach the next fetch, while a settings change waits for InvalidateSubData.
  • Cache mechanics. Hit, expiry and purge; single-flight; a load that overlaps a purge is not kept.
  • Hosts. Host rows are read fresh on a cache hit.
  • Registry. A released invalidator stops running.

Each of these tests fails when the behavior it guards is removed.

go test ./internal/sub/ -race, the internal/web/service suite and golangci-lint all pass.

Screenshots / recordings

N/A (no UI changes).

Breaking changes

None. A settings change made by a path that bypasses the panel's services can reach subscription bodies up to 10 s late. Ports, enable state, clients and traffic are never cached.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6758 **Author:** [@amirim1](https://github.com/amirim1) **Created:** 10/7/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/sub-data-cache` --- ### 📝 Commits (10+) - [`178c6c8`](https://github.com/MHSanaei/3x-ui/commit/178c6c8fbdcbbbe8e5dbbc33aec8ec904fd8f1eb) feat(sub): cache per-subscription data reads for subscription bodies - [`9c6e4b7`](https://github.com/MHSanaei/3x-ui/commit/9c6e4b7a37a97bcc63297926a21cc9f61be5e8e6) feat(sub): invalidate subscription snapshots on panel mutations - [`81c4e1b`](https://github.com/MHSanaei/3x-ui/commit/81c4e1bae20b31ed7e3b48f5589f27475fed94ae) Merge remote-tracking branch 'origin/main' into feat/sub-data-cache - [`17e43a5`](https://github.com/MHSanaei/3x-ui/commit/17e43a55394c5690013ebe941210b231c76425f5) Merge remote-tracking branch 'origin/main' into feat/sub-data-cache - [`ac6e928`](https://github.com/MHSanaei/3x-ui/commit/ac6e928dc17d327acb8b8a4d11117019e251c31e) Merge origin/main into feat/sub-data-cache - [`d2e3ab0`](https://github.com/MHSanaei/3x-ui/commit/d2e3ab04b0edb13341e65dc8beb86beefe6bb9cb) sub: fix shadowed inbounds in getInboundsBySubId else branch; gofmt - [`945731c`](https://github.com/MHSanaei/3x-ui/commit/945731caf5a00ae118acae9d739f96009e53ddad) Merge remote-tracking branch 'origin/main' into pr/6758 - [`efb05f0`](https://github.com/MHSanaei/3x-ui/commit/efb05f0e8ed4fe922f2b0368e10f90409b3037b6) fix(sub): release the snapshot cache with the subscription server - [`fc87225`](https://github.com/MHSanaei/3x-ui/commit/fc87225dfe19f035d5bc29a19568cac4380b135b) style(sub): fit the snapshot cache comments in two lines - [`b9246e5`](https://github.com/MHSanaei/3x-ui/commit/b9246e5b34bcae385eefcc2b9c9ecbf73b910f73) refactor(sub): cache each inbound's settings without its clients, not each subscriber ### 📊 Changes **13 files changed** (+469 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `go.mod` (+1 -1) 📝 `internal/sub/controller.go` (+10 -1) 📝 `internal/sub/service.go` (+57 -11) ➕ `internal/sub/settings_cache.go` (+103 -0) ➕ `internal/sub/settings_cache_test.go` (+244 -0) 📝 `internal/sub/sub.go` (+3 -0) 📝 `internal/web/service/client_bulk.go` (+1 -0) 📝 `internal/web/service/client_inbound_apply.go` (+4 -0) 📝 `internal/web/service/inbound.go` (+6 -0) 📝 `internal/web/service/inbound_disable.go` (+2 -0) 📝 `internal/web/service/inbound_node.go` (+3 -0) 📝 `internal/web/service/inbound_settings_commit.go` (+1 -0) ➕ `internal/web/service/sub_data_invalidation.go` (+34 -0) </details> ### 📄 Description ## Summary Subscription bodies (`/sub/`, `/json/`, `/clash/`) no longer read and scan each inbound's whole settings blob on every fetch. Each inbound's settings, minus its `clients` array, are cached in memory, keyed by inbound and shared by every subscriber of that inbound. ## Why On a large inbound the settings blob holds every client: about 25 MB at 100k clients. A profile of one subscription fetch at that size put about 57% of the time in reading that column and about 27% in scanning its `clients` array. Subscription bodies never use that array; they resolve their clients from the `clients` table (`GetClientsBySubId`). So every subscriber of the inbound paid for the whole blob on every poll. ## Design - **Keyed by inbound, not by subscriber.** An entry is the inbound's top-level settings only (tens of bytes), so memory stays flat however many subscribers poll, and every subscriber of an inbound hits the same entry. - **Only settings are cached.** The inbound row (port, listen, stream settings, enable, remark, ...) is read on every fetch with the `settings` column omitted. The subscriber's clients, traffic stats and hosts are read fresh, as before. - **WireGuard and AmneziaWG are not cached.** They read their full settings as before, because their links need the peers. - **Invalidation.** Panel edits purge the cache through the invalidator registry (`internal/web/service/sub_data_invalidation.go`), which is hooked into the inbound and client mutations. A 10 s TTL bounds anything that bypasses those services. A load that overlaps a purge is served once but not kept. - **Scope.** Only the subscription server enables the cache (`NewSUBController`), and `Server.Stop` releases its invalidator. Panel link and QR rendering read fresh and register nothing. - **Single-flight.** Concurrent misses for one inbound share a single load. ## Benchmark The `internal/sub` scale dataset: a 3-client subscriber inside one inbound holding N clients. Each figure is the average of 20 fetches, all on the same machine. | | main | cache miss | cache hit | 30 distinct subscribers: main → cached | |---|---|---|---|---| | SQLite, 10k | 9.2 ms | 6.9 ms | 1.2 ms | 9.1 → 1.1 ms | | SQLite, 100k | 72.5 ms | 55 ms | 2.1 ms | 72.7 → 3.6 ms | | PostgreSQL 16, 10k | 49.3 ms | 50.4 ms | 24.9 ms | 48 → 20.4 ms | | PostgreSQL 16, 100k | 332 ms | 330 ms | 20.7 ms | 320 → 31 ms | For this dataset the cache holds one 21-byte string per inbound. An earlier revision of this PR cached each subscriber's full inbound rows, and this design replaces it. At 100k clients one of its entries was 25 MB, a hit was slower than no cache on SQLite (110 ms vs 72 ms), and 30 subscribers within the TTL held about 840 MB. ## Type of change - [ ] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [x] Other (performance) ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [x] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? `internal/sub/settings_cache_test.go` covers the following: - **Parity.** Raw, JSON and Clash output is byte-identical with and without the cache, on a miss and on a hit. The subscriber has three inbounds: - VLESS; - Shadowsocks 2022, whose server key lives in settings; - WireGuard, whose peers live in settings. - **Fresh inbound row.** A port change and a disabled inbound reach the next fetch, while a settings change waits for `InvalidateSubData`. - **Cache mechanics.** Hit, expiry and purge; single-flight; a load that overlaps a purge is not kept. - **Hosts.** Host rows are read fresh on a cache hit. - **Registry.** A released invalidator stops running. Each of these tests fails when the behavior it guards is removed. `go test ./internal/sub/ -race`, the `internal/web/service` suite and `golangci-lint` all pass. ## Screenshots / recordings N/A (no UI changes). ## Breaking changes None. A settings change made by a path that bypasses the panel's services can reach subscription bodies up to 10 s late. Ports, enable state, clients and traffic are never cached. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:48 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6441
No description provided.