Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Capstone: Moderated Multi-Room Chat Router

Track

Design-only

Brief

A community platform is adding moderated rooms to its existing chat service, where users currently hold direct references to room delivery and storage components. Messages need per-user rate limits, role checks, and configurable content screening before anyone receives them. Moderators must be able to hide a message and later undo that action without losing the original content or the moderation history. Design a router that keeps room coordination centralized, allows screening rules to evolve, and supports independent delivery and audit subscribers.

Constraints

  • The service must sustain 5,000 messages per second across 2,000 active rooms with accepted-message routing below 150 ms at p95.
  • Four application instances run concurrently, so per-user and per-room rate counters must be atomic across instances.
  • A three-person backend team maintains the existing HTTP/WebSocket stack and a shared Redis deployment.
  • Screening chains differ by room, but every chain must end in an explicit allow, reject, or quarantine decision.
  • Only senior moderators may restore hidden content, and every hide and restore must retain actor, reason, and timestamp.

Required Patterns

  • Mediator — WHY: a room mediator can coordinate participants, persistence, screening, and delivery without giving every peer references to every other component.
  • Chain of Responsibility — WHY: rate limiting, authorization, and content screening are ordered handlers that can stop processing or pass a message to the next policy.
  • Observer — WHY: WebSocket delivery, metrics, and audit recording can subscribe to accepted or moderated outcomes without becoming links in the screening chain.
  • Memento — WHY: a private snapshot of message visibility and content state enables authorized restoration while keeping message internals encapsulated.

Solution Sketch

Illustrative pseudocode — not runnable.

ChatMediator.receive(envelope):
  require authenticated sender and existing room membership
  chain = chainFactory.forRoom(envelope.roomId)
  decision = chain.handle(envelope)
    RateLimitHandler:                 # first, before expensive screening
      counters = Redis.atomicIncrement(senderWindow, roomWindow)
      return Reject(RATE_LIMITED) when either budget is exhausted
      otherwise pass to next
    AuthorizationHandler: return Reject(FORBIDDEN) or pass to next
    ContentScreenHandler: return Allow, Reject, or Quarantine with reason

  if decision is Allow:
    message = messageStore.persist(envelope, visible = true)
    publishAfterCommit(MessageAccepted(message, decision.reason))
  else:
    publishAfterCommit(MessageScreened(envelope.id, decision))

moderator.hide(messageId, actor, reason):
  require actor has MODERATE permission
  message = messageStore.lockForModeration(messageId)
  snapshot = message.createMemento()  # private originator state + version
  moderationLog.append(Hide, snapshot, actor, reason, timestamp)
  message.hide()
  publishAfterCommit(MessageHidden(messageId))

moderator.undoHide(moderationEventId, actor, reason):
  require actor has SENIOR_MODERATE permission
  entry = moderationLog.requireHideEvent(moderationEventId)
  entry.message.restore(entry.memento, expectedVersion = entry.version)
  moderationLog.append(Restore, actor, reason, timestamp)

subscribers = [WebSocketRoomObserver, MetricsObserver, AppendOnlyAuditObserver]
subscriber failures are isolated and retried without rerunning the screening chain

Rubric

  • Coordination boundary: Passes if only the mediator coordinates room submission and outcome publication; fails if participants call screening, storage, and one another directly.
  • Chain behavior: Passes if each handler documents its stop/pass behavior, all terminal decisions are explicit, and room-specific chains cannot silently fall through.
  • Rate limiting: Passes if atomic shared counters enforce both user and room limits before content screening across all four instances.
  • Observer separation: Passes if delivery, metrics, and audit subscribe to committed outcomes and their retries cannot resubmit a message to the chain.
  • Undo authorization: Passes if restore requires the senior role and restores only the identified hidden message using its own versioned memento.
  • Moderation history: Passes if hide and restore append separate records with actor, reason, and timestamp, and restoration never deletes the hide event or later user messages.