Skip to content

Backend/1764 predictions idempotency rate limit - #1782

Merged
Olowodarey merged 12 commits into
Arena1X:mainfrom
patience-dabo:backend/1764-predictions-idempotency-rate-limit
Aug 30, 2026
Merged

Backend/1764 predictions idempotency rate limit#1782
Olowodarey merged 12 commits into
Arena1X:mainfrom
patience-dabo:backend/1764-predictions-idempotency-rate-limit

Conversation

@patience-dabo

Copy link
Copy Markdown
Contributor

Backend: Predictions Duplicate-Submission Guard + Per-User Rate Limiting

Summary

Implements issue #1764 by adding client-supplied idempotency keys and per-user rate limiting to prediction submissions, preventing duplicate submissions and protecting against rapid-fire client spam.

Changes

Core Features

  • Idempotency Support: Added clientIdempotencyKey (UUID4) to SubmitPredictionDto, SubmitBatchPredictionsDto, and BatchPredictionItemDto. Repeated submissions with the same key return the existing prediction instead of creating duplicates.
  • Unique Constraint: Added clientIdempotencyKey field to Prediction entity with unique index for idempotency tracking.
  • Per-User Rate Limiting: Implemented PredictionsRateLimitGuard with configurable limits (default: 30 submissions per 60 seconds), returning HTTP 429 with rate-limit headers.
  • Batch Support: Updated submitBatch() to validate idempotency keys within batch, detect existing keys, and prevent duplicates across batch items.

Files Modified

  • backend/src/predictions/dto/submit-prediction.dto.ts - Added clientIdempotencyKey with UUID4 validation
  • backend/src/predictions/dto/submit-batch-prediction.dto.ts - Added clientIdempotencyKey to batch DTO
  • backend/src/predictions/entities/prediction.entity.ts - Added clientIdempotencyKey field with unique constraint
  • backend/src/common/guards/predictions-rate-limit.guard.ts - New guard for per-user rate limiting
  • backend/src/predictions/predictions.controller.ts - Applied rate-limit guard to submit/submitBatch endpoints
  • backend/src/predictions/predictions.service.ts - Updated submit/submitBatch to return existing predictions on idempotent key reuse
  • backend/src/predictions/predictions.service.spec.ts - Added tests for idempotency key validation and duplicate handling
  • backend/src/common/guards/predictions-rate-limit.guard.spec.ts - Comprehensive tests for rate limiting

Testing

  • Unit tests for idempotency key validation and duplicate handling
  • Unit tests for rate-limit enforcement and 429 responses
  • Tests cover edge cases: invalid tokens, storage errors, expired keys
  • Configuration-driven limits via environment variables

Configuration

  • PREDICTIONS_RATE_LIMIT: Max submissions per window (default: 30)
  • PREDICTIONS_RATE_LIMIT_WINDOW_MS: Time window in milliseconds (default: 60000)

Closes #1764

@vercel

vercel Bot commented Aug 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
insight-arena-4rll Ready Ready Preview Aug 29, 2026 9:30pm

…and fix UUID generation

- Added clientIdempotencyKey to all batch prediction items in tests
- Fixed makeIdempotencyKey function to generate valid UUIDs
- Added explicit mock resets for predictions repo find calls in each test
…tests

- Add setHeader method to mock response object in beforeEach to prevent TypeError
- Consolidate mock response setup to avoid duplication
- Both rate-limit guard tests now pass (blocks 429 and error message validation)
- All 1503 tests passing
…y and rate limiting

- Use .overrideGuard() for cleaner PredictionsRateLimitGuard mocking in controller tests
- Remove guard provider injection to avoid ThrottlerStorage dependency resolution
- Update duplicate prediction test to account for idempotency key check
- Add clientIdempotencyKey to test data in service tests
- Clarify test description for duplicate prediction handling
@Olowodarey
Olowodarey merged commit a1ac692 into Arena1X:main Aug 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] predictions: Duplicate-submission guard + per-user rate limiting

2 participants