• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WP Desk = premium plugins for WordPress & WooCommerce
  • Plugins
        • Checkout & OrdersMake your work easier and automate the orders. Adjust the order form to your needs and make it more useful to your customers. Print order labels directly from WooCommerce. Put the orders in order using our plugins. Find out how to streamline the procurement process in your store.
          • Flexible Checkout Fields
          • Active Payments
          • Automatic Payment Status
          • Flexible Refund and Return Order
          • Print Orders and Address Labels
          • Flexible Subscriptions Recurring Shipping
          • Flexible Subscriptions Payment Retry
          • Flexible Subscriptions Stock Management
        • ProductsThese plugins will help you manage your WooCommerce products. Here you will find the plugins to import and update products from XML or CSV files. Then you can change the product unit of measure, show the lowest price in the last 30 days (Omnibus Directive), or let customers name the product price!
          • Flexible Product Fields
          • Dropshipping Import Products
          • Dropshipping FTP Import Products
          • Dropshipping Export Products
          • Custom Price
          • WP Desk Omnibus
          • Flexible Quantity Calculator
          • Dropshipping Orders Export
        • MarketingPromote your store online. Add products from your shop to price comparison websites directly from WooCommerce. Acquire new customers and communicate with your existing ones. Build a customer base and communicate with clients using one of the most effective marketing tools, namely e-mail marketing. Check out our promotional tools!
          • Flexible Pricing
          • Flexible Wishlist - Analytics & Emails
          • OLX WooCommerce
          • Flexible PDF Coupons
          • ShopMagic
          • Flexible Invoices
  • Bundle🔥
  • WP Desk Care
  • Support
  • Blog
  • My Account
  • 0

More results

Atomic operations in WordPress

Tech Posts, WordPress design & development

Krzysiek Dyszczyk3 minutes readUpdated: 2021-03-05Updated: 2026-06-08
blog category heading image

As the famous saying goes, There are only two hard things in Computer Science: cache invalidation and naming things. The author of this saying has apparently never run into any problems with process synchronization.

Contents

  • Threads... Threads everywhere
  • Snake in the grass
  • But why doesn't it work
  • Atomic operation
  • Database to the rescue
    • Related Articles
  • Mutexes, Deadlocks and other developer nightmares
  • WooCommerce bulk price update (programmatically and with a plugin)
  • WooCommerce Products - The basics of e-commerce product management
  • WooCommerce shortcodes & blocks - the complete list with examples

Threads... Threads everywhere

It's easy to forget that although we usually don't write asynchronous code in PHP and the WordPress backend, each line of code executes sequentially; in practice, the server runs and executes multiple PHP processes simultaneously. For this reason,every plugin’s code is always executed concurrently. We must always bear in mind the problems that arise in multi-threaded and distributed environments.

A typical WordPress problem where multithreading rears its ugly head is when, based on the value of a variable stored in the database, we set a new value for this variable. Strangely enough, such situations happen very often.

Snake in the grass

Let's assume that, as in WooCommerce, there is a post_type named product, and its metadata store information about how many products are in stock. When a customer buys a product, we want to reduce the number of products in stock by one. How to do it? Nothing is easier: we read the number of products in stock, decrease the value by one, and save it back.

Trivial? Yes, as long as we do not receive a report from the first angry customer who has sold more products than they had in stock. The next customer reports that they still have products in stock, but the store says there are no more. What has happened? Has anyone hacked our WordPress to put random numbers into the database? Or maybe it's because we haven’t used WooCommerce methods? Let's try:

Now it's PRO. We can now go to sleep, sure that this time we did everything perfectly. Unfortunately, the morning will bring us another sad customer. What happened? How could we have made a fatal error in two lines of otherwise obviously correct code?

But why doesn't it work

As you might have guessed, the problem is that multiple PHP processes can modify the same information simultaneously. Imagine that two different customers purchase the same product at the same time. Both processes take place parallelly. The first process gets the number of products in stock, the second process also does it a millisecond later; both get the same value, say 9. Both processes decrease this value by 1 and get 8, and both write the number 8 to the database. We sold two products, but the shop thinks there are still 8 products in stock. If no one notices this, products that are not in stock will soon be purchased.

While one PHP process was calmly executing line-by-line commands, another process slipped into the same place in the code and mixed up everything.
Will this happen frequently, or is it a purely academic problem? Unfortunately, and counterintuitively, since threads tend to wait together for I/O operations, this type of code will often fail even in a small store. It's just a matter of time.

How to solve this problem?

Atomic operation

Often, and this is also the case here, it is possible to replace several operations with one atomic operation. An atomic operation cannot be subdivided into smaller ones. Such an operation is either done or not done and will never be done a little. Because when we do the work with an atomic operation, other PHP processes will not enter between our commands. With one command, we will simultaneously get, change, and save the warehouse's stock. How to do it?
Certainly NOT like this:

Such refactoring does not change anything. PHP processes will continue to enter and execute the decrease_stock function line by line. The fact that a function can be invoked with one PHP command does not make the operation atomic.

Database to the rescue

We can take advantage of the database and the fact that a single UPDATE/DELETE operation is always atomic.

Now we have a code that will reduce the number of products in stock with one atomic operation.Note that we cannot use the wpdb::update method because this command does not enable decreasing the value, only overwriting it.

What to do when the task cannot be easily reduced to a single atomic operation? How does a database handle reading, changing, and writing values ​​atomically? I will share this with you in the next posts!

Related Articles

Mutexes, Deadlocks and other developer nightmares

The use of atomic operations is a beautiful and simple solution to many problems that arise in multi-threaded environments where many processes attemp...
WooCommerce bulk price update programmatically and with a plugin

WooCommerce bulk price update (programmatically and with a plugin)

Fast changes in WooCommerce are crucial to run your store. The bulk price update for WooCommerce products is one of them. Today, I will show you how t...
WooCommerce product manager plugin guide

WooCommerce Products - The basics of e-commerce product management

Launching and running a WooCommerce shop is very simple in the long-term perspective. The beginnings are difficult in any field. To make a start easy ...
WooCommerce shortcodes (practical guide with the cart, checkout, products, shop, product, and login shortcode examples)

WooCommerce shortcodes & blocks - the complete list with examples

You may find many posts about WooCommerce shortcodes online. In this article, you’ll find the full WooCommerce shortcode list with screenshots. Quickl...

3 minutes read78009 views

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Krzysiek Dyszczyk

Co‑founder & CEO of WP Desk

LinkedIn Profile

Krzysiek is the Co‑founder and CEO of WP Desk, having co-founded the company in 2012, and previously served as CTO, managing its technical vision...

Powered by WP Desk

WP Desk brings you great WooCommerce plugins. We strive to save your time and money by speeding up your processes. Use our plugins to build a better store. Awesome support included in the package.

Premium WooCommerce Plugins →
WP Desk › Blog › WordPress design & development › Atomic operations in WordPress

Primary Sidebar

Contents

  • Threads… Threads everywhere
  • Snake in the grass
  • But why doesn’t it work
  • Atomic operation
  • Database to the rescue

All Plugins Bundle £99

28 plugins included! 1 Site for each plugin Regular updates and support Priority 1-on-1 Support 30-day money back guarantee

Add to cart or View Details
WP Desk
Plugins used by 239,859+ shops

Stay updated on our how-to articles

WP Desk news, WooCommerce tips, promo codes - right to your inbox.

By entering your e-mail, you agree to our Terms & Conditions and Privacy Policy.

By entering your e-mail, you agree to our Terms & Conditions and Privacy Policy.

Footer

WP Desk - WooCommerce Plugins

At WP Desk we create great WooCommerce plugins with awesome support. Save time and money with our e-commerce solutions. See how we can help you improve your e-store →

Secured by Comodo

WP Desk

  • About us
  • Giving Back
  • Blog
  • Get Support
  • Contact us

Products

  • Premium Plugins
  • WP Desk Care
  • Documentation
  • WooCommerce Invoices
  • Email Marketing
  • PDF Coupons

Legal

  • Terms & Conditions
  • Refund Policy
  • Support Policy
  • Privacy Policy

© 2026 WP Desk

We value your privacy

We use necessary cookies to make our site work. We'd also like to set additional cookies to understand site usage, enhance your browsing experience, and serve personalized ads or content. By clicking ‘Accept All,’ you consent to our use of additional cookies.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}