We implement cache together
summary
Pure functional local cache implemented in Scala that returns effects (an IO-based API) and uses Ref (CAS-backed atomic state) wrapped around immutable Maps, then splits state into partitioned refs (one per shard) to remove a single-point contention. Get-or-load stores deferred load placeholders so concurrent misses attach to one in-flight load instead of duplicating work. Demo shows typical production patterns (10k hits with two misses saved ~200 ms); implementation is partial but working.
AI-written summary · about
chapters (13)
- 0:00Introduction to caching
- 2:40Disclaimer and goals
- 3:26Defining cache API
- 6:22Building a mutable cache
- 7:56Moving to functional cache
- 9:24Introducing Ref for shared state
- 11:56How Ref works under the hood
- 16:42Implementing getOrLoad method
- 18:35Performance drawbacks of single Ref
- 21:09Partitioning cache to reduce contention
- 27:00Problem with parallel cache loading
- 30:44Using deferred values to prevent duplicate loads
- 34:18Summary and further improvements
