Avoid attaching to orders promotions that will never be eligible? #4202
Replies: 5 comments 5 replies
-
This is great. Is this something that can be applied to Solidus core as a general technique? @elia if not, you are simply implementing this as an override of the sale_promotions? |
Beta Was this translation helpful? Give feedback.
-
We have a similar performance issue with this escenario: Some users might get a welcome promo code that automatically applied to their first order This caused a lot of promotions with promotion rules: first order, specific to one user, and potentially tied to a some taxons, and the action was to create adjustments in different percentages So, Spree::PromotionHandler#sale_promotions would return all of them then it tried to activate every single promo, including other users promos that will never be activated Our solution was to filter promotions that apply automatically that had the rule User and the value was not the user's email. This fixed the performance issue |
Beta Was this translation helpful? Give feedback.
-
I think there's enough feedback to justify the need for this in Solidus Core. If we have this behavior enabled with a feature flag, well, that's an easier choice. I will bring this to our Core Team meeting and shoot an update here. |
Beta Was this translation helpful? Give feedback.
-
@elia everyone in the Core Team is onboard with this direction, we are just waiting for the PR to review. Thanks! |
Beta Was this translation helpful? Give feedback.
-
#4304 is related, and will help future improvements in this domain: This (and other, more impactful) optimizations can only happen if the match policy for promotions is |
Beta Was this translation helpful? Give feedback.
-
What happened
A few months ago we faced an issue in production because the number of promotions in the system increased and we suddenly got a few dozens of them attached to every order. 😱
That with the one-page checkout we're currently employing that goes through all the steps all at once made the time taken to re-evaluate each of them exponentially higher with every additional promotion. 💥
you can se the latency go up, driven by calls to
populate
How it was fixed
In the aftermath we realized the system is attaching to orders a number of promotions that are actually guaranteed to never be eligible, in our case any
PromotionRule::User
that is not connected with the user of the order and hasmatch_policy: :all
can be skipped, making the calls torecalculate
much faster.after drastically reducing the number of promotion-adjustments attached to orders we're back to normal
What about you?
You can check how many ineligible promotions you have attached to your orders with a query similar to this:
What can we do?
We're now extending that optimization to most promotion rules to bring the number of ineligible promos from a constant number to nearly zero.
The technique is to filter the result of
Spree::PromotionHandler::Cart#sale_promotions
excluding those guaranteed to never be eligible, the code looks more or less like this:and can be generalized to:
Beta Was this translation helpful? Give feedback.
All reactions