List functional requirements for the system (Ask the chat bot for hints if stuck.)...
1) To play a chess game between users
2) To match users based on their skill level
3) To make a move only if the move is valid
4) System should be able to determine outcome of a game
5) Users should be able to get the game history and statistics
List non-functional requirements for the system...
1) System should be fault tolorent
2) System should have low latency
3) System should be highly available
Estimate the scale of the system you are going to design...
Average duration of a game 30 minutes for 2 player
Lets assume there are 10,000 users and each plays 10 games in a day
total time in seconds for 10 games = 30*10*60=18000
For 10^5 users total time in seconds =10^5*18*10^3
=9*10^8
total seconds in a day = 10^5
total qps =9*10^8/10^5=9*10^3
Define what APIs are expected from the system...
1) FindUser - To find the user based on the current player skill level it will return status 200 if user is found else return 400 staus
2) MoveValid - To check is the move is valid based on the state of the system
it returns status 200 if the move is valid else return 400
3)FindOutcome - System should return the outcome of the game baed on the current state and will return status 200 and message whether the game has been won
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
As the system is read heavy instead of write heavy as many api are reading from the table to know the next move,status of the game and other things so we will be using a sql database
When a game is started between two users a table name with hash of users is created and will have two columns row, col,id to store the position of a piece for a particular user
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Each Call is made to the load balancer which redirects the call to api server the api server will make a call to database to get the perfect match for a user.
After performing the perfect match the temp table is created in sql databse based on hash of user1 and user2 and for each write operation the update is first made to cache then to the database. and for calls like userMatch they will be directly hitting the db and small latency is acceptable.After the game is won the temp sql table which is created during the game will be deleted. As the system is realTime so we cannot use kafka for asynchronous system.
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Each Call is made to the load balancer which redirects the call to api server the api server will make a call to database to get the perfect match for a user.
After performing the perfect match the temp table is created in sql databse based on hash of user1 and user2 and for each write operation the update is first made to cache then to the database. and for calls like userMatch they will be directly hitting the db and small latency is acceptable.After the game is won the temp sql table which is created during the game will be deleted. As the system is realTime so we cannot use kafka for asynchronous system.
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Same as above
Explain any trade offs you have made and why you made certain tech choices...
1) Creating sql databse can be expensive we can also use no sql database like mongodb to store the game in each document
2)As we are choosing sql database the system will be in consistent state
Try to discuss as many failure scenarios/bottlenecks as possible.
1) As we are storing the table for each game then cache can be overloaded so storing all the values in database will be benefical
2) In case of cache failure the system can go in unconsistent state as most of the reads will go to database and there are chances that the read node is not in consistent state . To mitigate this we can use quoroms to guarantee the system is in consisten state.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?
1) We can use machine learning algorithms to perdict the outcome of a game at each move so the user can analyze the game at each move
2) We can also allow user to play against cpu which will use AI to predict next moves.