The problem
Two requests both read "10 available." Both reserve 6. That's overselling — the classic e-commerce bug — and it's usually a database-locking problem wearing a business-logic costume. Most fixes either slow everything down with a heavy lock held across a network round-trip, or don't actually close the race condition.
The approach
Optimistic concurrency instead of a database lock: an atomic compare-and-swap on a versioned record, with retry on conflict. No lock held while waiting on the network.
The test that actually matters
50 threads race to reserve 1 unit each from a stock of 20. The result: exactly 20 successes, 30 "out of stock" — zero oversold, every single run, not just on a lucky one. That's the whole point of testing concurrency logic with actual concurrent threads instead of asserting on the function in isolation.
Built with Java, Spring Boot, and gRPC — with the domain logic and the RPC layer tested separately, so a bug in the gRPC plumbing can't hide behind a mock of the business logic, and vice versa. The gRPC layer itself is tested in-process (server and channel in the same JVM), which is the pattern grpc-java itself recommends — no real network port needed to verify the service contract.
A build error that only showed up in CI
protoc-gen-grpc-java generates code that references javax.annotation.Generated, which was removed from the JDK starting with Java 9. Fixed by adding javax.annotation-api as a compile-time dependency — found because CI uses a clean JDK, not because it was anticipated.
Stack
Java, Spring Boot, gRPC.
If overselling has ever cost you a refund, an apology email, or a customer, this is the pattern that makes it structurally impossible instead of "handled most of the time."

