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
- Event overage_policy defaults to REJECT, supports all three policies
187
194
195
+
### Performance Optimizations (all applied)
196
+
197
+
Seven optimizations applied to the reserve/commit/release hot path, preserving all protocol correctness guarantees (atomicity, idempotency, ledger invariant `remaining = allocated - spent - reserved - debt`).
198
+
199
+
| # | Optimization | Impact | Location |
200
+
|---|-------------|--------|----------|
201
+
| 1 |**BCrypt API key cache** — ConcurrentHashMap keyed by SHA-256(key), 60s TTL | Eliminates ~100ms+ BCrypt per request on cache hit |`ApiKeyRepository.java`|
202
+
| 2 |**EVALSHA** — Script SHA loaded at startup, 40-char hash sent instead of full script | Saves ~1-5KB network per call; auto-fallback to EVAL on NOSCRIPT |`LuaScriptRegistry.java` (new), all `eval()` callers |
203
+
| 3 |**Pipelined balance fetch** — N HGETALL calls batched into single round-trip | Saves (N-1) round-trips for fallback paths (extend, events, idempotency hits) |`RedisReservationRepository.fetchBalancesForScopes()`|
204
+
| 4 |**Lua returns balances** — Balance snapshots collected atomically at end of reserve/commit/release scripts | Eliminates post-operation Java balance fetch entirely for primary paths |`reserve.lua`, `commit.lua`, `release.lua`|
205
+
| 5 |**Lua HMGET** — Single HMGET per scope instead of EXISTS + 4 HGET; cached fail-fast values in ALLOW_WITH_OVERDRAFT; TIME reuse | Fewer Redis commands inside Lua scripts |`reserve.lua`, `commit.lua`, `release.lua`|
206
+
| 6 |**Tenant config cache** — ConcurrentHashMap with configurable TTL (default 60s, `cycles.tenant-config.cache-ttl-ms`) | Saves 1 Redis GET per reserve/event |`RedisReservationRepository.getTenantConfig()`|
**Thread safety**: All caches use `ConcurrentHashMap` with immutable record values. No locking required.
210
+
**Backward compatibility**: Old reservations without `budgeted_scopes` field handled via `budgeted_scopes_json or affected_scopes_json` fallback in all Lua scripts.
211
+
212
+
### Performance Benchmarks
213
+
214
+
End-to-end HTTP latency measured with `CyclesProtocolBenchmarkTest` (Spring Boot + Jedis + Redis 7 via Testcontainers). 200 measured iterations after 50 warmup iterations per operation.
215
+
216
+
| Operation | p50 | p95 | p99 | min | max | mean |
- Results are from a containerized CI environment (Testcontainers Redis 7-Alpine, localhost networking). Production with dedicated Redis will be faster.
229
+
- Latencies include full HTTP round-trip: Spring Boot request handling, auth filter, JSON serialization, Redis EVALSHA, Lua execution, response building.
230
+
- The BCrypt cache eliminates ~100ms+ from all operations after the first request per API key (60s cache window).
231
+
- Run benchmarks: `mvn test -Dgroups=benchmark` (requires Docker)
232
+
188
233
---
189
234
190
235
## Previously Found Issues (all fixed)
@@ -241,4 +286,4 @@ Two-pass audit covering:
241
286
242
287
## Verdict
243
288
244
-
The server implementation is **fully compliant** with the YAML spec (v0.1.23). All 9 endpoints are implemented, all schemas match, auth/tenancy/idempotency are correctly enforced, and the normative behavioral requirements (atomic operations, debt/overdraft handling, scope derivation, error semantics, dry-run rules, grace period handling) are properly implemented. Test coverage expanded to 494 tests across 22 test classes. No remaining spec violations found.
289
+
The server implementation is **fully compliant** with the YAML spec (v0.1.23) and **performance optimized**. All 9 endpoints are implemented, all schemas match, auth/tenancy/idempotency are correctly enforced, and the normative behavioral requirements (atomic operations, debt/overdraft handling, scope derivation, error semantics, dry-run rules, grace period handling) are properly implemented. Seven performance optimizations reduce hot-path latency by eliminating redundant Redis round-trips, caching BCrypt validation, and returning balance snapshots atomically from Lua scripts. Test coverage expanded to 522 tests across 24 test classes, including 8 performance benchmark tests. Single-operation p50 latency: 4.1-8.5ms. Full reserve-commit lifecycle p50: 12.2ms. No remaining spec violations found.
Copy file name to clipboardExpand all lines: cycles-protocol-service/cycles-protocol-service-api/src/main/java/io/runcycles/protocol/api/auth/ApiKeyAuthenticationFilter.java
0 commit comments