0
0
LLDsystem_design~10 mins

Piece movement rules (polymorphism) in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Piece movement rules (polymorphism)
Growth Table: Piece Movement Rules with Polymorphism
Users / GamesPieces per GameMovement Rule Checks per MoveConcurrent MovesMemory Usage
100 users32 pieces1-5 checks10 concurrentLow (few objects)
10,000 users32 pieces1-5 checks1,000 concurrentModerate (thousands of objects)
1,000,000 users32 pieces1-5 checks100,000 concurrentHigh (millions of objects)
100,000,000 users32 pieces1-5 checks10,000,000 concurrentVery High (billions of objects)

As users and concurrent games grow, the number of piece objects and movement rule checks increase linearly, impacting memory and CPU.

First Bottleneck

The first bottleneck is the CPU processing the polymorphic movement rule checks for each piece move. Each move requires calling the correct movement logic based on piece type, which can be CPU intensive at scale.

Memory usage also grows as each piece is an object with its own behavior, increasing heap size and garbage collection overhead.

Scaling Solutions
  • Horizontal scaling: Add more game servers to distribute concurrent games and moves.
  • Caching: Cache common movement results or precompute valid moves to reduce repeated calculations.
  • Object pooling: Reuse piece objects to reduce memory churn and GC overhead.
  • Optimize polymorphism: Use efficient dispatch methods or flatten movement logic to reduce CPU cost.
  • Sharding: Partition games by user or game ID to isolate load.
Back-of-Envelope Cost Analysis

Assuming 1 move per second per user:

  • At 1,000 users: ~1,000 moves/sec, easily handled by 1-2 servers.
  • At 1,000,000 users: ~1,000,000 moves/sec, requires ~200-300 servers (assuming 3,000 moves/sec per server).
  • Memory per piece object ~1 KB, 32 pieces/game, 1M games = ~32 GB memory just for pieces.
  • Network bandwidth depends on move data size (~100 bytes), so 1M moves/sec = ~100 MB/s bandwidth.
Interview Tip

Start by explaining how polymorphism helps organize movement rules cleanly. Then discuss how CPU and memory grow with users and moves. Identify the CPU as the first bottleneck due to polymorphic calls. Propose horizontal scaling and caching as primary solutions. Mention object pooling and sharding for memory and load distribution. Keep answers structured: problem, bottleneck, solution.

Self Check

Your game server handles 1,000 moves per second. Traffic grows 10x to 10,000 moves per second. What do you do first and why?

Answer: Add more game servers (horizontal scaling) to distribute the increased load and maintain performance, because CPU is the bottleneck processing movement rules.

Key Result
Polymorphic piece movement rules scale linearly with users and moves, making CPU the first bottleneck; horizontal scaling and caching are key to handle growth.