How to Build a Scalable Discount System with Real‑Time Crowd Sync
This article explains a three‑step architecture for describing, storing, and calculating fan‑based discounts, introduces a crowd‑based abstraction to accelerate high‑traffic queries, and details a hybrid offline‑plus‑real‑time data‑sync strategy that ensures consistency and millisecond‑level response times.
Problem & Challenges
Designing a discount system that can describe, store, and compute promotions while remaining scalable and performant under high traffic, and ensuring data consistency for accelerated discount queries.
Background
In the Xianyu marketplace, sellers offer targeted one‑price discounts to all fans, old fans, or purchasers of specific items, allowing buyers to see the lowest applicable price in real time.
Technical Implementation
The solution is realized in three steps:
Decompose discount elements into a basic expression and calculation model.
Abstract and accelerate the discount‑object determination process to support high‑traffic queries.
Synchronize data offline plus real‑time to guarantee consistency.
Discount Description, Storage, and Calculation
A discount consists of three parts: Discount Object + Discount Product + Discount Price . For example, the rule "SellerA_all_fans + Product1234 + 18.88" means all fans of Seller A can buy Product 1234 for 18.88 CNY.
When a buyer views Product 1234, the system:
Fetches the matching discount rule.
Interprets the object symbol (e.g., all fans).
Checks whether the buyer belongs to that group and applies the price.
Crowd Abstraction and Acceleration
Two approaches exist for representing whether a user belongs to a group: tagging the user directly or creating a separate crowd object. The implementation adopts the crowd object method, storing relationships in Redis with keys like ${user}_${crowd}. Existence of the key indicates membership, enabling fast cache‑like checks.
Data Consistency for Crowd Sync
The synchronization process combines:
Offline T+1 batch jobs that load follower/followed relationships into the crowd service.
Real‑time streaming of follow/unfollow events to keep the crowd up to date.
Advantages include real‑time freshness, eventual consistency via batch correction, and full initial data loading.
Two consistency issues arise:
Unrecorded unfollow events in offline data cause stale crowd entries.
Race conditions where a recent unfollow is overwritten by a pending batch sync.
Solutions:
Assign an expiration time slightly longer than the batch interval to crowd entries, allowing stale unfollow records to expire automatically.
During real‑time sync, write a temporary “unfollow” flag in Redis; batch jobs check this flag to avoid overwriting.
These mechanisms ensure that high‑traffic recommendation and search scenarios see near‑real‑time consistency, while transactional flows can perform a final verification before order placement.
Conclusion
The article demonstrates that by decomposing discount elements, defining crowd objects, and employing a hybrid offline‑plus‑real‑time sync, a discount system can achieve:
Scalable description, storage, and computation of promotions.
Millisecond‑level response for tens of thousands of queries per second.
Strong data consistency guarantees through expiration handling and conflict‑avoidance logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
