Cache Memory

How caches are organised — lines, tags, sets and associativity; mapping addresses; replacement and write policies; the 3 Cs of misses; coherence — with an interactive cache simulator.

Advanced 4 min readLesson 11 of 15 Interview Q&A

A cache is a small, fast memory that holds copies of recently used lines of main memory. On every access the hardware must answer quickly: "is this address in the cache, and if so where?" The answer depends on how the cache is organised.

Splitting an address

For a cache with 2ˢ sets and 2ᵇ-byte lines, an address is split into three fields:

10110100110100101101tagindex (set)offset
20-bit address, 16 sets, 16-byte lines
  • Offset (b bits) — which byte within the line.
  • Index (s bits) — which set the line must go in.
  • Tag (the rest) — stored alongside the line to identify which memory block is there.

Each line also has a valid bit, and a dirty bit for write-back caches.

Associativity

OrganisationA block can go inProsCons
Direct-mappedExactly one lineSimplest, fastest lookupConflict misses when hot blocks share an index
N-way set-associativeAny of N lines in its setFar fewer conflictsCompare N tags in parallel
Fully associativeAnywhereNo conflict missesExpensive; only for tiny caches (TLBs)

Real L1 caches are typically 8–12-way, L2 16-way, and L3 12–16-way.

Try it

Choose direct-mapped or 2-way set-associative, step through the access sequence, and watch hits, misses and evictions.

InteractiveCache: 8 lines × 16-byte blocks
tag0000index0000offset00000
setway 0
0tag 0
1—
2—
3—
4—
5—
6—
7—
Access 0x000 → MISS. Miss (compulsory) → load block into set 0, way 0. Hit rate so far: 0/1
1/12

The 3 Cs of misses

Miss typeCauseReduce it by
Compulsory (cold)First access to a blockBigger lines, prefetching
CapacityThe working set is larger than the cacheA bigger cache, better locality (tiling)
ConflictToo many blocks map to the same setHigher associativity, padding data

(A 4th C, coherence misses, appears with multiple cores.)

Replacement policies

When a set is full, which line is evicted?

  • LRU (least recently used) — best in general; exact LRU is costly for high associativity, so hardware uses pseudo-LRU (tree bits).
  • FIFO, random — cheap; random is surprisingly decent.
  • Modern LLCs use adaptive policies (RRIP) that resist scanning patterns, where a one-off sweep through data would otherwise flush everything useful.

Write policies

Write-through

Every write updates the cache and memory. Simple and memory is always up to date, but it generates heavy traffic, so it's usually paired with a write buffer.

Write-back

Writes update only the cache and mark the line dirty; memory is updated when the line is evicted. Much less traffic. Used by nearly all modern CPU caches.

On a write miss: write-allocate loads the line and then writes it (usual with write-back); no-write-allocate writes straight to memory.

Worked example

A 32 KB, 8-way cache with 64-byte lines:

  1. Lines: 32 KB / 64 B = 512 lines.
  2. Sets: 512 / 8 = 64 sets → 6 index bits.
  3. Offset: 64 B → 6 offset bits.
  4. With 48-bit addresses: tag = 48 − 6 − 6 = 36 bits.

Notice that index + offset = 12 bits = a 4 KB page offset. That's deliberate: the L1 can be indexed with the virtual address while the TLB translates the tag in parallel (VIPT caches).

Multicore: cache coherence

Each core has private L1/L2 caches. If two cores cache the same line and one writes, the other's copy becomes stale. Coherence protocols prevent that. The classic one is MESI:

StateMeaning
ModifiedOnly this cache has it, and it's dirty
ExclusiveOnly this cache has it, and it's clean
SharedSeveral caches may have clean copies
InvalidNot usable

A write to a Shared line first invalidates the other copies. Cores keep track through snooping or a directory.

Key takeaways
  • Address = tag | index | offset; the index selects a set, the tag identifies the block.
  • Associativity trades lookup cost for fewer conflict misses.
  • Misses: compulsory, capacity, conflict (and coherence).
  • Write-back + write-allocate is the norm; dirty lines are written on eviction.
  • MESI keeps per-core caches coherent; watch out for false sharing.
Interview questions
QGiven a 64 KB 4-way cache with 32-byte lines and 32-bit addresses, how many tag bits are there?medium

Lines = 64 KB / 32 B = 2048. Sets = 2048 / 4 = 512 → 9 index bits. Offset = 5 bits. Tag = 32 − 9 − 5 = 18 bits.

QWhat is false sharing and how do you fix it?hard

Independent variables written by different cores share a cache line, so coherence invalidations bounce the line between cores even though there's no real data sharing. Pad or align each thread's hot data to its own cache line, or accumulate locally and combine at the end.