Explain caching and strategies.
Skip to solutionKEEP THE
mediumSystem Design
How does caching improve system performance?
187 views
01
Understand the problem
caching
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
How Does Caching Improve System Performance?
Target Audience: Junior & Senior Software Engineers preparing for System Design Interviews — no prior system design knowledge assumed. Difficulty: Easy to Medium
How to read this doc: Every concept is explained in plain language first. Right after, you'll s
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
LRU cache with eviction
Run Playgroundclass LRUCache {
constructor(capacity) {
this.capacity = capacity;
this.cache = new Map(); // key -> value
this.order = []; // most-recently-used at the end
}
get(key) {
if (!this.cache.has(key)) return -1;
this.order.splice(this.order.indexOf(key), 1);
this.order.push(key);
return this.cache.get(key);
}
put(key, value) {
if (this.cache.has(key)) {
this.order.splice(this.order.indexOf(key), 1);
} else if (this.cache.size >= this.capacity) {
const lruKey = this.order.shift();
this.cache.delete(lruKey);
}
this.cache.set(key, value);
this.order.push(key);
}
}
const cache = new LRUCache(2);
cache.put('a', 1);
cache.put('b', 2);
console.log('get(a):', cache.get('a'));
cache.put('c', 3);
console.log('get(b):', cache.get('b'));
console.log('get(a):', cache.get('a'));
console.log('get(c):', cache.get('c'));Cache-aside read
Run Playgroundcache = {} # stand-in for Redis/Memcached
def load_from_db(key):
print(" DB read for", key)
return key.upper() + "-value"
def get(key):
if key in cache: # 1. check cache
return cache[key] # 2a. hit
value = load_from_db(key) # 2b. miss -> load
cache[key] = value # 3. populate
return value
print(get("user:1")) # miss -> DB
print(get("user:1")) # hit -> no DB read
print(get("user:2")) # miss -> DB05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 39 of 99 decoded in the System Design track. One more won't hurt.