Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): prevent duplicate ingestion of events #1331

Merged
merged 5 commits into from
Oct 12, 2024
Merged

Conversation

detj
Copy link
Contributor

@detj detj commented Oct 12, 2024

Summary

Double or multiple ingestion may occur when multiple event requests with the same id appears at the same time. This leads to multiple duplicate events and hence unexpected downstream results.

Revamp the ingestion pipeline to catch conditions for double/multiple ingestion and handle them gracefully.

Tasks

  • Add postgres migration to add status column in event_reqs table
  • Prevent duplicate ingestion of events by keeping track of a status column in event_reqs table
  • Send Retry-After response header if another identical request is being processed
  • Rollback gracefully to cleanup event requests in pending state if request fails at any point
  • Use defer to end all OTel spans in ingestion
  • 📚 Update SDK API docs

Reproduction

  1. Add artificial delay of 2 seconds during ingestion
  2. Ingest using sessionator with --clean flag
  3. Ingest using sessionator in another window at the same time
  4. Multiple duplicate events get ingested or some requests fail with 500 Internal Server Error

Proposed Fix

  1. At ingestion start first check if another request with the same id is in progress
  2. If yes, reply with a 429 Too Many Requests status along with a Retry-After: 60 response header
  3. If no, persist event request to db with status as pending
    • If a primary key violation occurs, this means another request was started in between, reply with a 429 Too Many Requests status along with a Retry-After: 60 response header
  4. Start a db transaction
  5. Ingest events as usual
  6. Commit the transaction with the status of event request in db as done
  7. If step 6 fails, rollback by removing the pending event request from step 3

Architecture

Before

---
title: Double Ingestion
---
flowchart TB
    req1[req1, id: 100]
    req2[req2, id: 100]
    req1d{ req1 committed? }
    req2d{ req2 committed? }
    ingest(((double ingestion occurs)))
    res[Send response]

    req1 -->|occurs at same time| req1d
    req2 -->|occurs at same time| req2d
    req1d -->|No| ingest
    req2d -->|No| ingest
    ingest -->|202 Accepted| res
Loading

After

---
title: No Double Ingestion
---
flowchart TB
    req1[req1, id: 100]
    req2[req2, id: 100]
    req1d{ req1 committed? }
    req2d{ req2 committed? }
    ingest[ingest events]
    check{identical req in progress?}
    res[Send response]
    retry[retry after sometime]

    req1 -->|occurs at same time| req1d
    req2 -->|occurs at same time| req2d
    req1d -->|No| check
    req2d -->|No| check
    check -->|Yes| retry
    check -->|No| ingest
    ingest -->|202 Accepted| res
    retry -->|429 Too Many Requests| res
Loading

See also

- add postgres migration to add `status` column in `event_reqs` table
- prevent duplicate ingestion of events by keeping track of a `status`
column in `event_reqs` table
- send `retry-after` response header if another identical request is
being processed
- rollback gracefully to cleanup event requests in `pending` state if
request fails at any point
- use defer to end all otel spans in ingestion

fixes #1277

Signed-off-by: detj <[email protected]>
@detj detj added bug something isn't working backend backend related labels Oct 12, 2024
@detj detj self-assigned this Oct 12, 2024
Copy link

vercel bot commented Oct 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
measure-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 12, 2024 9:53am

- update put `/events` endpoint to add information about retry after
response header
- add additional response body example
- fix markdown formatting

Signed-off-by: detj <[email protected]>
- change response status code for in-progress duplicate event request to
`429 Too Many Requests`

Signed-off-by: detj <[email protected]>
@detj detj merged commit 885053f into main Oct 12, 2024
8 checks passed
@detj detj deleted the duplicate-ingestion branch October 12, 2024 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend backend related bug something isn't working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Prevent duplicate ingestion on simultaneous identical event requests
2 participants