jamesgol / woocommerce-cart-stock-reducer

Allow WooCommerce inventory stock to be reduced when adding items to cart
GNU General Public License v2.0
58 stars 19 forks source link

Cart Stock Reducer - Performance Issue at High Load #74

Closed connoti closed 2 years ago

connoti commented 5 years ago

Hi James, First Thank You for this plugin! It has helped out my client 100% on his site.

Here is the situation though, he does weekly Flash Sales which brings the server down each week. New Relic is reporting that 2 components in this plugin are causing the issue. Specifically WC_CSR_Sessions::find_items_in_carts and WC_CSR_Sessions::quantity_in_carts

If you see the graphs below you can see the impact that this has on the server. BTW this is a Kinsta Server with 6 PHP workers so it's not a server resource problem. What are your thoughts on this to get this figured out?

Screenshot 2019-04-02 10 03 56

jamesgol commented 5 years ago

There are definite room for improvements with those functions. I had intended on better using caching rather than rebuilding the list for each request.

connoti commented 5 years ago

Thanks for the reply! Is there something that you are aware of that could be a quick fix? The load was about 60-80 people but I could see how clicking around the site could really cause a lot of havoc here :)

jamesgol commented 5 years ago

I can't think of an easy quick fix. I believe most people that use this plugin have pretty small sites selling single items.

It is on my (long) todo list to work on improving performance, but I have limited free time available.

photogenic89 commented 3 years ago

Hej James, have you found time to further improve the performance since then? Only asking because we are planning on using your plugin for high load situations as well and we want avoid bringing our servers down.

jamesgol commented 3 years ago

It should be slightly better because it only queries the session table when something needs to know the quantity instead of on every request but it's still not at a level I'd be happy with for a high load site.

photogenic89 commented 3 years ago

What do you think would make sense here? Maybe to create a reduced_cart_item table which includes id's and virtual stock (and session id's?) of all products and which is constantly updated? Then you would not need to loop through all sessions but you could pick the right value directly from the database.... I'm currently going through your code, trying to contemplate what could be done...

jamesgol commented 3 years ago

It needs some major refactoring. The plugin was designed for WooCommerce < 3.0 and then when 3.0 came out I had to do some major hackery to get it to work properly. There are different actions/filters available now that would be better to use than the way it's working now.

I'm not a fan of adding another table for something like this. I'd probably use a transient to "cache" the virtual stock, anytime an item is added/removed/purchased delete the transient and let it be refreshed on the next query.

Really need some proper tests built out to test everything.

jamesgol commented 3 years ago

Sorry it's taken so long, but I just pushed out a new version that should solve this issue.