#72 Rule Engines/Price Consistency
Pricing and Discount Engine
Work out the final price of a booking from base rates, taxes, promotions and coupons, the same way every time.
What is being asked
Design the service that answers "what does this cost?" A hotel room has a base rate; then come member discounts, a 10%-off promotion, a coupon code, a package discount, taxes and fees. Search, the cart, checkout and the invoice must all show the same number.
Requirements
Functional
- Marketers create promotions with rules: dates, destinations, user segments, minimum spend, stacking limits.
- Apply coupons and validate their usage limits.
- Return an itemised price breakdown.
Non-functional
- Deterministic: the same input always gives the same price.
- Fast enough to price every result on a search page.
- Coupon limits ("first 1,000 users") enforced under concurrency.
Design
- Pricing runs as an ordered pipeline: base rate → markups → promotions → coupons → taxes and fees. Each step adds a line to the breakdown.
- Promotion rules are compiled into an in-memory structure indexed by destination and date, so evaluating thousands per search is cheap.
- At checkout, the engine issues a price quote with an ID and expiry. Payment charges exactly the quoted amount.
- Coupon redemptions use an atomic counter or a unique row per redemption, so limits hold under load.
Key decisions and trade-offs
- Quotes make prices stable. Without a quote, a promotion ending between cart and payment would change the total at the worst moment.
- Stacking rules must be explicit ("best single discount" vs "apply all"). Ambiguity here costs real money.
- Rules as data, not code. Marketers change promotions daily; a rule engine avoids deployments, but needs validation and a preview.
- Rounding and currency happen once, at the end, with a fixed rule; per-line rounding produces off-by-one-rupee invoices.
Follow-up questions
- How do you stop a coupon being shared and redeemed 50,000 times?
- How would you test a new promotion before it goes live?
- Prices shown on search are cached. How stale is acceptable?
- How do you explain a price to a customer support agent?