Ograniczanie liczby żądań API i strategie buforowania
Ochronią Państwo backend i obniżą koszty, wdrażając ograniczanie liczby żądań w celu powstrzymania nadużyć oraz warstwy buforowania, które ograniczają zbędną pracę i przyspieszają odpowiedzi.
Ograniczanie liczby żądań API i strategie buforowania to bezpłatna lekcja Indie Hacker Mobile Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Indie Hacker Mobile Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Indie Hacker Mobile Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Limit and Cache
As your app grows, two problems appear: abusive or runaway clients hammering your API, and the same expensive work repeated needlessly. Rate limiting and caching solve both.
Together they protect uptime and slash costs.
What Is Rate Limiting?
Rate limiting caps how many requests a client can make in a window — for example 100 requests per minute. Beyond that, requests are rejected or delayed.
It defends against abuse, bugs, and accidental loops.
The Token Bucket
A common algorithm is the token bucket: each request consumes a token; tokens refill at a steady rate. When the bucket is empty, requests are throttled.
let tokens = 5;
function allowRequest() {
if (tokens > 0) { tokens--; return true; }
return false;
}
console.log(allowRequest());
console.log(allowRequest());Communicating Limits
Good APIs return headers like X-RateLimit-Remaining and a 429 Too Many Requests status with a Retry-After hint.
This lets well-behaved clients back off gracefully.
What Is Caching?
Caching stores the result of expensive work so repeat requests return instantly without recomputing or re-fetching.
A cache hit saves database load, compute, and time.
Cache Keys and TTL
Each cached entry has a key identifying the request and a TTL (time to live) after which it expires and is refreshed.
const cache = new Map();
function setCache(key, value, ttlMs) {
cache.set(key, { value, expires: Date.now() + ttlMs });
}
setCache('user:1', { name: 'Alice' }, 60000);
console.log(cache.get('user:1'));Cache Layers
Caching happens at multiple levels:
- Client: in-app cache
- CDN: at the edge near users
- Server: in-memory or Redis
Each layer cuts work from the one below it.
Cache Invalidation
The hard part: stale data. When the underlying data changes, the cache must be invalidated or it serves outdated results.
Strategies include short TTLs, event-based invalidation, and versioned keys.
What Not to Cache
Avoid caching:
- Highly personalized or sensitive data without scoping by user
- Rapidly changing values where staleness misleads
Cache what is read often and changes rarely.
Combining the Two
Rate limiting and caching reinforce each other. Caching reduces how often you hit the limit, and limits protect uncached, expensive endpoints from abuse.
Apply both per endpoint based on cost and sensitivity.
A Protection Checklist
Before scaling:
- Rate limit per user and per IP
- Return 429 with Retry-After
- Cache hot, slow-changing reads with sensible TTLs
- Plan invalidation up front
- Never cache sensitive data unscoped
Resilient and cheap to run.
Quick Check
Test your rate limiting and caching knowledge.
Recap
You learned to protect and speed up your backend:
- Rate limiting caps requests and defends against abuse
- Token bucket is a common algorithm; return 429 with Retry-After
- Caching stores expensive results across client, CDN, and server
- Use TTLs and plan invalidation to avoid stale data
- Combine both per endpoint by cost and sensitivity
Resilient, fast, and cheap to operate.
Ucz się Indie Hacker Mobile Apps dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Ograniczanie liczby żądań API i strategie buforowania” jest bezpłatna?
Tak — pełny tekst „Ograniczanie liczby żądań API i strategie buforowania” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Indie Hacker Mobile Apps, przejdź na CoddyKit PRO. Kurs Indie Hacker Mobile Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Ograniczanie liczby żądań API i strategie buforowania”?
Ochronią Państwo backend i obniżą koszty, wdrażając ograniczanie liczby żądań w celu powstrzymania nadużyć oraz warstwy buforowania, które ograniczają zbędną pracę i przyspieszają odpowiedzi. Ćwiczysz Indie Hacker Mobile Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Indie Hacker Mobile Apps?
Nie wymagamy żadnego doświadczenia. Indie Hacker Mobile Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Ograniczanie liczby żądań API i strategie buforowania”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Indie Hacker Mobile Apps?
Tak. Każda lekcja Indie Hacker Mobile Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Optymalizacja BaaS pod kątem wydajności
- Niestandardowe integracje backendu
- Najlepsze praktyki bezpieczeństwa aplikacji mobilnych
- Ograniczanie liczby żądań API i strategie buforowania