Direct answer
I documented the ProjectHub Poker/Retro vertical as a real end-to-end slice: eleven Postgres tables (five poker, six retro, including one shared reaction aggregate), a one-directional phase FSM, STOMP broadcast, and server-side-only author tracking for anonymity. The hard part is the schema, not the WebSocket.
Real-time collaboration is a schema problem, not a WebSocket problem. Anyone opening a STOMP tutorial assumes the heart of collaborative Planning Poker is the channel that pushes messages to clients. It is not. The channel is the easy part: you wire it in an afternoon. The part that keeps you awake is designing aggregates that survive a reconnect, hide votes until reveal, and guarantee anonymity even when the facilitator must be able to moderate. The ProjectHub Poker/Retro vertical (migrations V184 and V185) is my proof of that thesis, taken from code that actually runs.
This is not a scaffold invented for the article: it is two already-shipped Flyway migrations, eleven tables with named constraints and workspace-scoped indices, read by the same Spring Boot backend that serves the rest of the portfolio. I do not change a single line of that code here: I narrate it as a real vertical slice, from the aggregate design on the database up to the broadcast that reaches the frontend companion-tools UI, with the trade-offs declared at every fork.
Why a one-directional phase FSM
The retro board has four phases in a strict sequence: BRAINSTORM, then REVEAL, then GROUP_VOTE, then ACTION. The transition is one-way, the facilitator cannot go back. The choice is deliberate: a board that regresses from GROUP_VOTE to BRAINSTORM would reopen card writing while votes are already tallied, and the final record would no longer be defensible.
The constraint lives on two levels. On the database, the phase value is
a VARCHAR(12) with a CHECK that admits only the four legal
tokens (chk_ph_retro_board_phase): no invented phase can enter the row.
The direction of the transition, instead, is imposed by the application layer before
the UPDATE: that is where the "no back-transition" rule lives. The
database guarantees the value is valid; the application guarantees the sequence is
monotonic. Separating the two responsibilities is why I did not push a Postgres
trigger to guard the sequence: the domain logic stays in the domain, and the database
stays a guardian of shape, not of flow.
The poker deck and the rule that keeps votes hidden
The poker room lives on five tables: the room aggregate, participants with a VOTER or
OBSERVER role, the session that drives the issue queue, the ordered issue queue, and
the individual votes. The deck is 13 Fibonacci tokens, stored as
VARCHAR(8) because the coffee token and the half point are not numbers.
The interesting trade-off is vote confidentiality. Values stay hidden until the
facilitator triggers reveal: that is a rule the database cannot enforce alone,
because the card_value is always physically in the
ph_poker_votes row. Confidentiality lives in the application layer, which
must never include card_value in WebSocket deltas before the reveal
event. The uniqueness constraint uk_ph_poker_vote on (session, issue,
round, participant) instead guarantees one vote per person per round: changing a vote
is an UPSERT, not a second insert. That way the round statistics (min, max, mode,
mean) stay honest even when someone reconsiders.
Server-side anonymity: an author_user_id that never leaves
Retro cards are anonymous toward the other participants, yet the server knows who
wrote them. The author_user_id column on ph_retro_cards
exists for two legitimate reasons: to let the author delete or edit their own card,
and to let the facilitator moderate. It must not appear in any payload toward the
others.
Here the trade-off is anonymity versus auditability, and my choice was to not really
pay it: I keep both, but on different planes. Anonymity is a property of the outbound
channel, not of the row. The row keeps the author for moderation and for
"own card" auth (whoever deletes must be the author); the STOMP broadcast and the API
responses toward the other members simply do not project that field. The same holds
for voter_user_id in dot-votes: it serves only to enforce the budget and
to retract one's own vote, and is never transmitted. The "true anonymity"
alternative, meaning not storing the author at all, would have made moderation and
own-card deletion impossible: too high a price for a team retrospective tool.
The dot-voting budget and the per-person count
In the GROUP_VOTE phase each person has a dot-vote budget, 3 by default, settable at
board creation. The uk_ph_retro_vote constraint on (board, voter,
target_type, target_id) prevents voting the same target twice; the overall budget,
instead, is counted by the application layer before the insert.
The reason the budget is not a database CHECK is simple: it is an
aggregate rule, not a row rule. A CHECK constraint sees one tuple at a
time and cannot count how many vote rows already exist for that voter on that board.
Counting the active votes and deciding whether to accept the insert is the
application's job, which has the full view. The database does its part with
per-target uniqueness and with CHECK vote_budget >= 1 on the board;
the rest is domain.
The reaction aggregate shared by two contexts
The sixth retro table is not retro-specific at all:
ph_reaction_count is a reaction aggregate shared between poker and retro.
It has a context_type discriminator (POKER or RETRO) and a
context_id that points, without a foreign key, to the room or the board.
The count is per (context, emoji), incremented with an UPSERT.
I chose a single shared aggregate instead of two twin tables
ph_poker_reaction and ph_retro_reaction because the floating
emoji reaction is the exact same mechanic in both contexts: a counter that goes up,
broadcast out, with no per-user granularity. Duplicating the table would have meant
duplicating the UPSERT handler and the broadcast code too. The cost of this choice is
the discriminator: every query must filter by context_type, and the
context_id is a soft reference without an FK, so cleaning up orphan
counts after a board deletion is the application's responsibility. It is the
classic trade between a more generic model and a weaker referential constraint: here
genericity won because privacy by design (no per-user emoji vote) makes the table
deliberately poor in relations.
STOMP and the WebSocket's real role
The real-time broadcast uses STOMP over WebSocket: the server publishes state deltas
(new card, vote reveal, phase advance, reaction) on topics the clients are subscribed
to. This is where the slice closes: a phase UPDATE on the database
becomes a message on a topic, and the frontend companion-tools UI redraws the board
with no polling.
But the WebSocket, I repeat, is the easy part. STOMP is a simple, well-documented messaging protocol; Spring integrates it with a few lines of configuration. The real work was done by the upstream schema decisions: which fields not to project in the payload (author, pre-reveal card_value, voter), which aggregate to share, which FSM to prevent from regressing. If the schema is right, the broadcast is almost boring: it pushes out exactly what is already safe to push. If the schema is wrong, no real-time channel saves you: you would broadcast the wrong data to everyone, quickly.
Who this read is for, and who it is not
If you are building a collaborative tool (a board, a whiteboard, a live vote) and you think the challenge is the transport, this slice shifts your center of gravity: almost every hard requirement (anonymity, vote confidentiality, defensible record, budget) is solved in the aggregate design and in the separation between what the database validates and what the application decides. If instead you are looking for a drop-in real-time collaboration library, this is not it: it is the map of how the schema holds up the UI, not a package to install.
Frequently asked questions
Why does the retro FSM not allow going back to a previous phase?
Because regressing from GROUP_VOTE to BRAINSTORM would reopen card writing while votes are already tallied, making the final record indefensible. The phase value is a VARCHAR with a CHECK on the four legal tokens on the database, but the non-regression rule lives in the application layer before the UPDATE: the database validates the shape, the application guarantees the monotonic sequence.
How does a card stay anonymous if the server stores author_user_id?
Anonymity is a property of the outbound channel, not of the row. The author_user_id column exists only for own-card auth and for facilitator moderation, but it is never projected into the STOMP deltas or the API responses toward the other members. The same holds for voter_user_id in dot-votes, used only for the budget and to retract one's own vote.
Why is the dot-voting budget not a database constraint?
Because it is an aggregate rule, not a row rule. A CHECK sees a single tuple and cannot count how many vote rows already exist for that voter on that board. The database guarantees only per-target uniqueness with uk_ph_retro_vote and vote_budget greater than or equal to 1; counting the active votes and deciding whether to accept the insert is the application's job.
Why do poker and retro share the same reaction table?
Because the floating emoji reaction is the same mechanic in both contexts: a counter that goes up, broadcast out, with no per-user granularity. A single ph_reaction_count aggregate with a context_type discriminator (POKER or RETRO) avoids duplicating the UPSERT handler and the broadcast code. The cost is the context_id as a soft reference without a foreign key, so cleaning up orphan counts is the application's responsibility.
The same series also includes Case Study: Migration from Microservices to Modular Monolith (Quantified ROI).
Commenti
Caricamento commenti...
Sign in to comment