Skip to content

Batch per-variant writes in AbTestingService to remove N+1 database writes #1127

Description

@RUKAYAT-CODER

Overview

AbTestingService iterates experiment.variants and issues a separate save() inside the loop (around src/ab-testing/ab-testing.service.ts:266 and :361), producing one write round-trip per variant. For an experiment with many variants this is a classic N+1 write pattern that multiplies latency and transaction overhead, and because the writes are not wrapped in a transaction a partial failure leaves the variant set inconsistent.

Specifications

Tasks:

  • Collect the mutated variant entities and persist them with a single bulk save(array) / insert call.
  • Wrap the bulk write in a transaction so all variants commit or none do.
  • Audit statistical-analysis.service.ts for the same per-variant loop pattern.
  • Add a test asserting the number of write queries is constant regardless of variant count.

Impacted Files:

  • src/ab-testing/ab-testing.service.ts
  • src/ab-testing/analysis/statistical-analysis.service.ts

Acceptance Criteria

  • Saving N variants issues one bulk write, not N.
  • The variant update is atomic.
  • A test pins the query count.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions