การแคชและการบีบอัดการตอบสนองของ API
เร่งการตอบสนองของแบ็กเอนด์ด้วยชั้นแคชในหน่วยความจำและแบบกระจาย การทำให้แคชไม่ถูกต้องอย่างชาญฉลาด และการลดขนาดข้อมูล เพื่อให้เซิร์ฟเวอร์ทำงานน้อยลงต่อคำขอหนึ่งรายการ
การแคชและการบีบอัดการตอบสนองของ API เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Cache on the Backend?
Recomputing the same response for every request wastes CPU and database time. Caching stores computed results so repeat requests return instantly, cutting both latency and load.
Layers of Caching
- In-process memory fastest, but per-instance.
- Distributed cache (Redis/Memcached) shared across servers.
- HTTP/CDN cache at the edge.
A Simple Cache-Aside Pattern
The most common pattern: check the cache, return on hit, otherwise compute, store, and return. This is called cache-aside.
async function getUser(id) {
const hit = await redis.get('user:' + id);
if (hit) return JSON.parse(hit);
const user = await db.findUser(id);
await redis.set('user:' + id, JSON.stringify(user), 'EX', 300);
return user;
}Choosing a TTL
A time to live balances freshness against hit rate. Volatile data needs short TTLs; reference data can live much longer. Always set some expiry to avoid stale buildup.
Invalidation Strategies
The hard part of caching is invalidation. On writes, either delete the affected keys or update them (write-through). Stale data here is a common production bug.
async function updateUser(id, data) {
await db.update(id, data);
await redis.del('user:' + id);
}HTTP Caching Headers
For cacheable API responses, set Cache-Control so browsers and CDNs can reuse them, removing the request entirely on a hit.
res.set('Cache-Control', 'public, max-age=60, stale-while-revalidate=300');Conditional Requests
ETag and If-None-Match let the server reply 304 Not Modified with no body when data is unchanged, saving bandwidth.
res.set('ETag', hashOf(payload));
// next time: if If-None-Match matches, send 304Shrinking the Payload
Return only the fields clients need, paginate large lists, and avoid over-fetching. Smaller payloads serialize faster and transfer quicker.
Compressing Responses
Enable gzip or Brotli on JSON responses. Combined with caching, this minimizes both compute and transfer per request.
const compression = require('compression');
app.use(compression());Avoiding Stampedes
When a hot key expires, many requests may hit the database at once (a cache stampede). Mitigate with locks, request coalescing, or stale-while-revalidate.
Strategy Summary
- Cache-aside with sensible TTLs.
- Invalidate on writes.
- Use Cache-Control and ETags.
- Trim and compress payloads.
- Guard against stampedes.
Quick Check
After a user updates their profile, the API keeps returning the old data for several minutes. What is the most likely cause?
Recap
You learned to cut backend work with layered caching (cache-aside, TTLs, invalidation on writes), HTTP caching via Cache-Control and ETags, payload trimming, and compression, while guarding against cache stampedes.
คำถามที่พบบ่อย
บทเรียน “การแคชและการบีบอัดการตอบสนองของ API” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแคชและการบีบอัดการตอบสนองของ API” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแคชและการบีบอัดการตอบสนองของ API”
เร่งการตอบสนองของแบ็กเอนด์ด้วยชั้นแคชในหน่วยความจำและแบบกระจาย การทำให้แคชไม่ถูกต้องอย่างชาญฉลาด และการลดขนาดข้อมูล เพื่อให้เซิร์ฟเวอร์ทำงานน้อยลงต่อคำขอหนึ่งรายการ คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแคชและการบีบอัดการตอบสนองของ API” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม
ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คอขวดด้านประสิทธิภาพฝั่งแบ็กเอนด์
- การปรับปรุงประสิทธิภาพคำสั่งฐานข้อมูล
- ผลกระทบของการแสดงผลฝั่งเซิร์ฟเวอร์ (SSR)
- การแคชและการบีบอัดการตอบสนองของ API