Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

one rtt to db on project and org

Thiago Araujo da Silva17451c9f6b18
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
index 13ae7dfa13..27fc8b1357 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
@@ -160,14 +160,16 @@ export const auditLogStreamOutboxServiceFactory = ({
// Dedup streams touched this batch so each is debounced/woken exactly once.
const streamsToFlush = new Map<string, { orgId: string; provider: LogProvider }>();
- for (const [orgId, logs] of logsByOrg) {
- // eslint-disable-next-line no-await-in-loop
- const streams = await auditLogStreamDAL.find({ orgId });
- for (const stream of streams) {
+ // One lookup for every org in the batch instead of a query per org — the common case is
+ // that most orgs have no streams configured, so a per-org loop is mostly empty round-trips.
+ const streams = await auditLogStreamDAL.find({ $in: { orgId: [...logsByOrg.keys()] } });
+ for (const stream of streams) {
+ const logs = logsByOrg.get(stream.orgId);
+ if (logs) {
for (const log of logs) {
- outboxRows.push({ streamId: stream.id, orgId, payload: log });
+ outboxRows.push({ streamId: stream.id, orgId: stream.orgId, payload: log });
}
- streamsToFlush.set(stream.id, { orgId, provider: stream.provider as LogProvider });
+ streamsToFlush.set(stream.id, { orgId: stream.orgId, provider: stream.provider as LogProvider });
}
}
diff --git a/backend/src/ee/services/audit-log/audit-log-queue.ts b/backend/src/ee/services/audit-log/audit-log-queue.ts
index 0753f13459..18ca9ef033 100644
--- a/backend/src/ee/services/audit-log/audit-log-queue.ts
+++ b/backend/src/ee/services/audit-log/audit-log-queue.ts
@@ -18,7 +18,7 @@ type TAuditLogQueueServiceFactoryDep = {
auditLogDAL: TAuditLogDALFactory;
auditLogStreamOutboxService: Pick<TAuditLogStreamOutboxServiceFactory, "enqueueForLogs">;
queueService: TQueueServiceFactory;
- projectDAL: Pick<TProjectDALFactory, "findById">;
+ projectDAL: Pick<TProjectDALFactory, "find">;
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
clickhouseClient: ClickHouseClient | null;
keyStore: Pick<TKeyStoreFactory, "streamAdd" | "streamCollect" | "streamTrim" | "acquireLock">;
@@ -152,9 +152,11 @@ export const auditLogQueueServiceFactory = async ({
const distinctProjectIds = [
...new Set(parsed.filter((entry) => !entry.orgId && entry.projectId).map((entry) => entry.projectId as string))
];
- for (const projectId of distinctProjectIds) {
- // eslint-disable-next-line no-await-in-loop
- projectCache.set(projectId, await projectDAL.findById(projectId));
+ if (distinctProjectIds.length > 0) {
+ const projects = await projectDAL.find({ $in: { id: distinctProjectIds } });
+ for (const project of projects) {
+ projectCache.set(project.id, project);
+ }
}
const resolveOrgId = (entry: TAuditLogStreamEntry): string | undefined =>