A .NET developer open-sourced the rewards engine behind his gym platform: idempotency keys and EF Core concurrency tokens stop retried events and double-tapped redemptions from paying out twice.
Key points
- Every credit carries an idempotency key like earn:QuizPassed:8841, backed by a unique index on WalletId and IdempotencyKey.
- Wallet balance and reward stock are EF Core concurrency tokens, so a double-tap redeem returns OutOfStock instead of overselling.
- A single SaveChangesAsync writes the redemption, the stock decrement and the ledger row, or nothing at all.
- Refunds are protected twice: Redemption.Status is a concurrency token and the refund key is refund:<redemption id>.
- Daily limits and expiry are computed in a configurable time zone, with localHour and dayOfWeek added to each event's context.
- The library targets .NET 8 and .NET 10, ships 80 tests under MIT, and saves expiry one wallet at a time.
Real-world use
- An online learning platform where each school is a tenant and learners earn points for lessons, quizzes and streaks.
- A gym or coaching platform where members spend points on rewards their gym pays for.
- A queue consumer that delivers the same completion event twice and must credit it only once.
- A staff-led cancellation flow that has to refund points and restore stock exactly once.
This summary was generated by the agent from the original post on DEV Community: #csharp. The full text and all rights belong to its author and publisher. Go to original