Redis Questions and Answers

 

1. What is Redis and why is it fast?

Redis is an in-memory key-value data store that supports multiple data structures like strings, hashes, lists, sets, and sorted sets.

Why fast?

  • In-memory storage (RAM)

  • Single-threaded event loop (no locking overhead)

  • Efficient C implementation

  • O(1) operations for most commands

2. What are Redis data types?

  • String

  • List

  • Set

  • Sorted Set

  • Hash

  • Stream

  • Bitmap

  • HyperLogLog

3. What is the difference between RDB and AOF?

FeatureRDB    AOF
Type                Snapshot                Append log
Performance                Faster               Slightly slower
Data Safety                Possible data loss                   Safer
File Size                Smaller               Larger

Best practice: Use both in production.

4. How does Redis handle concurrency?

Redis is single-threaded for command execution but uses:

  • I/O multiplexing

  • Event loop

  • Non-blocking I/O

This avoids race conditions internally.

5. What is Redis persistence?

  • RDB (Point-in-time snapshot)

  • AOF (Logs every write operation)

6. What is Redis eviction policy?

When memory is full:

  • noeviction

  • allkeys-lru

  • volatile-lru

  • allkeys-random

  • volatile-ttl

7.What is the use of Redis in Django?

With:

  • Django – Caching backend

  • Celery – Message broker

8. How to implement rate limiting in Redis?

Use INCR + EXPIRE:

INCR user:1:requests
EXPIRE user:1:requests 60

9. What is Redis Pub/Sub?

Publish messages to a channel, subscribers receive instantly.

10. What are limitations of Redis?

  • Data stored in RAM

  • No complex joins

  • Not suitable for heavy relational queries

11.What is Pub/Sub?

Redis Publish/Subscribe allows real-time messaging between services.

Example:

SUBSCRIBE chat

In another terminal:

PUBLISH chat "Hello World"

How It Works

  • Publisher sends message

  • Redis pushes message to subscribers

  • No message persistence

  • No acknowledgment mechanism

12.When to Use Pub/Sub?

✔ Real-time notifications
✔ Chat applications
✔ Live dashboards
✔ Microservice communication

Limitations

  • No durability

  • If subscriber is offline → message lost

  • No retry mechanism

For reliable messaging, consider:

  • Redis Streams

  • Or tools like Apache Kafka

13.Redis vs Memcached

Memcached is another in-memory caching system.

FeatureRedisMemcached
Data Types            Multiple    Strings only
Persistence            Yes    No
Replication            Yes    No
Pub/Sub            Yes    No
Clustering            Native    Client-based
Use Case            Cache + DB + Queue    Simple cache

When to Use Memcached?

  • Simple caching

  • Extremely high read traffic

  • No persistence required

When to Use Redis?

  • Complex caching

  • Leaderboards

  • Background jobs

  • Real-time systems

Redis is more versatile and production-ready for modern apps.

14.Redis Cluster & Sentinel in Production

What is Redis Sentinel?

Redis Sentinel provides:

  • Monitoring

  • Automatic failover

  • Master election

  • High availability

What is Redis Cluster?

Redis Cluster:

  • Shards data across multiple nodes

  • Provides horizontal scalability

  • Automatically partitions data

Architecture Comparison

Feature                        Sentinel         Cluster
High Availability                        Yes                Yes
Sharding                        No                Yes
Failover                        Automatic                Automatic
Scaling                        Vertical                Horizontal

When to Use Sentinel?

  • Single master setup

  • Need HA

  • Moderate data size

When to Use Cluster?

  • Large dataset

  • High throughput

  • Need horizontal scaling

Production Best Practices

✔ Use Redis for caching, not as primary DB
✔ Enable AOF + RDB
✔ Set memory limits
✔ Monitor using Redis INFO
✔ Use connection pooling
✔ Avoid large keys




Comments

Popular posts from this blog

Database Integration in FastAPI (SQLAlchemy CRUD)

Middleware & CORS in FastAPI

Python Data Handling