Cache שכתוב לא נכון = data stale, bugs מוזרים. Cache שכתוב נכון = 10x performance. הנה איך.
שכבות ה-Cache
(1) Browser cache: HTTP headers, service workers.
(2) CDN: static assets, edge caching.
(3) Application cache: Redis, Memcached.
(4) Database cache: query result cache.
(5) In-memory: local server memory.
Browser Cache
Static assets (JS, CSS, images): Cache-Control: public, max-age=31536000, immutable.
Hash in filename: main.abc123.js. שינוי = filename חדש = cache invalidation.
HTML: Cache-Control: no-cache. Always revalidate.
CDN Cache
Cloudflare, CloudFront, Fastly, Vercel Edge.
Static: cache everything.
Dynamic: cache by URL + query params + cookies. Careful with user-specific content.
Purge: API call to invalidate. Or short TTL + revalidate.
Redis Cache
Use cases: session storage, rate limiting, computed results, aggregations.
Patterns: cache-aside (check cache first, fetch from DB if miss), write-through (write to cache + DB simultaneously), write-behind (write to cache, async write to DB).
Recommended: cache-aside. פשוט וbetter consistency.
Cache Invalidation – הבעיה הכי קשה
Approaches:
(1) TTL: set expiration. Simple but data can be stale.
(2) Event-based: כשdata משתנה, מזמן invalidation. Complex but fresh.
(3) Version keys: include version בkey. update version = invalidate.
Cache Keys
Good format: entity:id:field.
Examples: user:12345:profile, product:widget-123:pricing.
Include version: user:12345:profile:v2. Update version instead of invalidate.
Serialization
JSON: readable, universal. Slower.
MessagePack, Protocol Buffers: faster, smaller. Requires schema.
For most projects: JSON is fine.
Memory Management
Redis eviction policies:
allkeys-lru: evict least recently used. Default for most use cases.
volatile-ttl: evict keys with shortest TTL.
noeviction: return error when memory full. Only for critical data.
Monitoring
Hit rate: % של requests שהgot cache. Target: 80%+.
Memory usage: warn at 70%, alert at 85%.
Slow commands: any command >100ms.
Connection pool: watch for exhaustion.
Common Mistakes
Caching everything: overhead > benefit for rarely-accessed data.
Ignoring cache stampede: קreadthundering herd – זכ many requests לfetch same missing key. Use locks or singleflight.
No monitoring: cache breaks silently. Metrics essential.
מבוסס על פרויקטים אמיתיים
המדריך הזה מבוסס על עבודה עם:
קריאה נוספת
אם המדריך הזה עזר לכם, אולי תרצו לקרוא גם על פיתוח SaaS מותאם – המדריך המקיף שלנו לתחום.
רוצים לדבר על הפרויקט שלכם?
שיחת ייעוץ חינם, ללא התחייבות - הרעיון שלכם + הניסיון שלנו
רוצים לדבר על הפרויקט שלכם?
אנחנו מתמחים בפיתוח SaaS, פתרונות AI, עיצוב UX/UI ובניית אתרים. ספרו לנו מה אתם צריכים.
דברו איתנו ←