Files
twenty/packages/twenty-server/src/engine/core-modules/application
Félix Malfait 71370d9e66 fix(server): dedupe in-flight application translation catalog loads (#22285)
## Problem

After v2.17.0 shipped app-owned metadata translations (#22235), `POST
/metadata` got slower for workspaces with translation-carrying apps
installed. Sentry flagged it as an N+1
(`performance_n_plus_one_db_queries`): a single request fires **many**
concurrent identical queries:

```sql
SELECT … FROM "core"."applicationTranslation" WHERE "applicationRegistrationId" = $1 AND "deletedAt" IS NULL
```

Span aggregates since the deploy: **272** such spans, **avg 74 ms**,
**p95 556 ms**, **max 694 ms**, all on `POST /metadata`. HTTP stays 200
— it's latency, not errors.

Reported via Sentry `TWENTY-SERVER-HQY` (`twenty-v7`).

## Root cause

`ApplicationTranslationCacheService` kept a TTL value cache but had **no
in-flight de-duplication**. The object/field metadata resolvers call
`getCatalog` directly, per record. On a cold/expired (30 s TTL) cache,
many fields of the same app resolve concurrently, all miss, and each
fires its own `repository.find` — a classic cache **stampede**, which
queues on the connection pool and produces the 556–694 ms tail. Each
read also pulls the full per-locale `messages` JSON, so the redundant
reads aren't free.

## Fix

Rebuild the service on the shared **`PromiseMemoizer`** primitive — the
same one `WorkspaceCacheService` and `CoreEntityCacheService` already
use. It pairs the 30 s TTL value cache with a `pending` promise map, so
concurrent callers for the same registration **share a single in-flight
query** instead of stampeding. Per-request query count for a given app
goes from N → 1.

- Public API (`getCatalog` / `invalidate`) is unchanged — no caller
touched.
- `invalidate` now clears via `memoizer.clearKeys(...)` (clears both the
cached value and any in-flight read), matching `WorkspaceCacheService`.
- Adds a unit test asserting 10 concurrent `getCatalog` calls trigger
exactly **one** `repository.find`, plus cache-hit / empty-locale /
post-invalidation reload cases.

## Notes

- Process-local 30 s TTL behaviour is unchanged (deliberate; no
cross-process invalidation), this only removes the redundant concurrent
reads.
- Verified formatting with oxfmt locally; couldn't run the server test
suite in this environment, so relying on CI for typecheck/test.

https://claude.ai/code/session_01NiE7o3cd3zCLZarVkJa6UA

---
_Generated by [Claude
Code](https://claude.ai/code/session_01NiE7o3cd3zCLZarVkJa6UA)_

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/22285?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-06-29 10:08:40 +02:00
..