KEMBAR78
Redis in Depth Study Notes | PDF | Cache (Computing) | Software
0% found this document useful (0 votes)
14 views5 pages

Redis in Depth Study Notes

Uploaded by

Pooja N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Redis in Depth Study Notes

Uploaded by

Pooja N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Redis In-Depth Study Notes

1. Introduction to Redis
Redis (REmote DIctionary Server) is an open-source, in-memory key-value store used for
high-performance applications.
Key Features:
- In-memory: Fast access as all data is stored in RAM
- Persistence: Optional RDB snapshots or AOF logs for durability
- Versatile data structures: Strings, Lists, Hashes, Sets, Sorted Sets, etc.
- Use cases: Caching, session management, message brokering, real-time analytics

Installation:
- Linux: sudo apt install redis-server
- Docker: docker run --name redis -p 6379:6379 -d redis
- CLI Tool: redis-cli to interact with the server

2. Redis Data Types (Deep Dive)


Strings:
- SET key value
- GET key
- INCR key, DECR key
- Use case: counters, tokens, cache values

Lists:
- LPUSH key value, RPUSH key value
- LPOP key, RPOP key
- LRANGE key start stop
- Use case: job queues, logs

Sets:
- SADD key member
- SMEMBERS key
- SINTER, SUNION, SDIFF for set operations
- Use case: unique values, tags, roles

Hashes:
- HSET key field value
- HGET key field
- HGETALL key
- Use case: storing user profiles

Sorted Sets:
- ZADD key score member
- ZRANGE key start stop WITHSCORES
- Use case: leaderboards, rankings

Bitmaps:
- SETBIT key offset value
- GETBIT key offset
- Use case: tracking binary states (e.g., login days)

HyperLogLog:
- PFADD key element
- PFCOUNT key
- Use case: approximate cardinality (e.g., unique visitors)

Streams:
- XADD key * field1 value1 ...
- XREAD COUNT 2 STREAMS key 0
- Use case: log collection, real-time processing

3. Expiry, Eviction & Persistence


Expiry:
- SET key value EX 60 (expires in 60s)
- EXPIRE key seconds
- TTL key

Eviction Policies:
- Configured in redis.conf or via CONFIG SET
- allkeys-lru, volatile-lru, allkeys-random, noeviction

Persistence:
- RDB: point-in-time snapshots (SAVE, BGSAVE)
- AOF: logs each write (append-only)
- Hybrid persistence also possible

4. Transactions & Lua Scripting


Transactions:
- MULTI ... EXEC: Queues and executes atomically
- WATCH key: Monitor changes and prevent race conditions

Lua Scripting:
- EVAL "return redis.call('SET', KEYS[1], ARGV[1])" 1 key value
- Atomic and optimized operations
- Avoids race conditions and improves performance

5. Pub/Sub Messaging
Publish/Subscribe:
- SUBSCRIBE channel
- PUBLISH channel message
- Used for real-time event distribution (e.g., chat systems)
- Fire-and-forget, no message persistence

6. Redis as a Cache
Why Redis is ideal for caching:
- High speed, TTL support, data eviction

Cache Patterns:
- Write-through: write to cache and DB together
- Write-around: write to DB only, cache on read
- Write-back: write to cache first, update DB later

Eviction Strategies:
- LRU (Least Recently Used)
- LFU (Least Frequently Used)
- FIFO (First In, First Out)

7. Redis for Distributed Systems


Redis Sentinel:
- Provides high availability
- Automatic failover, monitoring, notifications

Redis Cluster:
- Data partitioned across multiple nodes (sharding)
- Uses hash slots (0–16383)
- Cluster-aware client libraries are required
8. Production Considerations
Security:
- AUTH with password
- ACLs (Access Control Lists)
- Network restrictions via bind and protected-mode

Monitoring:
- INFO command for server statistics
- MONITOR for debugging
- RedisInsight GUI for visual monitoring

Backup & Restore:


- SAVE and BGSAVE
- AOF rewriting with BGREWRITEAOF

9. Redis with Java (Spring Boot)


- Add Redis dependency (Lettuce or Jedis)
- RedisTemplate<String, Object> for key-value operations

Example:
@Autowired
private StringRedisTemplate redisTemplate;
redisTemplate.opsForValue().set("name", "Alice");
String name = redisTemplate.opsForValue().get("name");

Caching with Spring Boot:


- @Cacheable, @CachePut, @CacheEvict
- Configure cacheManager in Redis config

10. Advanced Use Cases


Distributed Locking (RedLock Algorithm):
- SET key value NX PX 3000 for lock
- Validate before releasing the lock

Rate Limiting:
- INCR and EXPIRE for per-user rate control
- Use Lua for atomic counters

Streams for Message Queues:


- Create consumer groups with XGROUP
- XREADGROUP for consuming messages

Modules:
- RedisBloom: Bloom filters, Count-min sketch
- RedisGraph, RedisJSON, RedisTimeSeries for advanced data

Practice Projects
1. URL Shortener using Redis hashes and TTL
2. Leaderboard using Sorted Sets
3. Chat app with Pub/Sub or Streams
4. API Rate Limiter with counters and expiry
5. Session management using Redis Hashes in Spring Boot or Flask

You might also like