Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(core): add redis lock during insertion of event in event table during initial attempt of outgoing webhook delivery#7579

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
likhinbopanna merged 3 commits intomainfromeventsLocking
Apr 28, 2025

Conversation

dgeee13
Copy link
Contributor

@dgeee13dgeee13 commentedMar 20, 2025
edited
Loading

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

The current implementation of insertion of an event inevent table does not handle the case where multiple requests with the same idempotent_event_id might be processed concurrently, leading to multiple unique constraint violation errors in the database.

This was reported by a merchant who was facing high volumes of unique constraint violation errors.

NOTE: this unique constraint log error issue is only applicable for theinitial attempt of outgoing webhook delivery, not for retry attempts

Why does this happen?

  1. Multiple requests with the sameidempotent_event_id might reach the database at the same time.
  2. Each request checks for the event, finds nothing (since it’s not committed yet), and proceeds to insert.
  3. The first request succeeds, while the others fail due to the unique constraint on idempotent_event_id.

Solution (Using Redis Lock)
Before querying the database, acquire a lock in Redis for the givenidempotent_event_id.
If there are multiple processes with sameidempotent_event_id only one process will hold the lock, others will leave early.

If a lock is acquired:

  • Check if the event already exists in the database.
  • If it exists, return early
  • Otherwise, insert the event.
  • Once done, release the lock

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

How did you test it?

Cannot be directly tested.

Checklist

  • I formatted the codecargo +nightly fmt --all
  • I addressed lints thrown bycargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@dgeee13dgeee13 self-assigned thisMar 20, 2025
@dgeee13dgeee13 requested a review froma team as acode ownerMarch 20, 2025 07:28
@semanticdiff-comSemanticDiff.com
Copy link

semanticdiff-combot commentedMar 20, 2025
edited
Loading

Review changes with  SemanticDiff

Changed Files
FileStatus
  crates/router/src/core/webhooks/outgoing.rs  17% smaller
  config/config.example.tomlUnsupported file format
  config/deployments/integration_test.tomlUnsupported file format
  config/deployments/production.tomlUnsupported file format
  config/deployments/sandbox.tomlUnsupported file format
  config/development.tomlUnsupported file format
  config/docker_compose.tomlUnsupported file format
  crates/router/src/configs/settings.rs  0% smaller
  crates/router/src/configs/validations.rs  0% smaller
  crates/router/src/core/webhooks/utils.rs  0% smaller
  loadtest/config/development.tomlUnsupported file format

merchant_id.get_string_repr(),
unique_locking_key
);
let redis_lock_expiry_seconds = state.conf().lock_settings.redis_lock_expiry_seconds;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@jarnura Are we okay with using the same TTL as that used for API locking?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Have a separate locking expiry time

dgeee13 reacted with thumbs up emoji
@dgeee13dgeee13force-pushed theeventsLocking branch 4 times, most recently froma511363 to7d21415CompareApril 9, 2025 08:46
@dgeee13dgeee13 requested a review froma team as acode ownerApril 9, 2025 08:46
@dgeee13dgeee13force-pushed theeventsLocking branch 2 times, most recently fromd9aaea6 toe7774d9CompareApril 9, 2025 09:48
.attach_printable("The redis value which acquired the lock is not equal to the redis value requesting for releasing the lock")
}
}
Err(error) => Err(error).change_context(errors::ApiErrorResponse::InternalServerError),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please attach context messages usingattach_printable() /attach_printable_lazy() whenever raising internal server errors.

dgeee13 reacted with thumbs up emoji
@dgeee13dgeee13 requested review froma team ascode ownersApril 22, 2025 07:40
@dgeee13dgeee13 removed request fora teamApril 22, 2025 07:43
@dgeee13dgeee13 removed request fora teamApril 22, 2025 07:43
jarnura
jarnura previously approved these changesApr 23, 2025
Copy link
Member

@SanchithHegdeSanchithHegde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Other than that, looks good to me!

dgeee13 reacted with hooray emoji
@hyperswitch-bothyperswitch-botbot added M-database-changesMetadata: This PR involves database schema changes M-api-contract-changesMetadata: This PR involves API contract changes and removed M-database-changesMetadata: This PR involves database schema changes M-api-contract-changesMetadata: This PR involves API contract changes labelsApr 24, 2025
@likhinbopannalikhinbopanna added this pull request to themerge queueApr 28, 2025
Merged via the queue intomain with commiteed84feApr 28, 2025
20 of 24 checks passed
@likhinbopannalikhinbopanna deleted the eventsLocking branchApril 28, 2025 13:45
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jarnurajarnurajarnura approved these changes

@SanchithHegdeSanchithHegdeSanchithHegde approved these changes

Assignees

@dgeee13dgeee13

Labels
A-coreArea: Core flows
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@dgeee13@jarnura@SanchithHegde@likhinbopanna

[8]ページ先頭

©2009-2025 Movatter.jp