FILE X-1056 / Backend engineering
Redis Business Patterns: Caching, Distributed Locks, Message Queues, Feed Streams, and Flash Sales
ENGINEERING NOTES
Published / DavidHLPL
The 20 Redis business notes across the two repositories mirror each other (each topic appears once in Personal and once in Fuwari, in Chinese and English). This page anchors on cache consistency, lock correctness, and messaging semantics, converging them into reusable patterns rather than course transcripts.
Core Mechanism
1. Cache model and consistency
Redi缓存模型和思路.md(Redis Cache Model) andRedis-Caching-Models.mddistinguish Cache-Aside, Read/Write-Through, and invalidation strategies; TTL, proactive eviction, and delayed double-delete each have trade-offs.- Cache nulls (see
java-null-value) use placeholders to avoid penetration;Spring Cacheannotations are covered separately inCacheableAndCacheEvict.
2. Distributed locks and concurrency control
- Optimistic locks (CAS/version) suit read-heavy, retryable conflicts; pessimistic/Redisson distributed locks suit mutual exclusion of critical sections.
分布式锁.md(Distributed Lock) andDistributed-Locks-with-Redis.mduseSET NX EXatomicity; the Redisson article introduces watchdog renewal, but holding time, renewal, and reentrancy boundaries must be checked against the version.
3. Message queues, Feed streams, and flash sales
Redis消息队列.md(Redis Message Queue) compares List/Stream/PubSub; only Stream consumer groups with ACK provide reliable semantics.- Feed streams (push/pull/push-pull hybrid) and flash sales (stock deduction, rate limiting, async queues) are dissected by scenario in
Feed流设计模型.md(Feed Design) andSeckill-System.md, but capacity parameters were not measured. - Helper classes and Session replacement in
Redis工具类实现.md(Redis Helper) andReplacing-Traditional-Sessions-with-Redis.mdgive encapsulation examples.
Applicability
- Cache: read-heavy, tolerates brief inconsistency, hotspots are predictable.
- Locks: short critical sections, controllable timeouts, clear lock granularity.
- Queues: at-least-once is acceptable, consumption is idempotent.
Not Applicable and Risks
- Strong transactions, exactly-once semantics, and complex queries are not suited to pure Redis.
- If a distributed lock expires before business logic completes, it can cause dual activity; watchdog renewal, network partitions, and clock drift must be evaluated explicitly (see
resicacherelated pages). - Capacity designs for flash sales and Feed were not load-tested; thresholds must not be copied verbatim.
Minimum Verification
- Cache: load-test cache hits, repeated lookups for nonexistent data, and concurrent misses for a hot key separately, recording TTL and source-load counts.
- Locks: two processes contend for
SET NX, verifying timeout release andLuaunlock atomicity. - Queues: Stream consumer-group ACK/NACK, verify replay after disconnect.
Evidence and Uncertainty
- Source facts:
ingest-redis-businesscontains 20 source notes with Chinese-English duplicates. - Synthesis: This page converges business transcripts into consistency—mutual exclusion—semantics axes.
- Unconfirmed: Redisson version, Stream consumer-group parameters, and flash sales thresholds were not pinned to a version; verify against the current environment.
Related Pages
- redis-persistence-principle
- java-null-value
- redis-jackson-java-time
- resicache-observer-nested-execution
Sources and related documents
- ingest-redis-business
- Redis Persistence and Internals: RDB/AOF, Data Structures, and Advanced Mechanisms
- NullValue: The Cache-Null Placeholder and Serialization Boundary
- Redisson/Jackson’s Default Serialization Behavior for LocalDateTime and the Write-Read Asymmetry Boundary
- ResiCache: Observer Nested Execution Must Distinguish Lifecycle, Fragments, and Scope Tokens
Research notes
- Original document: src/content/note/en/redis-business-patterns.md
- Source revision: f49953fb0d4bd52a39bf81167b0c4b7dd7d78a64
- Evidence status: active