feat: add NATS KV cache bucket for username→sub lookups#44
Conversation
Introduce the auth-service-username-sub-cache KV bucket (168h TTL) to cache Auth0 username→sub results, reducing Management API pressure and lookup latency. Cache is checked before each Auth0 call; a miss falls through to Auth0 and populates the cache on success. Issue: LFXV2-1561 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Jordan Evans <jevans@linuxfoundation.org>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
WalkthroughThis pull request introduces a username-to-subscription caching layer using NATS JetStream KV buckets. It adds Helm manifests, configuration values, KV bucket initialization, cache-aside lookup logic in the message handler, and comprehensive tests. Changes
Sequence DiagramsequenceDiagram
participant MH as Message Handler
participant KVC as KV Cache
participant UR as User Reader
MH->>MH: UsernameToSub(username)
alt Cache Available
MH->>KVC: Get(username)
alt Cache Hit
KVC-->>MH: UserID
MH-->>MH: Return cached UserID
else Cache Miss
KVC-->>MH: KeyNotFound error
MH->>UR: SearchUser(username)
UR-->>MH: user (with UserID)
MH->>KVC: Put(username, UserID)
KVC-->>MH: Success
MH-->>MH: Return UserID
end
else Cache Unavailable
MH->>UR: SearchUser(username)
UR-->>MH: user (with UserID)
MH-->>MH: Return UserID
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Pull request overview
This PR adds a NATS JetStream KeyValue bucket to act as a cache for Auth0 username→sub lookups, using cache-aside logic to reduce Auth0 Management API calls while allowing graceful degradation when the bucket is unavailable.
Changes:
- Adds a new KV bucket (
auth-service-username-sub-cache) via Helm with a 168h TTL and history=1. - Initializes the bucket best-effort at NATS client startup and wires the KV store into the message handler.
- Implements cache-aside behavior in
UsernameToSuband adds cache hit/miss unit tests.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
charts/lfx-v2-auth-service/templates/nats-kv-buckets.yaml |
Adds a Helm-managed JetStream KV manifest for the username→sub cache bucket. |
charts/lfx-v2-auth-service/values.yaml |
Introduces default values for configuring the new cache bucket (TTL, size, etc.). |
pkg/constants/storage.go |
Adds a constant for the new KV bucket name. |
internal/infrastructure/nats/client.go |
Best-effort initialization of the cache KV bucket at startup. |
cmd/server/service/providers.go |
Wires the KV store into the message handler orchestrator when available. |
internal/service/message_handler.go |
Adds a minimal KV interface and cache-aside lookup/populate logic in UsernameToSub. |
internal/service/message_handler_test.go |
Adds unit tests covering cache hit and cache miss behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // The username→sub cache bucket is best-effort: if it doesn't exist or is | ||
| // unavailable the service still works, just without caching. | ||
| if err := client.KeyValueStore(ctx, constants.KVBucketNameUsernameSubCache); err != nil { | ||
| slog.WarnContext(ctx, "username→sub cache bucket unavailable, caching disabled", | ||
| "error", err, | ||
| "bucket", constants.KVBucketNameUsernameSubCache, | ||
| ) | ||
| } else { |
There was a problem hiding this comment.
This best-effort initialization still emits an error-level log because KeyValueStore() logs slog.ErrorContext before returning the error. As a result, when the bucket is missing/unavailable startup will log both an ERROR and this WARN, which contradicts the PR description (“logs a warning and continues”) and may trigger error-based alerting. Consider adding a non-error logging path for optional buckets (e.g., detect/handle missing-bucket/JetStream-unavailable errors here, or add a KeyValueStore variant/flag that doesn’t log at error level for expected best-effort failures).
| maxValueSize: {{ .Values.nats.username_sub_cache_kv_bucket.maxValueSize }} | ||
| maxBytes: {{ .Values.nats.username_sub_cache_kv_bucket.maxBytes }} | ||
| compression: {{ .Values.nats.username_sub_cache_kv_bucket.compression }} | ||
| ttl: {{ .Values.nats.username_sub_cache_kv_bucket.ttl }} |
There was a problem hiding this comment.
Can you include replicas here as well please?
Summary
auth-service-username-sub-cacheNATS JetStream KV bucket (168h TTL) to cache Auth0 username→sub lookupsChanges
charts/.../nats-kv-buckets.yaml— new KV bucket manifest (unconditional, 168h TTL, history=1)charts/.../values.yaml—username_sub_cache_kv_bucketdefaultspkg/constants/storage.go—KVBucketNameUsernameSubCacheconstantinternal/infrastructure/nats/client.go— best-effort bucket init on startupinternal/service/message_handler.go—kvCacheinterface, cache-aside logic inUsernameToSubcmd/server/service/providers.go— wires KV store into the message handlerinternal/service/message_handler_test.go— cache hit and miss testsCloses LFXV2-1561
🤖 Generated with Claude Code