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:
func ShouldRefresh(
now, expiry time.Time,
recompute time.Duration,
beta float64,
rnd func() float64,
) boolAcceptance criteria
- One minute before expiry, a recomputation duration of
100*time.Millisecond, beta1and random value0.5must not trigger a refresh. - At the expiry instant, the function returns
true. It also returnstrueafter expiry, even when the recomputation duration is zero. - With
nowtwo seconds before expiry and a one-second recomputation duration, random valuemath.Exp(-3)triggers an early refresh, whilemath.Exp(-1)does not. - With
nowone nanosecond before expiry, a one-hour recomputation duration, beta1and random value1, the function does not refresh early. - With
nowthree seconds before expiry, a one-second recomputation duration and random valuemath.Exp(-1), beta1does not trigger a refresh, while beta4does. - With
nowone millisecond before expiry, beta0or recomputation duration0disables early refresh, even with random value0.0001. - A random value of
0, withnowone hour before expiry, a one-second recomputation duration and beta1, must be handled without duration overflow or a panic. - Across 1000 random values from
0.001through1, 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
rndfunction argument. Do not use a global random generator. - Do not read the system clock inside the function. Use the supplied
nowandexpiry. - 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()) >= expiryEquivalently, 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:
cd backend
make test W=6O3
go test -race -tags w6o3 ./internal/cache/ -run W6O3Read the first failing assertion to identify whether the problem is in expiry handling, the random-value calculation, beta scaling or numerical safety.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Runtime and performance extensions
3 items
- 6.15Runtime feature switchesnot completed
- 6.16Histogram quantile utilitynot completed
- 6.17XFetch cache refresh decidernot completed
Week 6 done · Up next · 6 modules
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement
ShouldRefreshin the existing cache package. - Use the XFetch probability formula with injected randomness.
- Handle expiry and disabled early-refresh configurations.
- Prevent overflow when the random value is zero.
- Verify that smaller random values increase early-refresh eligibility.
- Pass all eight build-tagged tests and the race-enabled test run.
Commands
cd backend && make test W=6O3cd backend && go test -race -tags w6o3 ./internal/cache/ -run W6O3Tests
TestW6O3_NeverRefreshesLongBeforeExpiryinbackend/internal/cache/xfetch_test.goTestW6O3_AlwaysRefreshesAtOrAfterExpiryinbackend/internal/cache/xfetch_test.goTestW6O3_ASmallRandomValueRefreshesEarlierinbackend/internal/cache/xfetch_test.goTestW6O3_ARandomValueOfOneAddsNoEarlyRefreshinbackend/internal/cache/xfetch_test.goTestW6O3_BetaScalesTheEarlyWindowinbackend/internal/cache/xfetch_test.goTestW6O3_ZeroBetaOrZeroRecomputeOnlyRefreshesAfterExpiryinbackend/internal/cache/xfetch_test.goTestW6O3_ARandomValueOfZeroDoesNotOverflowinbackend/internal/cache/xfetch_test.goTestW6O3_RefreshProbabilityRisesAsExpiryApproachesinbackend/internal/cache/xfetch_test.go