Design Stack Overflow

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-31

Problem Statement

Design a low-level architecture for a question-and-answer platform where users can post questions, answer them, upvote/downvote content, and track reputation. The system should handle multiple users, tags, notifications, and support moderation features.


Functional Requirements (FR)


User Management


•Users can register/login using email or social accounts.
•Maintain user profiles including reputation, badges, and activity history.


Question Management


•Users can post, edit, and delete questions.
•Questions have title, body, tags, and optional attachments.


Answer Management


•Users can post, edit, and delete answers.
•Mark one answer as “accepted” by the question owner.


Voting System


•Users can upvote/downvote questions and answers.
•Reputation changes based on votes and accepted answers.


Tag Management


•Categorize questions using tags.
•Tags have popularity metrics and subscription options.


Commenting System


•Users can comment on questions and answers.


Search & Filters


•Search questions by keywords, tags, or users.
•Filter by popularity, recency, or relevance.


Notifications


•Notify users about answers, comments, votes, and reputation changes.


Moderation


•Flag inappropriate content.
•Admins can delete or edit content.


Non-Functional Requirements (NFR)


•Scalability → Handle millions of questions, answers, and users.
•Low Latency → Quick retrieval of questions, answers, and search results.
•Consistency → Accurate reputation and vote counts.
•High Availability → Ensure 24/7 uptime.
•Security → Protect user data, prevent spam, and secure authentication.


Core Classes & Entities


User


•userId, name, email, reputation, badges, activityHistory


Question


•questionId, userId, title, body, tags, votes, answers, status


Answer


•answerId, questionId, userId, body, votes, isAccepted


Comment


•commentId, userId, contentId (question/answer), body


Tag


•tagId, name, popularity, subscribers


Vote


•voteId, userId, contentId (question/answer), voteType (up/down)


Notification


•notificationId, userId, message, timestamp


Key Methods / Operations


User Service


•registerUser(), loginUser(), getUserProfile(userId)


Question Service


•postQuestion(userId, title, body, tags)
•editQuestion(questionId, newDetails)
•deleteQuestion(questionId)
•getQuestionsByTag(tag)


Answer Service


•postAnswer(userId, questionId, body)
•markAccepted(answerId)
•editAnswer(answerId, newBody)


Vote Service


•upvote(userId, contentId)
•downvote(userId, contentId)


Comment Service


•addComment(userId, contentId, body)
•editComment(commentId, body)


Notification Service


•sendNotification(userId, message)


Design Considerations


Reputation & Votes


•Update user reputation atomically on vote/accept events.
•Prevent duplicate voting by the same user.


Search & Tagging


•Use indexing or search engine (like Elasticsearch) for fast queries.


Concurrency & Consistency


•Ensure vote counts and accepted answers remain consistent across concurrent operations.


Extensibility


•Support badges, achievements, question history, and future analytics features.


Caching


•Frequently accessed questions, top answers, and trending tags cached for performance.


Example Flow


1.User posts a question → Question Service saves question → Update tags.
2.Another user answers → Answer Service saves answer → Notify question owner.
3.Users upvote/downvote → Vote Service updates votes → User reputation recalculated.
4.Question owner marks answer as accepted → Answer Service updates isAccepted → Update reputation.
5.Notifications sent for votes, comments, and answers.


Solution

Comments