Designing a Million‑User, Millisecond‑Level Lottery System with MySQL and Redis

This article presents a scalable design for selecting N winners from over ten million users during a flash‑sale event, comparing naive SQL approaches with Redis‑based solutions that guarantee uniqueness, atomicity, and sub‑millisecond latency.

JavaEdge
JavaEdge
JavaEdge
Designing a Million‑User, Millisecond‑Level Lottery System with MySQL and Redis

Requirement

During a flash‑sale event at midnight, the system must randomly select N users from more than ten million followers for prize distribution, ensuring that each user can participate only once and cannot win multiple times.

Design Options

1. Simple RAND() ordering

Generate a random number for each row and order by it.

select * from 关注用户表 order by rand() desc limit 0,100

This approach quickly becomes infeasible with tens of millions of rows.

2. Two‑step offset selection

First compute a random offset, then fetch a single row.

offset = SELECT FLOOR(RAND()*COUNT(*)) AS offset FROM 关注用户表;
select * from 关注用户表 limit offset,1;

Works but requires two statements and still cannot guarantee uniqueness under high concurrency.

3. Redis Set with SPOP

When a user follows the live stream, insert its ID into a Redis Set userlist. The set guarantees global uniqueness and stores IDs in hash‑shuffled order. sadd userlist xxxid During the draw, call spop userlist to atomically pop a random ID. Repeat until the required number of winners is obtained. Then retrieve full user data with a single SELECT ... WHERE id IN (...) query (max 1000 IDs per IN clause).

4. Pure Redis solution

If memory is not a constraint, keep all user IDs in a Redis Set. For ten million users the set occupies roughly 3 GB. spop can return 100 IDs in about 2.5 ms, and the operation is fully atomic.

sadd userlist '123456:ikun'
sadd userlist '123456:akun'
sadd userlist '123456:bkun'

Performance tests show 1000 spop calls take 2565 ms (≈2.5 ms per call).

Conclusion

The Redis‑based approaches provide millisecond‑level latency and guarantee non‑duplicate winners at the scale of tens of millions of users. Do you have a better design?

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redismysqlLotteryrandom selection
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.