Redis 8.0 Granular Hash Field TTL (HEXPIRE, HSETEX & HGETDEL)

#!/usr/bin/env bash # Redis 7.4 / 8.0+ Granular Hash Field Expiration Blueprint # Replaces sprawling user:1042:device:<id> top-level keys with a single compact Hash per user set -euo pipefail REDIS_CLI="${REDIS_CLI:-redis-cli}" USER_KEY="acct:1042:auth" # 1. HSETEX (Redis 8.0+): Atomically Set Multiple Hash Fields With Shared Expiration (Seconds) # Stores desktop & mobile sessions with a 7-day (604800s) sliding TTL ${REDIS_CLI} HSETEX "${USER_KEY}" EX 604800 FIELDS 2 \ "sess:macbook" '{"ip":"192.0.2.10","mfa":true}' \ "sess:iphone" '{"ip":"198.51.100.24","mfa":true}' # 2. Store a Short-Lived 5-Minute Step-Up OTP Inside the Exact Same Hash (300s TTL) ${REDIS_CLI} HSETEX "${USER_KEY}" EX 300 FIELDS 1 \ "otp:stepup" "849201" # 3. Extend ONLY the Mobile Session TTL If New Expiry Is Greater (GT Flag) ${REDIS_CLI} HEXPIRE "${USER_KEY}" 1209600 GT FIELDS 1 "sess:iphone" # 4. Inspect Remaining Per-Field TTLs (Seconds) Without Fetching Payload Values ${REDIS_CLI} HTTL "${USER_KEY}" FIELDS 3 "sess:macbook" "sess:iphone" "otp:stepup" # 5. One-Time Atomic Read + Delete of Step-Up OTP (Prevents Replay Attacks in 1 RTT) CONSUMED_OTP=$("${REDIS_CLI}" HGETDEL "${USER_KEY}" FIELDS 1 "otp:stepup") echo "Verified & burned one-time OTP: ${CONSUMED_OTP}"
Redis 8.0 Granular Hash Field TTL (HEXPIRE, HSETEX & HGETDEL)
Note
Notification