Redis
Redis Data Types
| Type | Use Case |
|---|---|
| String | Simple cache, counters |
| Hash | Object fields |
| List | Queues, recent items |
| Set | Unique items, tags |
| Sorted Set | Leaderboards, ranges |
Spring Boot + Redis
@Service
public class CacheService {
@Autowired
private StringRedisTemplate redis;
public void cache(String key, String value, Duration ttl) {
redis.opsForValue().set(key, value, ttl);
}
public String get(String key) {
return redis.opsForValue().get(key);
}
}
Redis Patterns
Data Structures
| Type | Use Case |
|---|---|
| String | Simple values, counters |
| Hash | Objects, user profiles |
| List | Queues, activity feeds |
| Set | Tags, unique items |
| Sorted Set | Leaderboards, time series |
Best Practices
- Use connection pooling
- Set TTL for all keys
- Monitor memory usage
- Use pipelines for bulk operations
Common Patterns
- Rate limiting with sliding window
- Session storage
- Pub/Sub for real-time updates
Key Points
- Understanding Redis is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Redis in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Redis implementation
// Key aspects: validation, error handling, logging, testing
public class Redis {
// Production-ready implementation
}Identify and handle edge cases for Redis. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Redis. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. Redis is primarily?
2. Redis default port?
3. What is the primary purpose of Redis?
4. What is a common mistake when implementing Redis?
Flashcards
Question
Redis primary use?
Click to reveal answer
Answer
In-memory data store for caching
Question
Redis port?
Click to reveal answer
Answer
6379
Question
What is Redis?
Click to reveal answer
Answer
Redis is a key concept in backend development.
Question
When to use Redis?
Click to reveal answer
Answer
Use Redis when building production systems that require reliability, scalability, and maintainability.
Question
Redis best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Redis is in-memory data store
- 2.Supports: String, Hash, List, Set, Sorted Set
- 3.Spring: StringRedisTemplate or RedisTemplate
- 4.Used for caching, sessions, queues
Interview Tips
- •Use Redis for caching
- •Know Redis data types
Cheat Sheet
Redis
- In-memory data store
- Types: String, Hash, List, Set, Sorted Set
- Port: 6379
- Spring: StringRedisTemplate