Files
twenty/packages/twenty-server
Weiko 3689e89440 Optimize upgrade status gauges with count-only queries (#23574)
## Context

Upgrade health metrics and the admin upgrade-status query currently
share `getInstanceAndAllWorkspacesStatus`.

On a cache hit, that method reads the cached behind/failed workspace
IDs, then hydrates every workspace name with an individual
`CoreEntityCacheService.get` call. This is useful for the admin
response, but the gauges only need the number of workspaces in each
state.

As the number of behind or failed workspaces grows, every gauge refresh
therefore creates a fan-out of entity-cache lookups. Those lookups can
include Redis validation and response deserialization. Production
profiling of slow upgrade-status requests showed
`loadWorkspaceNamesById` and `CoreEntityCacheService.get` on the hot
path, so this PR removes that unnecessary repeated work.

## What changed

- Added a count-only upgrade-status method for metric collection.
- Updated upgrade gauges to use cached ID counts without loading
workspace names.
- Replaced the admin path's per-workspace cache lookups with one
repository query selecting only `id` and `displayName`.
- Removed the upgrade module's now-unused core-entity-cache dependency.

## Why this improves performance

### Metrics path

Before:

- Read the cached upgrade-status IDs.
- Run one entity-cache lookup per behind/failed workspace.
- Discard the hydrated names and only use the array lengths.

After:

- Read the same cached upgrade-status IDs.
- Derive counts directly from those IDs.
- Perform no workspace-name lookup.

**This changes metric collection from a fixed set of status-cache calls
plus `N` entity-cache calls to only the fixed status-cache calls. The
amount of ID data still scales with the number of affected workspaces,
but the Redis/client round-trip fan-out does not.**

### Admin path

The admin response still needs workspace names. It now loads them with
one primary-key `IN` query instead of `N` independent entity-cache
calls. This reduces round trips and repeated cache validation while
preserving the response shape.

## Safety and behavior preservation

- Upgrade-status cache keys, TTLs and invalidation behavior are
unchanged.
- A missing cache marker still triggers the existing full status
refresh.
- Metrics names and values are unchanged.
- The admin GraphQL response is unchanged.
- Cached workspace IDs missing from the database still produce a `null`
name, matching the previous behavior.
- The batched query runs only for callers that request the detailed
admin payload, not for metric collection.

## Expected impact

- Remove recurring per-workspace cache fan-out from every API process
collecting upgrade gauges.
- Reduce Redis client work, response deserialization and event-loop
pressure during metric collection.
- Reduce latency for detailed admin upgrade-status requests.

This targets one profiled source of tail latency. It is not expected to
eliminate all API p99 outliers, which also have independent causes.

## Validation

- 36 focused upgrade-status and gauge tests pass.
- `yarn nx typecheck twenty-server` passes.
- Oxlint passes with zero warnings and errors.
- Oxfmt and `git diff --check` pass.
2026-07-30 14:38:48 +00:00
..