L1 vs L2 vs L3 cache

Get FREE domain for 1st year and build your brand new site

Free book on Graph Algorithms

In this article, we have covered the differences between L1 cache, L2 cache and L3 cache in depth. We have explained each point in depth with examples.

Table of contents:

  1. Background on Cache
  2. Differences between L1, L2 and L3 cache
    • Size of L1, L2, L3 Cache
    • Speed of Cache
    • Data and Instruction Cache
    • Sharing of Cache
  3. Conclusion + Summary Table

Background on Cache

There are 3 types of cache that is available in a system. They are:

  • L1 cache
  • L2 cache
  • L3 cache

L1 is the first level cache. L2 cache is the second level cache while l3 cache is the third level cache.

Differences between L1, L2 and L3 cache

The differences between L1, L2 and L3 cache are:

  • Size of L1, L2, L3 Cache: L3 > L2 > L1
  • Speed of Cache: L1 > L2 > L3
  • Data and Instruction Cache
  • Sharing of Cache

Size of L1, L2, L3 Cache

Based on size, L3 cache is the largest followed by L2 cache with L1 cache being the smallest.

L1 cache < L2 cache < L3 cache

More the size of cache, the better the performance of the system. That being said, it is not possible to simply increase the size of cache as it may degrade the performance of the cache itself.

Usually, the size of L1, L2 and L3 cache are as follows:

  • L1 cache: 32KB
  • L2 cache: 128KB
  • L3 cache: 8MB

As you can see, in most cases, L3 cache is significantly larger than L1 and L2 cache combined. L2 cache is usually 2 to 4 times bigger than L1 cache.

Speed of Cache: L1 > L2 > L3

As L1 cache is closest to the system, it is the fastest. Additionally, the size of L1 cache is the smallest so data access time is minimum.

Therefore, the speed of L1 cache is the fastest.

L2 cache is the second level of cache and hence, it is slower than L1 cache but faster than L3 cache. L3 cache is the third level of cache and hence,

Data and Instruction Cache

Cache can be used to store two types of information:

  • Instructions
  • Data

L1 cache stores both data and instruction. Therefore, L1 cache is divided into two components:

  • L1d: L1 cache for data
  • L1i: L1 cache for instruction

On the other hand, L2 and L3 cache store only data. These caches do not store instructions. Hence, whenever, L2 and L3 cache is mentioned, it refers to Data Cache but when L1 cache is mentioned, it should be clarified if it is data or instruction cache.

Sharing of Cache

A system has multiple CPU cores. A standard server has around 128 CPU cores today. Each process can choose which core to run on and we can control this as well.

Each CPU core has separate L1 and L2 cache.

Every CPU core in a cluster share the same L3 cache. Hence, L3 cache is the only shared cache while L1 and L2 cache are exclusive.

The details of which cache is linked to which CPU core can be found using commands like lstopo or lstopo-no-graphics.

Conclusion + Summary Table

Following table summarizes the differences between L1, L2 and L3 cache:

L1 vs L2 vs L3 cache
Differences between L1, L2 and L3 cache
SpeedL1 > L2 > L3
SizeL3 > L2 > L1
SharingL3 is shared among CPU cores
L1, L2 is not shared.
Data, Instruction cacheL1 is both data and instruction.
L2, L3 is only data cache
LocationL1 is in CPU core
L2: in chip, outside CPU core
L3 outside chip

With this article at OpenGenus, you must have the complete idea of the differences between the three types of cache: L1, L2 and L3 cache.