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 race condition with 2 stripe customers for 1 user #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alekseyrozh
Copy link

@alekseyrozh alekseyrozh commented Jan 30, 2025

In the originally provided code snippet for creating a stripe customer there's a race condition when you can create 2 customers for 1 user.

This is due to the fact that the check if the customer already exists is conducted on your server, which gets this data asynchronously. As a result your server might think that the customer does not exist, but in reality it was just created. Even if calling stripe api to determine this instead of relying on your own data will have this issue because you can get the response that "the customer does not exist", but while this response is coming to your server, the customer was already created.

There are only 2 solutions to this problem:

  1. (❌ doesn't exist) Have stripe provide you an atomic getOrCreateCustomer({ userId }) operation
  2. (✅ exists) Provide an idempotency key to the createCustomer operation. This is explained here - Prevent customer creation race conditions stripe/stripe-node#476 (comment)

@t3dotgg t3dotgg force-pushed the main branch 2 times, most recently from 93e3d2f to f525f50 Compare February 7, 2025 23:38
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.

1 participant