You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf: optimize GraphQL route and add domain caching
- Wrap data-fetching functions (getPage, getSiteInfo, getFullSiteSetup) with React.cache() to deduplicate SSR calls
- Add in-memory domain cache with 60s TTL (domain-cache.ts)
- Use getCachedDomain in verify-domain route and GraphQL route handler
- Parallelize domain lookup, session check, and body parsing with Promise.all in GraphQL route
- Store plain objects in domain cache, hydrate fresh Mongoose docs per request to prevent cross-request mutation
3.[GraphQL route handler](file:///Users/rajat/dev/projects/courselit/apps/web/app/api/graph/route.ts#L22-L84) does another `DomainModel.findOne()` + `auth.api.getSession()` + `User.findOne()`
124
-
4. Then the actual resolver logic runs
125
-
126
-
### The Fix
127
-
128
-
**For public pages** (no auth needed), call the database models directly from Server Components:
This eliminates the HTTP round-trip overhead and the redundant auth/domain resolution in the GraphQL handler.
104
+
> [!CAUTION] > **Decision: Keep data fetching through the GraphQL API.** The GraphQL resolvers contain critical business logic that runs on first access — such as `initSharedWidgets`, permission checks, and admin-vs-public field filtering. Duplicating this logic in direct DB queries would be fragile, error-prone, and hard to maintain. The HTTP self-fetch overhead is acceptable given the `React.cache()` deduplication (item #1) and domain caching (item #2) already in place.
161
105
162
106
---
163
107
164
-
## 🟡 4. Redis Caching Layer for Tenant Data (High ROI for Multi-Tenant)
108
+
## 🟡 4. Redis Caching Layer for Tenant Data — Phase 2
165
109
166
110
**Impact: ~80–95% reduction in MongoDB load for public pages**
167
111
**Effort: Medium**
@@ -318,14 +262,12 @@ For a multi-tenant setup, put a CDN (Cloudflare, CloudFront) in front with **Var
| 1 |`React.cache()` on data fetchers | 🔴 Critical | ⬜ Small | ⭐⭐⭐⭐⭐ |
324
-
| 2 | Cache `verify-domain`| 🔴 High | ⬜ Small | ⭐⭐⭐⭐⭐ |
325
-
| 3 | Direct DB calls from Server Components | 🟡 High | 🟨 Medium | ⭐⭐⭐⭐ |
326
-
| 4 | Redis caching layer | 🟡 High | 🟨 Medium | ⭐⭐⭐⭐ |
327
-
| 5 | Convert client → server components | 🟡 Medium | 🟥 High | ⭐⭐⭐ |
328
-
| 6 | Optimize font loading | 🟢 Medium | ⬜ Small | ⭐⭐⭐ |
329
-
| 7 | HTTP caching / CDN | 🟢 Medium | ⬜ Small | ⭐⭐⭐ |
330
-
331
-
> [!TIP] > **Recommended first step**: Apply optimization #1 (`React.cache()`) — it's a 15-minute change that will immediately cut your per-page HTTP requests from ~11 down to ~3, giving you the biggest bang for minimal effort.
0 commit comments