Skip to main content

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 document
  • get-posting-matrix.ts - Called for rule processing
  • list-chart-of-accounts.ts - Called frequently
  • search-chart-of-accounts.ts - Called frequently
🟡 HIGH FREQUENCY (20-50ms → 2-5ms):
  • get-organisation-details.ts
  • get-tool-settings.ts
  • get-chart-of-account-by-id.ts
  • get-chart-of-account-id-by-number.ts
  • get-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
Write Operations (6 files): Each file now has cache invalidation instructions showing:
  • Which cache keys to invalidate
  • When to invalidate
  • Example pseudo-code

3. Created Implementation Documentation

Created redis-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:

  1. apps/webapp/app/lib/actions/gl_dimensions/get-gl-dimensions.ts
  2. apps/webapp/app/lib/actions/posting_matrix/get-posting-matrix.ts
  3. apps/webapp/app/lib/actions/posting_matrix/get-posting-matrix-grouped-by-trigger-event.ts
  4. apps/webapp/app/lib/actions/chart_of_accounts/list-chart-of-accounts.ts
  5. apps/webapp/app/lib/actions/chart_of_accounts/search-chart-of-accounts.ts
  6. apps/webapp/app/lib/actions/chart_of_accounts/get-chart-of-account-by-id.ts
  7. apps/webapp/app/lib/actions/chart_of_accounts/get-chart-of-account-id-by-number.ts
  8. apps/webapp/app/lib/actions/organization/get-organisation-details.ts
  9. apps/webapp/app/lib/actions/tools/get-tool-settings.ts

Server Actions with Invalidation Comments:

  1. apps/webapp/app/lib/actions/posting_matrix/save-posting-matrix.ts
  2. apps/webapp/app/lib/actions/posting_matrix/delete-posting-matrix.ts
  3. apps/webapp/app/lib/actions/chart_of_accounts/create-chart-of-account.ts
  4. apps/webapp/app/lib/actions/chart_of_accounts/update-chart-of-account.ts
  5. apps/webapp/app/lib/actions/tools/update-tool-settings.ts
  6. apps/webapp/app/lib/actions/organization/update-organisation-onboarding-complete.ts

Documentation Created:

  1. redis-caching-implementation-plan.mdx (Comprehensive guide)
  2. redis-caching-summary.mdx (This file)

🎯 Cache Key Patterns

Posting Matrix & GL Dimensions

gl_dimensions:${organizationId}:${triggerEventId}:${includePostingMatrix}:${includeDeleted}
posting_matrix:${id}:${organizationId}
posting_matrices_grouped:${organizationId}

Chart of Accounts

chart_of_accounts:list:${organizationId}:${onlyReconcileAccounts}
chart_of_accounts:search:${organizationId}
chart_of_accounts:id:${organizationId}:${id}
chart_of_accounts:number:${organizationId}:${accountNumber}

Organization & Settings

organization:details:${organizationId}
tool_settings:${organizationId}:${toolSlug}

🔄 Cache Invalidation Map

When Posting Matrix Changes:

Invalidate:
  • posting_matrix:${matrixId}:${organizationId}
  • posting_matrices_grouped:${organizationId}
  • gl_dimensions:${organizationId}:* (all variants)
Triggers:
  • 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)
Triggers:
  • Chart account create
  • Chart account update
  • Chart account delete/archive

When Organization Settings Change:

Invalidate:
  • organization:details:${organizationId}
Triggers:
  • Organization onboarding complete
  • Organization settings update

When Tool Settings Change:

Invalidate:
  • tool_settings:${organizationId}:${toolSlug}
Triggers:
  • 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
Total Estimated Time: 12-18 hours (2-3 days)

📋 Next Steps for Egor

  1. Read the detailed plan: redis-caching-implementation-plan.mdx
  2. Follow the inline TODOs: Each file has exact instructions at the point where code should be added
  3. Set up Redis:
    • Choose provider (Upstash recommended)
    • Get connection details
    • Add to environment variables
  4. Implement by priority:
    • Start with 🔴 CRITICAL PATH items
    • Then 🟡 HIGH FREQUENCY items
    • Test after each phase
  5. 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:
# Find all caching TODOs
grep -r "REDIS CACHING" apps/webapp/app/lib/actions/

# Find all invalidation TODOs
grep -r "REDIS CACHE INVALIDATION" apps/webapp/app/lib/actions/
Or search for:
  • 🔴 REDIS CACHING - Critical path (implement first)
  • 🟡 REDIS CACHING - High frequency (implement second)
  • 🔴 REDIS CACHE INVALIDATION - Critical invalidation
  • 🟡 REDIS CACHE INVALIDATION - Medium invalidation