The Real Fullstack

The Racy Cache Map

The cache was supposed to cut calls to the pricing service. Why did they triple?

مهندس8 دقيقةمختبر تصحيح الأخطاءتحدي الشيفرة
javaconcurrenthashmapgopython

لم يُترجم هذا التحدي إلى العربية بعد. أنت تقرأ النسخة الإنجليزية.

المهمة:Find the real bug behind the duplicate loads, then make the cache correct under concurrency, cheap under contention and bounded in memory.السياق · أهداف التعلّم ▾

Hollow Basket wrapped its pricing client in a small in-memory cache built on a HashMap. Since Black Friday traffic arrived, the pricing service gets several identical calls in the same millisecond, the map occasionally loses entries, and memory keeps growing until the next deploy.

المهمة: Find the real bug behind the duplicate loads, then make the cache correct under concurrency, cheap under contention and bounded in memory.

أهداف التعلّم

  • Recognise check-then-act on shared state as a race.
  • Use atomic per-key operations instead of one global lock.
  • Bound in-memory caches instead of growing the heap.
المحاكاةمحاكاة
request thread Arequest thread Bprice cachepricing service
تشغيل المحاكاة
private final Map<String, Price> cache = new ConcurrentHashMap<>();Price get(String sku) {  // atomic per key: one loader per missing sku, other threads wait for that value  return cache.computeIfAbsent(sku, pricingClient::load);}// bounded: use a cache library with maximumSize + expiry instead of an unbounded map

تبسيط

Three symptoms, each resolved or merely hidden by specific changes. Real caches also need expiry, error handling and metrics. نموذج تعليمي — الأرقام توضّح سلوكًا وليست قياسات أداء.

المفاهيم

مفيد قبل هذا التحدي

تحديات ذات صلة

المراجع