-
Notifications
You must be signed in to change notification settings - Fork 633
refactor: usagelimiter with redis #3800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f0a1dc0
poc
Flo4604 902b0ed
poc
Flo4604 86ad929
shutdown
Flo4604 b3d42d7
fix bad test print
Flo4604 9fc66a9
more changes
Flo4604 b00249d
more changes
Flo4604 874b150
more changes
Flo4604 549952b
more changes
Flo4604 6fdc001
more changes
Flo4604 8494c22
more changes
Flo4604 db53eb5
more changes
Flo4604 d17b066
readme changes
Flo4604 533f12c
Update go/internal/services/keys/validation.go
Flo4604 727b7d5
Merge branch 'main' into perf/usagelimiter-use-redis
Flo4604 f56ccc7
update usagelimiter
Flo4604 2d436f1
Merge branch 'main' into perf/usagelimiter-use-redis
Flo4604 afd4df4
fix rabbit comments
Flo4604 6b618f7
Merge branch 'perf/usagelimiter-use-redis' of github.com:unkeyed/unke…
Flo4604 e4d6200
rl shouldnt close redis
Flo4604 7bd1d2f
fix rabbit comments
Flo4604 28df7a2
metrics
Flo4604 fd66eb4
Merge branch 'main' into perf/usagelimiter-use-redis
Flo4604 8fcd10d
adjust after merging main
Flo4604 e6cf981
adjust for comments
Flo4604 6b27358
adjust for comments
Flo4604 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
go/apps/api/integration/multi_node_usagelimiting/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # Multi-Node Usage Limiting Integration Tests | ||
|
|
||
| This package contains comprehensive integration tests for the usage limiting system under multi-node scenarios. These tests verify both accuracy and performance characteristics. | ||
|
|
||
| ## Test Structure | ||
|
|
||
| ### Generated Tests (`generated/`) | ||
|
|
||
| Automatically generated tests that cover various combinations of: | ||
|
|
||
| - **Node counts**: 1, 3, 5, 9 nodes | ||
| - **Credit amounts**: 10, 100, 500, 1000 credits | ||
| - **Cost per request**: 1, 5, 10, 20 credits | ||
| - **Load factors**: 0.8x, 0.9x, 1.0x, 1.2x, 1.5x, 2.0x, 3.0x | ||
| - **Test durations**: 10-30 seconds | ||
|
|
||
| ### Accuracy Tests (`accuracy_test.go`) | ||
|
|
||
| High-concurrency tests focused on credit counting accuracy: | ||
|
|
||
| - **Race condition detection**: 50-100 concurrent requests | ||
| - **Credit precision**: Verify exact credit consumption | ||
| - **Multi-node consistency**: Ensure distributed accuracy | ||
| - **Edge cases**: Single credit scenarios, high-cost operations | ||
|
|
||
| ### Performance Tests (`performance_test.go`) | ||
|
|
||
| Benchmarks and latency measurements: | ||
|
|
||
| - **Throughput benchmarks**: Measure maximum RPS | ||
| - **Latency analysis**: P50, P95, P99 percentiles | ||
| - **Sustained load tests**: 10-second stress tests | ||
| - **Concurrency scaling**: 1-100 concurrent workers | ||
|
|
||
| ## Running Tests | ||
|
|
||
| ### Quick Integration Tests (Single Node) | ||
|
|
||
| ```bash | ||
| go test -tags=integration ./apps/api/integration/multi_node_usagelimiting/generated/usagelimit_nodes01_*/ | ||
| ``` | ||
|
|
||
| ### Full Integration Suite | ||
|
|
||
| ```bash | ||
| go test -tags=integration_long ./apps/api/integration/multi_node_usagelimiting/generated/*/ | ||
| ``` | ||
|
|
||
| ### Stress Tests (9+ Nodes) | ||
|
|
||
| ```bash | ||
| go test -tags=stress ./apps/api/integration/multi_node_usagelimiting/generated/*/ | ||
| ``` | ||
|
|
||
| ### Accuracy Tests | ||
|
|
||
| ```bash | ||
| go test -tags=integration -run TestUsageLimitAccuracy ./apps/api/integration/multi_node_usagelimiting/ | ||
| ``` | ||
|
|
||
| ### Performance Tests | ||
|
|
||
| ```bash | ||
| go test -tags=integration -run TestUsageLimitLatency ./apps/api/integration/multi_node_usagelimiting/ | ||
| go test -tags=integration -run TestUsageLimitThroughput ./apps/api/integration/multi_node_usagelimiting/ | ||
| ``` | ||
|
Flo4604 marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Benchmarks | ||
|
|
||
| ```bash | ||
| go test -tags=integration -bench=BenchmarkUsageLimitPerformance ./apps/api/integration/multi_node_usagelimiting/ | ||
|
Flo4604 marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| ## Test Scenarios | ||
|
|
||
| ### Accuracy Verification | ||
|
|
||
| - **Credit precision**: Never exceed the credit limit | ||
| - **Race condition handling**: High concurrency doesn't cause over-spending | ||
| - **Multi-node consistency**: Distributed nodes maintain accuracy | ||
| - **Eventually consistent**: Redis and DB converge | ||
| - **Load balancing**: Traffic distributed across nodes | ||
|
|
||
| ### Performance Expectations | ||
|
|
||
| - **Latency**: P95 < 200ms, P99 < 500ms | ||
| - **Throughput**: > 100 RPS sustained | ||
| - **Error rate**: < 1% under normal load | ||
|
|
||
| ### Edge Cases Tested | ||
|
|
||
| - **Single credit exhaustion**: 1 credit, 100 concurrent requests | ||
| - **High-cost operations**: 20 credits per request | ||
| - **Extreme load**: 3x expected request rate | ||
| - **Node failures**: Graceful degradation (manual testing) | ||
|
|
||
| ## Test Data Flow | ||
|
|
||
| 1. **Setup**: Create API key with specific credit limit | ||
| 2. **Load generation**: Concurrent requests across multiple nodes | ||
| 3. **Real-time verification**: Check response accuracy | ||
| 4. **ClickHouse validation**: Verify analytics data | ||
| 5. **Database consistency**: Check eventual consistency | ||
| 6. **Cleanup**: Automatic test resource cleanup | ||
|
|
||
| ## Regenerating Tests | ||
|
|
||
| To add new test scenarios: | ||
|
|
||
| 1. Edit `generate_tests/main.go` | ||
| 2. Add new `TestCase` entries to `realisticCombinations` or `extremeEdgeCases` | ||
| 3. Run the generator: | ||
| ```bash | ||
| cd generate_tests && go generate | ||
| ``` | ||
|
|
||
|
Flo4604 marked this conversation as resolved.
Outdated
|
||
| ## Key Metrics Tracked | ||
|
|
||
| - **Success rate**: Percentage of successful verifications | ||
| - **Credit accuracy**: Actual vs expected credit consumption | ||
| - **Latency distribution**: Response time percentiles | ||
| - **Node distribution**: Traffic spread across nodes | ||
| - **Error patterns**: Types and frequency of failures | ||
|
|
||
| These tests ensure the usage limiting system maintains both high performance and perfect accuracy under production-like loads across multiple nodes. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.