Entity Service API Atomic Update?

System Information
  • Strapi Version: 4.9.0
  • Operating System: Ubuntu 22.04
  • Database: Postgres
  • Node Version: v16.19.0
  • NPM Version: 8.19.3
  • Yarn Version: 1.22.19

I’m creating a custom api endpoint for my website users to update tags on video content. In my controller, I have the following code.

    const { tag, vod: vodId } = ctx.request.body.data;

    const vod = await strapi.entityService.findOne('api::vod.vod', vodId, {
      populate: { tags: true }

    const existingTags = vod.attributes.tags
    const updatedTags = vod.attributes.tags.concat([tag])

    await strapi.entityService.update('api::vod.vod', vodId, {
      data: {
        tags: updatedTags

This code gets the list of existing vod tags, add the new tag from the user, and updates the vod record with the new tags list.

I see a potential issue here. Because Strapi’s entity service is asyncronous, I think it is possible for updates from two or more users updating tags on the same vod at the same time to interfere with each other.

Here is the potential issue, illustrated.

  1. entityService.findOne for user1 retrieves tags: [‘cats’, ‘dogs’]
  2. entityService.findOne for user2 retrieves tags: [‘cats’, ‘dogs’]
  3. entityService.update for user 1 sets tags: [‘cats’, ‘dogs’, ‘turtles’]
  4. entityService.update for user 2 sets tags: [‘cats’, ‘dogs’, ‘parrots’]

In this example, user 1’s tag addition, ‘turtles’ was overwritten, because the order of the operations was not atomic.

Is there a way to avoid this scenario in strapi?

Answering my own question here. There is a way to partial update the list of relations, which avoids the necessity for a full update using entityService.findOne followed by entityService.update.

    await strapi.entityService.update('api::vod.vod', vodId, {
      data: {
        tags: {
          connect: [tag]

connect is part of the REST API, but it also applies to entityService. This single request avoids the potential non-atomic operation problem when making multiple requests.

1 Like