medplum / medplum

Medplum is a healthcare platform that helps you quickly develop high-quality compliant applications.
https://medplum.com
Apache License 2.0
1.33k stars 365 forks source link

Race condition in createResourceIfNoneExist #4878

Open rahul1 opened 1 month ago

rahul1 commented 1 month ago

User Driven:

Background

Our current implementation of createResourceIfNoneExist performs a search and then writes a resource.

In rare cases, this can cause two client side jobs running in parallel to create duplicate resources

Proposal

make the createResourceIfNoneExist SDK function wrap a 1-element transaction bundle instead.

codyebberson commented 1 month ago

Even better, we should implement the trial-maturity "Conditional Create": https://www.hl7.org/fhir/http.html#ccreate

It would be about the same SQL behind the scenes, but does not have all of the baggage of transaction bundles.

rahul1 commented 1 month ago

@codyebberson agree that's the better way to do it