Commit 79a6989
committed
perf(redis): keyTypeAt fast path for stream/hash/zset commands
keyTypeAt was the leader's #2 CPU consumer in production (74.8 percent of
the 30s pprof, on top of XREAD busy-poll which Phase A addressed).
Each call issues up to ~19 Pebble seeks across every collection family
before returning. Most callers know which type they expect: XADD wants
a stream, HSET wants a hash, ZADD wants a zset. Probing every other
type is wasted work in the steady state.
Add keyTypeAtExpect(ctx, key, readTS, expected) that probes only the
prefixes for the expected type (typically 2-3 seeks):
- redisTypeStream: StreamMetaKey + legacy redisStreamKey (2 seeks)
- redisTypeHash: HashFieldScanPrefix + HashMetaKey + delta (3 seeks)
- redisTypeSet/ZSet: same shape (3 seeks)
- redisTypeList: ListMetaKey + delta (2 seeks)
- redisTypeString: 3 ExistsAt across str/HLL/legacy (3 seeks)
On hit, return after the TTL filter -- ~6-9x reduction over the slow
path's 19 seeks. On miss, fall through to the full keyTypeAt slow
path so wrongType detection still surfaces the actual key type for
WRONGTYPE replies. Strict semantics preserved on every branch.
Converted call sites:
- Stream (6): xaddTxn, streamTypeForWrite, resolveXReadDollarID,
xreadOnce (per-iteration, hottest call), xlen, rangeStream.
- Hash (8): buildHashFieldElems, hgetSlow, hexistsSlow, hmget,
hdelTxn, hlen, hincrbyWithMigration, hgetall.
- ZSet (7): zsetRangeEmptyFastResult, zaddTxn, zincrbyTxn,
zrangeRead, zrem, zremrangebyrank, tryBZPopMin.
Set / list / string / mixed-kind callers (validateExactSetKind,
smembers, pfcount, getdel, incr, ltrim, lindex, etc.) are left for a
follow-up. Same pattern, but their throughput is lower in the
production traffic mix that motivated this work.
Test coverage:
- TestRedis_StreamCommandsRejectWrongType (new) -- locks down the
fall-through path for streams: XADD/XLEN/XRANGE/XREAD on a string
key all return WRONGTYPE, proving keyTypeAtExpect's slow-path
delegation when the expected probe comes back empty.
- Existing TestRedis_HGET_WRONGTYPE / TestRedis_HEXISTS_WRONGTYPE /
TestRedis_SISMEMBER_WRONGTYPE etc. continue to lock down the same
property for hash/set commands.
- Existing collection round-trip tests (TestRedis_Stream*,
TestRedis_Hash*, TestRedis_Z*, TestRedis_BullMQ*) cover the fast
path's hit case end-to-end -- XADD-then-XREAD, HSET-then-HGET,
ZADD-then-ZRANGE all run through keyTypeAtExpect now.
Self-review (CLAUDE.md 5 lenses):
1. Data loss -- None. Read-only probe path; the slow-path fallback is
the unchanged keyTypeAt, so any branch that previously detected
the right type still does.
2. Concurrency -- No new shared state. Each call is read-only against
the existing MVCC snapshot at readTS.
3. Performance -- 6-9x seek reduction on the hit case (the steady
state for live keys), no extra seeks on the miss case (caller
pays the same ~19 seeks they would have without this PR).
4. Data consistency -- TTL filter is applied identically to the slow
path; the fast path returns "found at readTS" only after the same
applyTTLFilter call. WrongType detection is preserved by the slow-
path delegation when expected probes come back empty.
5. Test coverage -- new wrongType lockdown for stream commands; the
existing collection-family wrongType tests lock down hash/set/zset.
The hit-path is exercised by every existing collection round-trip
test which now run through keyTypeAtExpect.1 parent bf67fa5 commit 79a6989
3 files changed
Lines changed: 145 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2042 | 2042 | | |
2043 | 2043 | | |
2044 | 2044 | | |
2045 | | - | |
| 2045 | + | |
2046 | 2046 | | |
2047 | 2047 | | |
2048 | 2048 | | |
| |||
2497 | 2497 | | |
2498 | 2498 | | |
2499 | 2499 | | |
2500 | | - | |
| 2500 | + | |
2501 | 2501 | | |
2502 | 2502 | | |
2503 | 2503 | | |
| |||
2524 | 2524 | | |
2525 | 2525 | | |
2526 | 2526 | | |
2527 | | - | |
| 2527 | + | |
2528 | 2528 | | |
2529 | 2529 | | |
2530 | 2530 | | |
| |||
2555 | 2555 | | |
2556 | 2556 | | |
2557 | 2557 | | |
2558 | | - | |
| 2558 | + | |
2559 | 2559 | | |
2560 | 2560 | | |
2561 | 2561 | | |
| |||
2669 | 2669 | | |
2670 | 2670 | | |
2671 | 2671 | | |
2672 | | - | |
| 2672 | + | |
2673 | 2673 | | |
2674 | 2674 | | |
2675 | 2675 | | |
| |||
2792 | 2792 | | |
2793 | 2793 | | |
2794 | 2794 | | |
2795 | | - | |
| 2795 | + | |
2796 | 2796 | | |
2797 | 2797 | | |
2798 | 2798 | | |
| |||
2822 | 2822 | | |
2823 | 2823 | | |
2824 | 2824 | | |
2825 | | - | |
| 2825 | + | |
2826 | 2826 | | |
2827 | 2827 | | |
2828 | 2828 | | |
| |||
2941 | 2941 | | |
2942 | 2942 | | |
2943 | 2943 | | |
2944 | | - | |
| 2944 | + | |
2945 | 2945 | | |
2946 | 2946 | | |
2947 | 2947 | | |
| |||
3040 | 3040 | | |
3041 | 3041 | | |
3042 | 3042 | | |
3043 | | - | |
| 3043 | + | |
3044 | 3044 | | |
3045 | 3045 | | |
3046 | 3046 | | |
| |||
3262 | 3262 | | |
3263 | 3263 | | |
3264 | 3264 | | |
3265 | | - | |
| 3265 | + | |
3266 | 3266 | | |
3267 | 3267 | | |
3268 | 3268 | | |
| |||
3332 | 3332 | | |
3333 | 3333 | | |
3334 | 3334 | | |
3335 | | - | |
| 3335 | + | |
3336 | 3336 | | |
3337 | 3337 | | |
3338 | 3338 | | |
| |||
3502 | 3502 | | |
3503 | 3503 | | |
3504 | 3504 | | |
3505 | | - | |
| 3505 | + | |
3506 | 3506 | | |
3507 | 3507 | | |
3508 | 3508 | | |
| |||
3542 | 3542 | | |
3543 | 3543 | | |
3544 | 3544 | | |
3545 | | - | |
| 3545 | + | |
3546 | 3546 | | |
3547 | 3547 | | |
3548 | 3548 | | |
| |||
3590 | 3590 | | |
3591 | 3591 | | |
3592 | 3592 | | |
3593 | | - | |
| 3593 | + | |
3594 | 3594 | | |
3595 | 3595 | | |
3596 | 3596 | | |
| |||
3627 | 3627 | | |
3628 | 3628 | | |
3629 | 3629 | | |
3630 | | - | |
| 3630 | + | |
3631 | 3631 | | |
3632 | 3632 | | |
3633 | 3633 | | |
| |||
4013 | 4013 | | |
4014 | 4014 | | |
4015 | 4015 | | |
4016 | | - | |
| 4016 | + | |
4017 | 4017 | | |
4018 | 4018 | | |
4019 | 4019 | | |
| |||
4328 | 4328 | | |
4329 | 4329 | | |
4330 | 4330 | | |
4331 | | - | |
| 4331 | + | |
4332 | 4332 | | |
4333 | 4333 | | |
4334 | 4334 | | |
| |||
4554 | 4554 | | |
4555 | 4555 | | |
4556 | 4556 | | |
4557 | | - | |
| 4557 | + | |
4558 | 4558 | | |
4559 | 4559 | | |
4560 | 4560 | | |
| |||
4606 | 4606 | | |
4607 | 4607 | | |
4608 | 4608 | | |
4609 | | - | |
| 4609 | + | |
4610 | 4610 | | |
4611 | 4611 | | |
4612 | 4612 | | |
| |||
4897 | 4897 | | |
4898 | 4898 | | |
4899 | 4899 | | |
4900 | | - | |
| 4900 | + | |
4901 | 4901 | | |
4902 | 4902 | | |
4903 | 4903 | | |
| |||
5001 | 5001 | | |
5002 | 5002 | | |
5003 | 5003 | | |
5004 | | - | |
| 5004 | + | |
5005 | 5005 | | |
5006 | 5006 | | |
5007 | 5007 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
104 | 153 | | |
105 | 154 | | |
106 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
317 | 392 | | |
318 | 393 | | |
319 | 394 | | |
| |||
0 commit comments