Rebuild Membership Billing State From Out Of Order Events
Last updated: November 29, 2026
Quick Overview
Membership events (subscribe, change_tier, pause, resume, payment_failed, payment_succeeded, cancel) arrive with per-membership sequence numbers but can be duplicated or out of order. Build a projector that applies them strictly in sequence, buffers gaps, rejects invalid transitions, and exposes each membership's current status and tier.
Patreon
November 29, 20260
0
0 solved
Membership events (subscribe, change_tier, pause, resume, payment_failed, payment_succeeded, cancel) arrive with per-membership sequence numbers but can be duplicated or out of order. Build a projector that applies them strictly in sequence, buffers gaps, rejects invalid transitions, and exposes each membership's current status and tier.
Patreon's billing system has a state machine that now has to support pauses, gifts and comped memberships alongside paid ones, and events from processors and app stores rarely arrive neatly. The interviewer checks state machine design, idempotent ingestion, gap buffering, and what you do with events that are valid in sequence but illegal for the current state.
What the Interviewer Expects
- Encodes the state machine as an explicit transition table rather than nested ifs
- Drops duplicates by sequence number so redelivery is idempotent
- Buffers events past a gap and drains the contiguous prefix once the gap fills
- Consumes but records invalid transitions instead of blocking the stream forever
- Exposes whether a membership is waiting on a missing sequence
- Discusses persistence of the cursor and pending buffer
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- A sequence number never arrives. How long do you wait, and what do you do then?
- How would you support concurrent states, such as a paid membership that is also partially gift-covered?
- How do you answer 'what was the status at time T' for a dispute or audit?
- How would you shard this across workers while keeping per-membership ordering?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Per membership, sequence numbers start at 1 and are dense. Events can be redelivered or arrive early. Output after each ingest: the seqs that got applied. Query: current status, tier, and whether we a...
High-Level Design
Each membership holds status, tier, next_seq and a pending map seq -> event. Ingest drops anything below next_seq or already pending (duplicate), stores the event, then drains while `next_...