Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 38 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 26, 20264 min read

Handling Conflict Resolution: Optimistic Locking in WordPress

Prevent data overwrites and ensure data integrity in your WordPress plugins by implementing optimistic locking, handling concurrency, and improving UX.

WordPressREST APIReactConcurrencyData Integrityphpplugin-development

Previously in this course, we built robust custom hooks in creating-custom-hooks-for-api-logic-in-wordpress-plugins to simplify our data layer. Now, we're tackling a critical production-level challenge: what happens when two admins try to edit the same knowledge base article at the same time? Without a strategy, the last person to hit "Save" silently overwrites the previous user’s changes.

Understanding Concurrency and Data Integrity

In a distributed environment like the WordPress admin, concurrency issues arise when the gap between "reading" data and "writing" data is long enough for another process to intervene. To maintain data integrity, we must ensure that a user is only modifying the version of the data they actually saw on their screen.

We achieve this via optimistic locking. Instead of locking the database row (which can lead to performance bottlenecks), we assume conflicts are rare. We attach a version number (or timestamp) to our records. When a user submits an update, the system compares the version they retrieved with the current version in the database. If they don't match, the update is rejected, preventing accidental data loss.

Implementing Optimistic Locking in the REST API

To implement this, we need to track a version column in our database. Every time an entry is updated, we increment this value.

1. The Database Schema Update

First, ensure your custom table has a version column (integer, default 1).

2. The REST API Update Logic

When updating your Knowledge Base entry, your API endpoint must accept the expected version and verify it.

PHP
#6A9955">// Inside your REST API update callback
public function update_item( $request ) {
    $id = $request['id'];
    $expected_version = (int) $request['version'];
    
    global $wpdb;
    $table = $wpdb->prefix . 'kb_entries';
    
    #6A9955">// Fetch the current state from the DB
    $current_entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE id = %d", $id ) );

    #6A9955">// CONCURRENCY CHECK
    if ( (int) $current_entry->version !== $expected_version ) {
        return new WP_Error( 
            'kb_conflict', 
            'The data has been modified by another user. Please refresh and try again.', 
            array( 'status' => 409 ) 
        );
    }

    #6A9955">// Perform the update and increment version
    $wpdb->update( 
        $table, 
        [ 'content' => $request['content'], 'version' => $current_entry->version + 1 ],
        [ 'id' => $id ]
    );
    
    return rest_ensure_response( [ 'success' => true ] );
}

Improving UX with Conflict Notifications

When the API returns a 409 Conflict status, your React application must handle it gracefully. Instead of a generic error, provide a way for the user to reload the data without losing their current work if possible, or at least inform them clearly.

In your React component, use the api-fetch response to catch the error:

JAVASCRIPT
const handleSave = async (data) => {
    try {
        await apiFetch({
            path: CE9178">`/kb/v1/entries/${data.id}`,
            method: CE9178">'POST',
            data: { ...data, version: currentVersion }
        });
    } catch (error) {
        if (error.code === CE9178">'kb_conflict') {
            // UX: Alert the user and offer a refresh
            alert("This entry has changed! Please reload to see the latest version.");
        }
    }
};

Hands-on Exercise

  1. Modify your database: Add a version column to your kb_entries table if you haven't already.
  2. Update the API: Implement the version check in your POST or PUT endpoint as shown above.
  3. Test the flow:
    • Open two browser tabs with the same edit screen.
    • Save in Tab A (incrementing the version).
    • Attempt to save in Tab B (which still has the old version number).
    • Verify that your API returns a 409 error and your UI displays the notification.

Common Pitfalls

  • Ignoring the Status Code: Always use 409 Conflict for concurrency errors. This is the semantic HTTP standard and allows your frontend to distinguish between validation errors (400) and concurrency conflicts.
  • Stale Data in Redux: If you are using the WordPress Data store (as discussed in handling-complex-state-dependencies-in-wordpress-data-stores), ensure your store refreshes the version after a successful save. If the store remains stale, the user will trigger a conflict on their very next save attempt.
  • Forgetting to Increment: It's easy to update the content but forget to increment the version number in your SQL query. Always perform both atomically.

Recap

Optimistic locking is a lightweight, high-performance way to handle concurrency. By validating version numbers against the database at the moment of submission, we protect data integrity. When conflicts occur, surfacing the issue clearly through the UI ensures a professional and reliable user experience.

Up next: We will build a Modal Confirmation System to add a final layer of safety to destructive actions.

Previous lessonIntegrating with Gutenberg BlocksNext lesson Building a Modal Confirmation System
Back to Blog

Similar Posts

WordPressJune 26, 20263 min read

Implementing Activity Logging: Auditing REST API Changes in WordPress

Learn to build a robust audit system by hooking into the WordPress REST API, logging changes to a custom table, and displaying the history in your React UI.

Read more
WordPressJune 26, 20263 min read

Building a Modal Confirmation System for WordPress Plugins

Learn to build a safe, user-centric Modal confirmation system in your WordPress React admin UI to prevent accidental data loss during API operations.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 38 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min
Read more
WordPressJune 26, 20264 min read

Integrating with Gutenberg Blocks: Dynamic Data Fetching

Learn to build dynamic Gutenberg blocks that fetch live Knowledge Base data via the REST API. Master the @wordpress/data store integration for your blocks.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    4 min
  • 25

    Advanced Sanitization Techniques

    3 min
  • 26

    Input Validation and Error Handling

    3 min
  • 27

    Protecting Admin Screens

    3 min
  • 28

    Production Build Pipeline

    3 min
  • 29

    Debugging React in the WordPress Admin

    4 min
  • 30

    Building Search and Filter Functionality

    3 min
  • 31

    Internationalization in React

    3 min
  • 32

    Managing File Uploads via REST API

    3 min
  • 33

    Optimizing API Response Times

    3 min
  • 34

    Working with Date and Time in React

    3 min
  • 35

    Implementing Drag-and-Drop Sorting

    3 min
  • 36

    Creating Custom Hooks for API Logic

    3 min
  • 37

    Integrating with Gutenberg Blocks

    4 min
  • 38

    Handling Conflict Resolution

    4 min
  • 39

    Building a Modal Confirmation System

    3 min
  • 40

    Implementing Activity Logging

    3 min
  • 41

    Using Webpack Aliases

    3 min
  • 42

    Unit Testing API Endpoints

    3 min
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course