



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS787: Advanced Algorithms Scribe: Dalibor Zeleny and Xiaoyong Chai Lecturer: Shuchi Chawla Topic: Caching Algorithms Date: October 31 2007
Today we discussed the issue of caching—a problem that is well suited for the use of online algo- rithms. We talked about deterministic and randomized caching algorithms, and proved bounds on their competitive ratios.
In today’s computers, we have to make tradeoffs between the amount of memory we have and the speed at which we can access it. We solve the problem by having a larger but slower main memory. Whenever we need data from some page of the main memory, we must bring it into a smaller section of fast memory called the cache. It may happen that the memory page we request is already in the cache. If this is the case, we say that we have a cache hit. Otherwise we have a cache miss, and we must go in the main memory to bring the requested page. It may happen that the cache is full when we do that, so it is necessary to evict some other page of memory from the cache and replace it with the page we read from main memory, or we can choose not to place the page we brought from main memory in the cache. Cache misses slow down programs because the program cannot continue executing until the requested page is fetched from the main memory. Managing the cache in a good way is, therefore, necessary in order for programs to run fast. The goal of a caching algorithm is to evict pages from the cache in a way that minimizes the number of cache misses. A similar problem, called paging, arises when we bring pages from a hard drive to the main memory. In this case, we can view main memory as a cache for the hard drive.
For the rest of this lecture, assume that the cache has a capacity of k pages and that main memory has a capacity of N pages. For example consider a cache with k = 3 pages and a main memory with N = 5 pages. A program could request page 4 from main memory, then page 1, then 2 etc. We call the sequence of memory pages requested by the program the request sequence. One request sequence could be 4, 1, 2, 1, 5, 3, 4, 4, 1, 2, 3 in our case. Initially, the cache could contain pages 1, 2, and 3. When we execute this program, we need access to page number 4. Since it’s not in the cache, we have a cache miss. We could evict page 2 from the cache and replace it with page 4. Next, our program needs to access page 1, which is in the cache. Hence, we have a cache hit. Now our program needs page 2, which is not in the cache, so we have a miss. We could choose to evict page 1 from the cache and put page 2 in its place. Next, the program requests page 1, so we have another miss. This time we could decide to just read page 1 and not place it in the cache at all. We continue this way until all the memory requests are processed.
In general, we don’t know what the next terms in the request sequence are going to be. Thus, caching is a place where we can try to apply an online algorithm. For a request sequence σ and a given algorithm ALG, we call ALG(σ) the cost of the algorithm ALG on request sequence σ, and define it to be the number of cache misses that happen when ALG processes the memory requests and maintains the cache.
If we knew the entire request sequence before we started processing the memory requests, we could use a greedy algorithm that minimizes the number of cache misses that occur. Whenever we have a cache miss, we go to main memory to fetch the memory page p we need. Then we look at this memory page and all the memory pages in the cache. We evict the page for which the next request occurs the latest in the future from among all the pages currently in the cache, and replace that page with p in the cache. If the next time p is requested comes after the next time all the pages in the cache are requested again, we don’t put p in the cache.
Once again, take our sample request sequence 4, 1, 2, 1, 5, 3, 4, 4, 1, 2, 3 and assume that pages 1 through 3 are in the cache. The optimal algorithm has a cache miss when page 4 is requested. Since page 4 is requested again later than pages 1 through 3 are, the algorithm doesn’t put 4 in the cache and doesn’t evict any page. The next miss occurs when page 5 is requested. Once again, the next times pages 1 through 3 are requested occur before page 5 is requested the next time, so page 5 is not brought in the cache. The next cache miss happens when page 4 is requested. At that point, page 3 gets evicted from the cache and is replaced with page 4 because pages 1, 2 and 4 get requested again before page 3 does. Finally, the last cache miss occurs when page 3 is requested (last term in the request sequence). At this point, the algorithm could choose to evict page 1 and put page 3 in the cache instead. Thus, the optimal algorithm has 4 cache misses on this request sequence.
Notice that in the case of caching, we have an optimal algorithm assuming that we know the entire input (in our case the input is the request sequence). Last lecture, we discussed the list update problem. We do not have an algorithm that runs in polynomial time and can find an optimal solution to the list update problem, even when it has access to the entire input at the very beginning. This fact makes the analysis of online algorithms for caching easier because we know what algorithm we compare the online algorithms against.
For the remainder of this lecture, we will assume that a caching algorithm always has to bring the memory page in the cache on a cache miss, and doesn’t have the option of just looking at it and not putting it in the cache, as it will make our proofs simpler. This version is also much closer to what happens in hardware.
There are many deterministic online algorithms for caching. We give some examples below. In each of the cases, when a cache miss occurs, the new memory page is brought into the cache. The name of the algorithm suggests which page should be evicted from the cache if the cache is full.
LRU (Least Recently Used) The page that has been in the cache for the longest time without being used gets evicted.
FIFO (First In First Out) The cache works like a queue. We evict the page that’s at the head of the queue and then enqueue the new page that was brought into the cache.
at least k memory requests. It follows that for every cache miss the optimal algorithm makes, ALG makes at least k cache misses, which means that the competitive ratio of ALG is at least k.
Claim 20.3.2 LRU has a competitive ratio of k.
Proof: First, we divide the request sequence σ into phases as follows:
As an example, suppose σ = 4, 1 , 2 , 1 , 5 , 3 , 4 , 4 , 1 , 2 , 3 and k = 3. We divide σ into three phases as in Figure 20.3.1 below. Phase 2 begins at page 5 since page 5 is the third distinct page after phase 1 began (pages 1 and 2 are the first and second distinct pages, respectively).
Figure 20.3.1: Three phases of sequence σ.
Next, we show that OPT makes at least one cache miss each time a new phase begins. Denote the j-th distinct page in phase i as pij. Consider pages pi 2 - pik and page pi 1 +1. These are k distinct pages by the definition of a phase. Then if none of the pages pi 2 - pik incur a cache miss, pi 1 +1 must incur one. This is because pages pi 2 - pik and pi 1 +1 are k distinct pages, page pi 1 is in the cache, and only k pages can reside in the cache. Let N be the number of phases. Then we have OPT(σ) ≥ N − 1. On the other hand, LRU makes at most k misses per phase. Thus LRU(σ) ≤ kN. As a result, LRU has a competitive ratio of k.
This proof relies on the fact that on a page fault, OPT (and any other caching algorithm) has to bring the page from memory into the cache, and evict another page if the cache is full. If this were not the case, we could only demonstrate a competitve ratio of 2k. It is also possible to show that this bound on the competitive ratio is tight.
As a variant of the above LRU algorithm, the 1-bit LRU algorithm (also known as the marking algorithm) associates each page in the cache with 1 bit. If a cache page is recently used, the corresponding bit value is 1 (marked); otherwise, the bit value is 0 (unmarked). The algorithm works as follows:
Following the proof of Claim 20.3.2, we can easily see that the above deterministic 1-bit LRU algorithm also has a competitive ratio of k.
Given a deterministic LRU algorithm, an adversary can come up with a worst-case scenario by always requesting the page which was just evicted. As a way to improve the competitive ratio, we consider a randomized 1-bit LRU algorithm in which a page is evicted randomly. Before we go into the algorithm, we shall define the competitive ratio for a randomized online algorithm.
Definition 20.4.1 A randomized online algorithm ALG has a competitive ratio r if for ∀σ,
E[ALG(σ)] ≤ r · OPT(σ) + c,
where c is a constant independent of σ.
Note that the competitive ratio of a randomized algorithm is defined over the expected performance of the algorithm rather than the worst-case performance.
The randomized 1-bit LRU algorithm is rather simple: at every step, run the 1-bit LRU algorithm and evict an unmarked page uniformly at random when necessary.
Claim 20.4.2 The randomized 1-bit LRU algorithm has a competitive ratio of 2 Hk, where Hk is the k-th harmonic number, which is defined as
Hk = 1 +
k
∈ (log k, log(k) + 1).
Proof: Let us first analyze the expected number of cache misses in phase i. Recall that at the end of phase i − 1, there are k pages in the cache. Let mi denote the number of “new” pages in phase i, i.e., those pages that were not requested in phase i − 1. Call the rest of the pages in phase i “old” pages. Requesting a new page causes an eviction while requesting an old page may not. Assume for simplicity that the mi new pages appear the first in the request sequence of phase i. Actually, one should convince oneself that this assumption indicates the worst-case scenario (think about how the number of cache misses changes if any one of these new page requests is delayed). Once the mi new pages are requested, they are marked in the cache. The remaining k − mi pages in the cache are old pages. Now let us consider the probability of a cache miss when we request an old page for the first time, i.e., when we request the (mi + 1)-th distinct page in phase i. This old page by definition was in the cache at the end of phase i − 1. When requested, however, it may not be in the cache since it can be evicted for caching one of the mi new pages. Then,
P r[(mi + 1)-th distinct page suffers a cache miss] =
( (^) k− 1 k−mi
( (^) k k−mi
mi k