System design · LLD design
Stack Overflow
FR
1.User can add question which will have a title , body and a list of tag
2.User can add a answer for a particular question
3.one vote by a user for an item
4.author of the question can mark any answer as accepted
4.search a question by tag and optionally by a keyword in title
NFR
1.scale : system should be able handler increase number of question, answer and votes
2.Concurrency : system to be able to handle concurrent request on vote up and down
3.Extensible: system should handle different kinds of search
Entities
Tag :
-id
-name
User
-userID
-name
-emailID
Question extends Votable**
**-questionId: int
-tags : Set<Tags>
-user : User
-title : String
-creationTime: timestamp
-body : string
-answers : List<Answer>
-Answer acceptedAnswer
+addAnswer(Answer)
+acceptedAnswer(Answer)
+getAuthor()
Answer extends Votable**
**-answerID : int
-user : User
-body : String
-isAccepted : boolean
-question : Question
+markAsAccepted()
+getAuthor()
Vote:
-User vote
-VoterType
VoterType Enum
UPVOTE (1) , DOWNVOTE (-1)
Votable
-HashMap<String, Vote> votes; (based on userid)
+addVote(vote) - synchronized
Check if author is doing the vote return false
If existing vote revert that and add the new vote
+retractVote(vote) - synchronized
Remove vote
+getScore()
QuestionRepository - interface
+save()
+findByTitleKeyword()
+findByTag()
InmemoryQuestionRepository - implements QuestionRepository
ConcurrentHashMap<String , Set<Question>> tagBaseDQuestions
ConcurrentHashMap<String , Set<Question>> titleBaseDQuestions
+save()
+findByTitleKeyword() - split on words and make it as a key
+findByTag()
StackOverflowSystem
-QuestionRepository repository
-ConcurrentHashMap<String , User> users;
+createUser(userid , name , emailid)
+addQuestion(title, author , List<Tag> tags, body)
+postAnswer(question, author, body)
+vote(userid, Voterable, voteype)
+searchByTag()
+searchByTitleKeyword()
+acceptedAnswer(questionId, answerID)