XFetch cache refresh decider

You build a pure XFetch refresh decision function that uses expiry time, recomputation duration, beta and an injected random source to decide whether a cache entry should refresh early.

Ticket

Build an XFetch refresh decision function in backend/internal/cache/xfetch.go. It decides whether a cache reader should refresh an entry before its expiry using the current time, expiry time, measured recomputation duration, beta and an injected random source.

A popular menu entry can cause a Thundering herd when many requests attempt to rebuild it at the same expiry instant. XFetch lets readers spread refresh decisions before expiry, based on the cost of rebuilding the entry. This optional component complements the existing cache-aside implementation. No later required exercise depends on it.

The tests call this exported function directly:

go
func ShouldRefresh(
    now, expiry time.Time,
    recompute time.Duration,
    beta float64,
    rnd func() float64,
) bool

Acceptance criteria

  • One minute before expiry, a recomputation duration of 100*time.Millisecond, beta 1 and random value 0.5 must not trigger a refresh.
  • At the expiry instant, the function returns true. It also returns true after expiry, even when the recomputation duration is zero.
  • With now two seconds before expiry and a one-second recomputation duration, random value math.Exp(-3) triggers an early refresh, while math.Exp(-1) does not.
  • With now one nanosecond before expiry, a one-hour recomputation duration, beta 1 and random value 1, the function does not refresh early.
  • With now three seconds before expiry, a one-second recomputation duration and random value math.Exp(-1), beta 1 does not trigger a refresh, while beta 4 does.
  • With now one millisecond before expiry, beta 0 or recomputation duration 0 disables early refresh, even with random value 0.0001.
  • A random value of 0, with now one hour before expiry, a one-second recomputation duration and beta 1, must be handled without duration overflow or a panic.
  • Across 1000 random values from 0.001 through 1, the number of refresh decisions 500 milliseconds before expiry must exceed the number five seconds before expiry.

Constraints

  • Preserve the exact function signature shown in the ticket.
  • Accept randomness through the rnd function argument. Do not use a global random generator.
  • Do not read the system clock inside the function. Use the supplied now and expiry.
  • Keep the function pure. Do not introduce shared mutable state, background goroutines, network calls or cache storage operations.
  • Do not modify the existing tests or add sleeps to test time-dependent behaviour.

Design direction

Keep the implementation focused on the decision function in the existing cache package. Separate the expiry check, configuration checks, random sampling and early-window calculation so each decision has a clear boundary.

Use the XFetch rule:

now - recompute * beta * ln(rnd()) >= expiry

Equivalently, calculate:

earlyWindow = recomputeSeconds * beta * -ln(rnd())

Then determine whether adding that window to now reaches expiry.

A smaller random value produces a larger early-refresh window. A larger beta also increases the window for the same recomputation duration and random value. A random value of 1 produces a zero window.

Handle expiry before checking the early-refresh configuration. A request at or after expiry must refresh regardless of beta or recomputation duration. For earlier requests, zero or negative recomputation duration and beta disable early refresh.

Calculate the window in floating-point seconds. Handle zero random values before taking the logarithm, and bound extreme results before converting them into a duration. Any layout that meets the contract passes.

How to check your work

Run the exercise-specific tests and the race-enabled tests:

bash
cd backend
make test W=6O3
go test -race -tags w6o3 ./internal/cache/ -run W6O3

Read the first failing assertion to identify whether the problem is in expiry handling, the random-value calculation, beta scaling or numerical safety.

Finished Exercise 6.17?

Runtime and performance extensions

3 items

  • 6.15Runtime feature switchesnot completed
  • 6.16Histogram quantile utilitynot completed
  • 6.17XFetch cache refresh decidernot completed

Week 7 · Deployment and production operations

Week 6 done · Up next · 6 modules

Open week 7

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Implement ShouldRefresh in the existing cache package.
  2. Use the XFetch probability formula with injected randomness.
  3. Handle expiry and disabled early-refresh configurations.
  4. Prevent overflow when the random value is zero.
  5. Verify that smaller random values increase early-refresh eligibility.
  6. Pass all eight build-tagged tests and the race-enabled test run.

Commands

terminal
cd backend && make test W=6O3
terminal
cd backend && go test -race -tags w6o3 ./internal/cache/ -run W6O3

Tests

  • TestW6O3_NeverRefreshesLongBeforeExpiry in backend/internal/cache/xfetch_test.go
  • TestW6O3_AlwaysRefreshesAtOrAfterExpiry in backend/internal/cache/xfetch_test.go
  • TestW6O3_ASmallRandomValueRefreshesEarlier in backend/internal/cache/xfetch_test.go
  • TestW6O3_ARandomValueOfOneAddsNoEarlyRefresh in backend/internal/cache/xfetch_test.go
  • TestW6O3_BetaScalesTheEarlyWindow in backend/internal/cache/xfetch_test.go
  • TestW6O3_ZeroBetaOrZeroRecomputeOnlyRefreshesAfterExpiry in backend/internal/cache/xfetch_test.go
  • TestW6O3_ARandomValueOfZeroDoesNotOverflow in backend/internal/cache/xfetch_test.go
  • TestW6O3_RefreshProbabilityRisesAsExpiryApproaches in backend/internal/cache/xfetch_test.go