Redis Caching Implementation - Quick Summary
✅ What Was Done
1. Identified Critical Server Actions
Analyzed the codebase and identified 9 server actions where Redis caching will have the most impact: 🔴 CRITICAL PATH (100ms → 5ms):get-gl-dimensions.ts- Called for EVERY documentget-posting-matrix.ts- Called for rule processinglist-chart-of-accounts.ts- Called frequentlysearch-chart-of-accounts.ts- Called frequently
get-organisation-details.tsget-tool-settings.tsget-chart-of-account-by-id.tsget-chart-of-account-id-by-number.tsget-posting-matrix-grouped-by-trigger-event.ts
2. Added Inline TODO Comments
Added detailed caching instructions directly in 15 files: Read Operations (9 files): Each file now has a comment block showing:- Exact cache key format
- Recommended TTL (time-to-live)
- Priority level
- Example pseudo-code
- Which cache keys to invalidate
- When to invalidate
- Example pseudo-code
3. Created Implementation Documentation
Createdredis-caching-implementation-plan.mdx with:
- Complete implementation guide
- Cache invalidation map
- Setup instructions
- Testing strategy
- Cost breakdown ($50/mo)
- Expected performance (27x improvement)
📂 Files Modified
Server Actions with Caching Comments:
- ✅
apps/webapp/app/lib/actions/gl_dimensions/get-gl-dimensions.ts - ✅
apps/webapp/app/lib/actions/posting_matrix/get-posting-matrix.ts - ✅
apps/webapp/app/lib/actions/posting_matrix/get-posting-matrix-grouped-by-trigger-event.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/list-chart-of-accounts.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/search-chart-of-accounts.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/get-chart-of-account-by-id.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/get-chart-of-account-id-by-number.ts - ✅
apps/webapp/app/lib/actions/organization/get-organisation-details.ts - ✅
apps/webapp/app/lib/actions/tools/get-tool-settings.ts
Server Actions with Invalidation Comments:
- ✅
apps/webapp/app/lib/actions/posting_matrix/save-posting-matrix.ts - ✅
apps/webapp/app/lib/actions/posting_matrix/delete-posting-matrix.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/create-chart-of-account.ts - ✅
apps/webapp/app/lib/actions/chart_of_accounts/update-chart-of-account.ts - ✅
apps/webapp/app/lib/actions/tools/update-tool-settings.ts - ✅
apps/webapp/app/lib/actions/organization/update-organisation-onboarding-complete.ts
Documentation Created:
- ✅
redis-caching-implementation-plan.mdx(Comprehensive guide) - ✅
redis-caching-summary.mdx(This file)
🎯 Cache Key Patterns
Posting Matrix & GL Dimensions
Chart of Accounts
Organization & Settings
🔄 Cache Invalidation Map
When Posting Matrix Changes:
Invalidate:posting_matrix:${matrixId}:${organizationId}posting_matrices_grouped:${organizationId}gl_dimensions:${organizationId}:*(all variants)
- Posting matrix save/update
- Posting matrix delete
- Posting matrix create
When Chart of Accounts Changes:
Invalidate:chart_of_accounts:*:${organizationId}:*(all variants)gl_dimensions:${organizationId}:*(if chart is in posting rules)
- Chart account create
- Chart account update
- Chart account delete/archive
When Organization Settings Change:
Invalidate:organization:details:${organizationId}
- Organization onboarding complete
- Organization settings update
When Tool Settings Change:
Invalidate:tool_settings:${organizationId}:${toolSlug}
- Tool/integration settings update
🚀 Implementation Timeline
Phase 1: Setup (1-2 hours)
- Set up Redis instance (Upstash recommended)
- Add Redis client utility
- Configure environment variables
Phase 2: Critical Path Caching (4-6 hours)
- Implement caching in 4 critical server actions
- Test performance improvement
Phase 3: Cache Invalidation (3-4 hours)
- Add invalidation to 6 mutation actions
- Test invalidation correctness
Phase 4: Secondary Caching (2-3 hours)
- Implement remaining 5 server actions
- Add remaining invalidation logic
Phase 5: Testing & Monitoring (2-3 hours)
- Load test to verify 27x improvement
- Set up monitoring and alerts
- Document for team
📋 Next Steps for Egor
-
Read the detailed plan:
redis-caching-implementation-plan.mdx - Follow the inline TODOs: Each file has exact instructions at the point where code should be added
-
Set up Redis:
- Choose provider (Upstash recommended)
- Get connection details
- Add to environment variables
-
Implement by priority:
- Start with 🔴 CRITICAL PATH items
- Then 🟡 HIGH FREQUENCY items
- Test after each phase
-
Verify performance:
- Measure before/after metrics
- Confirm 27x improvement target
- Monitor in staging before production
🔍 Finding the TODOs
Search for these patterns in the codebase:🔴 REDIS CACHING- Critical path (implement first)🟡 REDIS CACHING- High frequency (implement second)🔴 REDIS CACHE INVALIDATION- Critical invalidation🟡 REDIS CACHE INVALIDATION- Medium invalidation