Post Concurrent Balance Transfers Without Deadlocks or Double Posting

Last updated: November 29, 2026

Quick Overview

Implement a transfer(transfer_id, src, dst, amount) operation on an in-memory ledger that many threads call at once, guaranteeing no deadlock, no negative balances, no money created or lost, and no double posting when the same transfer id is delivered twice.

Stripe
Software Engineering Fundamentals
Software Engineer
Stripe
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

0

0 solved


Implement a transfer(transfer_id, src, dst, amount) operation on an in-memory ledger that many threads call at once, guaranteeing no deadlock, no negative balances, no money created or lost, and no double posting when the same transfer id is delivered twice.

Moving funds between balances is the heart of Stripe Treasury, Connect payouts, and the internal ledger, and interviewers use a transfer function to probe locking and transaction reasoning. They want to see lock ordering, atomic debit plus credit, invariant checks, and a clear story for how the same guarantees hold in a relational database.

What the Interviewer Expects
  • Identifies the A to B versus B to A deadlock and fixes it with a global lock order
  • Makes the debit, credit, and journal entries atomic under the locks
  • Rejects transfers that would overdraw the source and validates amount and accounts
  • Deduplicates on transfer id so redelivered messages are no-ops
  • States and tests the conservation invariant (sum of balances constant, journal sums to zero)
  • Translates the design to SQL: SELECT FOR UPDATE in id order, unique constraint on transfer id, serializable or row locks
Key Topics to Cover
lock ordering
deadlock avoidance
double-entry ledger
idempotent consumers
database transactions
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you implement this in Postgres and which isolation level do you need?
  • What changes if one hot platform account is the source of most transfers?
  • How would you support a transfer that spans currencies with an FX rate?
  • How do you test for deadlocks and lost updates rather than hoping they do not happen?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • Balances are integers in minor units (never floats).
  • transfer is atomic: either both sides move and two journal entries are written, or nothing happens.
  • No deadlock when threads transfer in op...
High-Level Design

One lock per account plus a small lock for the dedupe set. The naive version locks src then dst, so thread 1 (A to B) holds A while thread 2 (B to A) holds B, and both wait forever. Acquiring lock...


Submit Your Answer
Markdown supported