The Real Fullstack

The Dependency Injection Trap

Nobody wrote code that mixes two users' carts. Why does Alice see Bob's total at lunchtime?

エンジニア8 分デバッグラボコード課題
javaspring-bootangulartypescript

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Find the root cause in the logs and code, then fix it without hurting throughput or leaking data in a different way.背景 · 学習目標 ▾

Larkspur's cart pricing service is a Spring bean. Under load, a few customers report totals that don't match their cart. Logs show two requests for different users handled a few milliseconds apart by different threads — and the wrong total returned.

ミッション: Find the root cause in the logs and code, then fix it without hurting throughput or leaking data in a different way.

学習目標

  • Know the default scope of Spring beans and Angular root services.
  • Keep singletons stateless.
  • Spot fixes that only hide shared-state bugs.
シミュレーションシミュレーション値
AliceBobCartPricingServiceCarts DB
シミュレーションを実行
@Service                                   // singleton: ONE instance for all requestspublic class CartPricingService {  private Cart currentCart;                 // per-request data in a shared field  public Money total(String userId) {    this.currentCart = carts.load(userId);  // request B can overwrite this …    applyPromotions();    return sum(this.currentCart);           // … before request A reads it  }}

簡略化

One symptom; each fix maps to its effects. In real systems this bug is also a data-protection incident. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

先に取り組むと役立つ課題

関連する課題

参考資料