TExcelTAD
Home › Online Algorithms & Competitive Analysis › Resource Augmentation: k Pages Against h

Resource Augmentation: k Pages Against h

The lower bound kk compares an online cache with an offline optimum that has the same cache size. That comparison is harsh: knowing the future is worth a lot. A gentler and often more informative question is: how much more memory does an online algorithm need to compete with OPT? Sleator and Tarjan already asked it in their 1985 paper. With kk slots online against OPT with h≤kh \le k slots, the answer is the ratio kk−h+1\frac{k}{k - h + 1}: double the cache and LRU is within a factor 2 of the optimal smaller cache. This idea, comparing against a weaker optimum, is called resource augmentation, and it became a general method in scheduling and beyond (Kalyanasundaram and Pruhs, 2000).

The upper bound

Write OPTh\mathrm{OPT}_h for the optimal offline cost with hh slots and ALGk\mathrm{ALG}_k for the online algorithm with kk slots.

Theorem (Sleator and Tarjan, 1985). Every marking or conservative algorithm (LRU, FIFO, CLOCK, FWF) satisfies, for every h≤kh \le k and every σ\sigma, with the same initial cache contents,

ALGk(σ)≤kk−h+1⋅OPTh(σ)+k.\mathrm{ALG}_k(\sigma) \le \frac{k}{k - h + 1} \cdot \mathrm{OPT}_h(\sigma) + k .
Proof

Use the kk-phases of σ\sigma from LRU and FIFO Are k-Competitive: each phase has kk distinct pages, except perhaps the last. As before, ALGk≤k\mathrm{ALG}_k \le k misses per phase, so ALGk≤km\mathrm{ALG}_k \le km for mm phases.

For phase i<mi < m, let pp be its first request and let the window WiW_i run from the second request of phase ii to the first request of phase i+1i + 1. The window requests at least kk distinct pages other than pp. After serving pp, OPTh\mathrm{OPT}_h holds pp and at most h−1h - 1 other pages, so at least k−(h−1)=k−h+1k - (h - 1) = k - h + 1 of those kk pages are missing and each causes a miss when first requested in the window. Windows are disjoint, so OPTh≥(m−1)(k−h+1)\mathrm{OPT}_h \ge (m - 1)(k - h + 1) and

ALGk≤km=kk−h+1 (m−1)(k−h+1)+k≤kk−h+1 OPTh+k.\mathrm{ALG}_k \le km = \frac{k}{k - h + 1}\,(m - 1)(k - h + 1) + k \le \frac{k}{k - h + 1}\,\mathrm{OPT}_h + k .

For h=kh = k this is the familiar k OPT+kk\,\mathrm{OPT} + k.

The matching lower bound

Theorem (Sleator and Tarjan, 1985). For every deterministic online algorithm with kk slots and every h≤kh \le k, there are arbitrarily long sequences with ALGk(σ)≥kk−h+1 OPTh(σ)−O(k)\mathrm{ALG}_k(\sigma) \ge \frac{k}{k - h + 1}\,\mathrm{OPT}_h(\sigma) - O(k).

The adversary is the cruel one from the lower bound for deterministic paging: with k+1k + 1 pages it always requests the page missing from the online cache, so the online algorithm misses on every request. The work is in showing that OPTh\mathrm{OPT}_h misses on at most a k−h+1k\frac{k - h + 1}{k} fraction of such a sequence (up to an additive term); we do not reproduce that part of the proof and check it numerically instead. The program generates the cruel sequence for LRU (which is simply the cycle 1,2,…,k+11, 2, \ldots, k + 1 repeated) and for FIFO and a hashed rule, then computes OPTh\mathrm{OPT}_h with Belady's rule for every hh.

python · try it yourself
def opt_misses(reqs, h):
    n = len(reqs)
    nxt, seen = [0] * n, {}
    for t in range(n - 1, -1, -1):
        nxt[t] = seen.get(reqs[t], n + reqs[t])
        seen[reqs[t]] = t
    cache, misses = {}, 0
    for t, p in enumerate(reqs):
        if p not in cache:
            misses += 1
            if len(cache) == h:
                del cache[max(cache, key=cache.get)]
        cache[p] = nxt[t]
    return misses

def cruel(k, n, evict):
    cache, loaded, reqs = [], {}, []
    for t in range(n):
        page = next(p for p in range(k + 1) if p not in cache)
        reqs.append(page)
        if len(cache) == k:
            cache.remove(evict(cache, loaded, t))
        cache.append(page)
        loaded[page] = t
    return reqs

rules = {
    "LRU/FIFO": lambda c, loaded, t: min(c, key=lambda p: loaded[p]),   # every request misses, so LRU = FIFO
    "hashed": lambda c, loaded, t: sorted(c)[(t * 40503) % 97 % len(c)],
}
k, n = 8, 4000
print(f"k = {k}, n = {n} requests, the online algorithm misses on all of them")
print(" h   OPT_h   n/OPT_h (LRU/FIFO)   n/OPT_h (hashed)   k/(k-h+1)")
for h in [1, 2, 4, 6, 7, 8]:
    ratios = [n / opt_misses(cruel(k, n, rule), h) for rule in rules.values()]
    print(f"{h:2}  {opt_misses(cruel(k, n, rules['LRU/FIFO']), h):6}   {ratios[0]:18.3f}   {ratios[1]:16.3f}   {k / (k - h + 1):9.3f}")
k = 8, n = 4000 requests, the online algorithm misses on all of them
 h   OPT_h   n/OPT_h (LRU/FIFO)   n/OPT_h (hashed)   k/(k-h+1)
 1    4000                1.000              1.000       1.000
 2    3501                1.143              1.729       1.143
 4    2503                1.598              3.571       1.600
 6    1505                2.658              6.182       2.667
 7    1006                3.976              9.501       4.000
 8     507                7.890             18.605       8.000

For every hh the ratio against LRU and FIFO matches kk−h+1\frac{k}{k - h + 1} up to start-up effects; the hashed rule does worse, as non-marking rules may.

Reading the bound

The function kk−h+1\frac{k}{k-h+1} falls quickly as the online cache grows:

python · try it yourself
from math import ceil

print("  h   k = h: ratio   k = 2h: ratio   k for ratio <= 1.1   k for ratio <= 1.01")
for h in [4, 16, 64, 256, 1024]:
    r1 = h / 1
    r2 = 2 * h / (h + 1)
    k11 = ceil(1.1 * (h - 1) / 0.1)        # k/(k-h+1) <= 1 + eps  <=>  k >= (1 + eps)(h - 1)/eps
    k101 = ceil(1.01 * (h - 1) / 0.01)
    print(f"{h:4}   {r1:12.0f}   {r2:13.3f}   {k11:18}   {k101:19}")
  h   k = h: ratio   k = 2h: ratio   k for ratio <= 1.1   k for ratio <= 1.01
   4              4           1.600                   33                   303
  16             16           1.882                  165                  1515
  64             64           1.969                  694                  6363
 256            256           1.992                 2805                 25755
1024           1024           1.998                11254                103323

With k=hk = h the guarantee is the useless factor hh; with k=2hk = 2h it is below 2 for every hh. Getting within 10 percent of OPT requires about 11h11h slots, and within 1 percent about 101h101h: the bound is still pessimistic, since real traces with locality behave much better, but it explains why a modest amount of extra memory removes the worst-case gap between online and offline caching.

In C++

LRU with kk slots against Belady with hh slots on a seeded trace with locality, for k=2hk = 2h. The measured ratio is far below the worst-case bound 2hh+1\frac{2h}{h+1}, and for small caches LRU with twice the memory even beats the smaller optimal cache.

cpp · try it yourself
#include <cstdio>
#include <list>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

int lru(const std::vector<int>& r, int k) {
    std::list<int> order;
    std::unordered_map<int, std::list<int>::iterator> pos;
    int misses = 0;
    for (int p : r) {
        auto it = pos.find(p);
        if (it != pos.end()) order.erase(it->second);
        else { ++misses; if ((int)pos.size() == k) { pos.erase(order.back()); order.pop_back(); } }
        order.push_front(p);
        pos[p] = order.begin();
    }
    return misses;
}

int belady(const std::vector<int>& r, int h) {
    int n = r.size(), misses = 0;
    std::vector<int> nxt(n);
    std::unordered_map<int, int> seen;
    for (int t = n - 1; t >= 0; --t) { nxt[t] = seen.count(r[t]) ? seen[r[t]] : n + r[t]; seen[r[t]] = t; }
    std::set<std::pair<int, int>> s;
    std::unordered_map<int, int> key;
    for (int t = 0; t < n; ++t) {
        if (key.count(r[t])) s.erase({key[r[t]], r[t]});
        else { ++misses; if ((int)key.size() == h) { auto f = std::prev(s.end()); key.erase(f->second); s.erase(f); } }
        key[r[t]] = nxt[t];
        s.insert({nxt[t], r[t]});
    }
    return misses;
}

int main() {
    std::mt19937 rng(31);
    std::vector<int> r;
    int page = 0;
    for (int t = 0; t < 50000; ++t) {                         // a random walk with occasional jumps
        unsigned x = rng() % 100;
        if (x < 3) page = rng() % 200;
        else page = (page + (x % 5) - 2 + 200) % 200;
        r.push_back(page);
    }
    std::printf("   h   OPT_h   LRU_2h   LRU_2h/OPT_h   bound 2h/(h+1)\n");
    for (int h : {2, 4, 8, 16, 32}) {
        int o = belady(r, h), l = lru(r, 2 * h);
        std::printf("%4d  %6d  %7d   %12.3f   %14.3f\n", h, o, l, double(l) / o, 2.0 * h / (h + 1));
    }
}
   h   OPT_h   LRU_2h   LRU_2h/OPT_h   bound 2h/(h+1)
   2   27180    22096          0.813            1.333
   4   18897    16124          0.853            1.600
   8   14384    14001          0.973            1.778
  16   11568    12601          1.089            1.882
  32    8802    10069          1.144            1.939

In practice: sizing a cache

System designers rarely ask "how close is LRU to OPT for a fixed cache?". They ask "how large must the cache be for a target miss rate?", and they answer it with a miss-ratio curve: the miss rate of LRU as a function of the cache size, which for LRU (a stack algorithm) can be computed for all sizes in one pass over a trace (Mattson, Gecsei, Slutz and Traiger, 1970). Resource augmentation is the worst-case version of the same question: it compares the online curve at size kk with the optimal curve at size hh. The C++ experiment above shows the typical picture: on a trace with locality the online curve at 2h2h stays close to the offline curve at hh (within 15 percent in that experiment, and below it for small caches), far better than the worst-case factor, and the worst case only appears on loops slightly larger than the cache.

Beyond paging

Resource augmentation compares an online algorithm with a handicapped optimum: a smaller cache, slower machines, or fewer servers. It explains why simple heuristics work well in practice despite bad competitive ratios, and it is the standard analysis tool for online scheduling with speed augmentation (Kalyanasundaram and Pruhs, 2000; Phillips, Stein, Torng and Wein, 2002). For randomized paging the effect is similar: Young (1991) showed that the marking algorithm with kk slots is roughly 2ln⁡kk−h2\ln\frac{k}{k-h}-competitive against OPTh\mathrm{OPT}_h for h<kh < k, and that no randomized algorithm beats about ln⁡kk−h\ln\frac{k}{k-h}.

Test yourself

LRU has k=10k = 10 slots, OPT has h=6h = 6. What competitive ratio does the theorem give?

  1. 1010
  2. 22
  3. 10/610/6
  4. 6/56/5

In the proof, how many misses does OPTh\mathrm{OPT}_h make at least in each window?

  1. 1
  2. hh
  3. k−h+1k - h + 1
  4. kk

Why is it fair to say that doubling the cache makes LRU nearly optimal in the worst case?

  1. With k=2hk = 2h, LRU is 2hh+1<2\frac{2h}{h+1} < 2-competitive against the optimum with hh slots
  2. With k=2hk = 2h, LRU equals OPT
  3. Doubling the cache halves the misses
  4. LRU becomes randomized