Skip to content

Polls live only in one server's memory, so they break with more than one ws-server #252

Done

Description

@TheCodeHeist-Coder

What happens

All poll state is held in plain Maps inside a single Node process:

// apps/ws-server/src/utils/pollManager.ts
const activePolls = new Map<string, ActivePoll>();
const recordedPollsCache = new Map<string, ActivePoll[]>();
const pollTimers = new Map<string, NodeJS.Timeout>();

The rest of ws-server is deliberately built so it can run on several instances: session data, questions, the leaderboard and the question deadline all live in Redis, and broadcastToSession publishes over Redis pub/sub so a participant on instance B sees what a host on instance A did. ARCHITECTURE.md documents this under "Redis pub/sub for horizontal scale".

Polls skip that entirely.

Why it matters

With two or more ws-server instances behind a load balancer:

  • A host on instance A creates a poll. broadcastToSession relays poll:started to instance B, so B's participants see the poll.
  • Those participants vote. Their sockets are on B, so votePoll looks in B's activePolls map, finds nothing, and returns POLL_NOT_FOUND.
  • Every participant not on the host's instance gets "Could not record your vote."

The same split hits poll:end, poll:record and poll:get-recorded. A restart or deploy also wipes every active poll, since nothing is read back from Postgres.

This does not show up in the current single-instance compose setup, which is why it has not been noticed, but it blocks scaling the WS tier — the one thing the architecture was designed for.

The fix

Move poll state into Redis, the way the question timer already does it:

  • quiz:poll:active:${sessionId} — the poll, as JSON
  • quiz:poll:voters:${pollId} — a Redis hash of participantId -> optionId, so the duplicate-vote check works across instances. HSETNX makes "one vote per participant" atomic instead of a Map.has check that races.
  • Vote counts derived from the hash, or kept in a parallel hash with HINCRBY

apps/ws-server/src/utils/timeManager.ts and cache.ts are good models — the deadline key is the same shape of problem, already solved.

The timer needs care too. setTimeout lives on one instance, so an auto-expiring poll ends only if that instance is still up. Storing an endsAt deadline (as startQuestionTimer does) and checking it is more robust than relying on the timer alone.

Scope

This is a meaty one — good for someone who wants to understand the Redis layer. Worth reading the "Caching and scale" section of ARCHITECTURE.md first.

Files

  • apps/ws-server/src/utils/pollManager.ts
  • apps/ws-server/src/utils/handleMessages.ts — the poll:* cases

Activity

  1. added
    bug
    on Nov 25, 2026
  2. divyanshAgarwal123 commented on Nov 25, 2026

    @divyanshAgarwal123
    Contributor

    Could you assign #252 to me, sequenced after the narrow #251 vote-binding fix if you allocate both? On main dc7a83c, active/recorded state and timers remain process-local Maps, while question timing/broadcasts already use Redis. I would reuse that Redis layer, make create/vote/end decisions atomic across workers, preserve the existing one-vote and Prisma recording behavior, serialize the voter state compatibly, and enforce stored deadlines so restart does not leave an expired poll active.

    Validation would use two independent manager instances against a disposable local Redis, concurrent duplicate/competing votes, end/expiry races, restart recovery, recorded-poll retrieval and unchanged handler/organizer behavior, plus the full ws-server and repository CI gates. I would keep the frontend wire contract stable and coordinate the shared handleMessages edits rather than stack unrelated changes into #251. No implementation or test result is claimed yet. The #242 configuration stage is ready in #265; I will wait for your assignment/merge-order confirmation and respect your active-issue limit. Please allocate this only if the additional slot fits your policy; the existing nsoc26/level3 classification is yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglevel3nsoc26

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions