Assist_Design/docs/CDC_QUICK_REFERENCE.md

251 lines
7.4 KiB
Markdown
Raw Normal View History

# 🎯 Quick Reference: CDC Cache Strategy
## Your Questions Answered
### 1⃣ What happens to offline customers?
**Short Answer:** Cache is **deleted** when data changes, NOT kept for offline users.
```
Customer offline for 7 days:
Day 1: Logged in → Cache populated
Day 2: Product changes → CDC invalidates → Cache DELETED ✅
Day 3-7: Customer offline → No cache exists
Day 8: Customer logs back in → Cache miss → Fresh fetch
Result: No stale data, no wasted memory!
```
---
### 2⃣ Should we stop invalidating for offline customers?
**Short Answer:** NO - Current approach is optimal!
```
❌ Bad: Track online users
if (user.isOnline) invalidate()
Problem: Complex, race conditions, doesn't save API calls
✅ Good: Invalidate everything (current)
invalidateAllCatalogs()
Result: Simple, correct, efficient
```
---
### 3⃣ How many API calls does CDC save?
**Short Answer:** **98% reduction** in API calls!
```
WITHOUT CDC (5-minute TTL):
100 users × 3 catalog views/day = 300 API calls/day
Monthly: 9,000 API calls
WITH CDC (event-driven):
5 product changes/day × 1 API call = 5 API calls/day
Monthly: 150 API calls
SAVINGS: 8,850 API calls/month (98.3% reduction) 🎉
```
---
### 4⃣ Do we even need Salesforce API with CDC?
**Short Answer:** YES - CDC notifies, API fetches data.
```
CDC Event contains:
✅ Notification that Product X changed
❌ Does NOT contain the new product data
You still need to:
1. Receive CDC event → Invalidate cache
2. Customer requests catalog → Cache miss
3. Fetch from Salesforce API → Get actual data
4. Store in cache → Serve to customer
```
---
## 📊 Comparison Table
| Metric | TTL (5 min) | CDC + Hybrid TTL | Improvement |
|--------|-------------|------------------|-------------|
| **API calls/day** | 300 | 5-7 | **98% less** |
| **API calls/month** | 9,000 | 150-210 | **98% less** |
| **Cache hit ratio** | ~0% | 95-99% | **Much better** |
| **Data freshness** | Up to 5 min stale | < 5 sec stale | **Real-time** |
| **Memory usage** | High (never expires) | Low (TTL cleanup) | **Efficient** |
---
## ⚙️ Recommended Configuration
```typescript
// Catalog Cache
CATALOG_TTL = 86400 // 24 hours
STATIC_TTL = 604800 // 7 days
ELIGIBILITY_TTL = 3600 // 1 hour
VOLATILE_TTL = 60 // 1 minute
// Order Cache
SUMMARY_TTL = 3600 // 1 hour
DETAIL_TTL = 7200 // 2 hours
```
**Why Hybrid TTL?**
-**Primary:** CDC events invalidate when data changes (real-time)
-**Backup:** TTL expires unused entries (memory cleanup)
-**Best of both:** Real-time freshness + memory efficiency
---
## 🔄 How It Works
```
┌─────────────────────────────────────────────────────┐
│ CDC HYBRID CACHE FLOW │
├─────────────────────────────────────────────────────┤
│ │
│ Salesforce: Product price changes │
│ ↓ │
│ CDC Event: Product2ChangeEvent │
│ ↓ │
│ Portal: CatalogCdcSubscriber │
│ ↓ │
│ Redis: DELETE catalog:internet:plans │
│ ↓ │
│ User 1 requests catalog (cache miss) │
│ ↓ │
│ Fetch from Salesforce API (1 call) │
│ ↓ │
│ Store in Redis with TTL: 24 hours │
│ ↓ │
│ Users 2-100 request catalog (cache hit) ✅ │
│ │
│ IF no CDC event for 24 hours: │
│ ↓ │
│ TTL expires → Cache deleted (cleanup) │
│ ↓ │
│ Next user → Cache miss → Fresh fetch │
│ │
└─────────────────────────────────────────────────────┘
```
---
## 📈 Real-World Example
### Scenario: 100 Active Users, Internet Service Provider
```
Daily Activity:
- 100 users log in per day
- Each user views catalog 3 times
- Products change 5 times/day (price updates, new plans)
WITH CDC + Hybrid TTL:
8:00 AM - User 1 logs in
→ Cache miss (overnight TTL expired)
→ API call #1: Fetch catalog
→ Cache populated (24h TTL)
8:05 AM - Users 2-50 log in
→ Cache HIT (no API calls) ✅
10:30 AM - Product price updated in Salesforce
→ CDC event received
→ Cache invalidated (deleted)
10:32 AM - User 51 logs in
→ Cache miss (just invalidated)
→ API call #2: Fetch fresh catalog
→ Cache populated (24h TTL)
10:35 AM - Users 52-100 log in
→ Cache HIT (no API calls) ✅
... (3 more product changes during day)
End of Day:
Total API calls: 5 (one per product change)
Cache hit ratio: 95%
Next Morning (8:00 AM):
→ 24h TTL expired overnight
→ First user: Cache miss → API call
→ Subsequent users: Cache hit
```
**Monthly Stats:**
- API calls: ~150 (5/day × 30 days)
- Compared to 5-min TTL: 9,000 calls
- **Savings: 8,850 API calls (98% reduction)**
---
## ✅ Why Your Current Setup + Hybrid TTL is Perfect
### 1. CDC Handles Real-Time Changes ✅
```
Product changes → Instant invalidation (< 5 seconds)
Customer sees fresh data immediately
```
### 2. TTL Handles Memory Cleanup ✅
```
Unused cache entries → Expire after 24h
Redis memory stays lean
```
### 3. Offline Customers Don't Matter ✅
```
Customer offline 7 days:
- Day 2: Cache deleted (CDC or TTL)
- Day 8: Cache rebuilt on login
No stale data, no wasted resources
```
### 4. Minimal API Calls ✅
```
5 product changes/day = 5 API calls
100 users share cached results
98% reduction vs TTL-only approach
```
---
## 🚀 Implementation Status
**Catalog CDC:** Implemented with hybrid TTL
**Order CDC:** Implemented with smart filtering + hybrid TTL
**Environment Config:** All channels configured
**Module Registration:** All subscribers registered
**Documentation:** Comprehensive guides created
**You're production-ready!** 🎉
---
## 🎓 Key Takeaways
1. **CDC deletes cache, doesn't update it** → Offline users don't accumulate stale data
2. **Global invalidation is correct** → Simpler and more efficient than selective
3. **98% API call reduction** → From 9,000/month to 150/month
4. **Still need Salesforce API** → CDC notifies, API fetches actual data
5. **Hybrid TTL is optimal** → Real-time freshness + memory efficiency
---
## 📚 Related Documentation
- [CDC_SETUP_VERIFICATION.md](./CDC_SETUP_VERIFICATION.md) - Catalog CDC setup guide
- [ORDER_CDC_SETUP.md](./ORDER_CDC_SETUP.md) - Order CDC setup guide
- [CDC_API_USAGE_ANALYSIS.md](./CDC_API_USAGE_ANALYSIS.md) - Detailed API analysis
- [CACHING_STRATEGY.md](./CACHING_STRATEGY.md) - Overall caching architecture