Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Apply caching consistently across primary and fallback data sources
If the primary data provider caches results, the fallback provider on the same path should cache too, so behavior is consistent regardless of which source served the request.
Bad example
| 1 | async fetchRate(currency: string, date: string) { |
| 2 | const fromPrimary = await this.primary.fetchCached(currency, date); // cached |
| 3 | if (fromPrimary) return fromPrimary; |
| 4 |
|
| 5 | // fallback path bypasses caching entirely |
| 6 | return this.fallback.fetchRaw(currency, date); |
| 7 | } |
Explanation (EN)
When the primary source fails or returns nothing, every request hits the uncached fallback directly. Under load this can hammer the fallback dependency and gives inconsistent latency/freshness depending on which source answered.
Objašnjenje (HR)
Kad primarni izvor zakaže ili ne vrati ništa, svaki zahtjev izravno udara u necacheirani fallback. Pod opterećenjem to može preopteretiti fallback ovisnost i daje nedosljednu latenciju/svježinu ovisno o tome koji je izvor odgovorio.
Good example
| 1 | async fetchRate(currency: string, date: string) { |
| 2 | const fromPrimary = await this.primary.fetchCached(currency, date); |
| 3 | if (fromPrimary) return fromPrimary; |
| 4 |
|
| 5 | // fallback fetches are wrapped in the same cache layer |
| 6 | return this.fallback.fetchCached(currency, date); |
| 7 | } |
Explanation (EN)
Both the primary and fallback fetches go through a cache layer, so repeated requests are served consistently and the fallback dependency is protected the same way the primary is.
Objašnjenje (HR)
I primarno i fallback dohvaćanje prolaze kroz sloj predmemorije, pa se ponovljeni zahtjevi poslužuju dosljedno i fallback ovisnost je zaštićena jednako kao i primarna.
Notes (EN)
Verify the fallback path's caching explicitly during review; it is easy to add caching to the happy path and forget the fallback.
Bilješke (HR)
Eksplicitno provjeri predmemoriranje fallback puta tijekom pregleda; lako je dodati predmemoriju na glavni put i zaboraviti na fallback.