# Custom fields concurrency issue

**URL:** https://meta.discourse.org/t/custom-fields-concurrency-issue/364988
**Category:** Development
**Created:** [May 6, 2025, 12:11pm UTC](https://meta.discourse.org/t/custom-fields-concurrency-issue/364988 "2025-05-06T12:11:11Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [May 6, 2025, 12:11pm UTC](https://meta.discourse.org/t/custom-fields-concurrency-issue/364988/1 "2025-05-06T12:11:11Z")

</div>

Sometimes we run into concurrency issues with custom fields.

Example is a webhook handler that does this

```plaintext
user_id = params[:externalUserId].split('-')[1]
user = User.find_by(id: user_id)
raise Discourse::NotFound unless user

... do a lot of processing ...

user.custom_fields[:myfield] = params[:whatever]
user.save_custom_fields

```

When two webhooks fire in rapid succession for the same user, we sometimes end up with a duplicate row. The next time the user custom fields are accessed they look like  
`{"myfield"=>["value", "value"]}`

I see that `(user_id, name)` is defined as an index but not unique on `user_custom_fields`.

What would be the best pattern to prevent this from happening?

---

_[View the full topic](https://meta.discourse.org/t/custom-fields-concurrency-issue/364988)._
