75 Popular Redis Interview Questions ?

Here’s a list of popular Redis interview questions tailored for developers, with concise yet insightful answers. These questions span Redis fundamentals, Spring Boot integration, use cases, and advanced features, making them ideal for preparing for technical interviews.


Redis Interview Questions and Answers

Fundamentals

  1. What is Redis?
    • Answer: Redis (Remote Dictionary Server) is an open-source, in-memory data structure store used as a database, cache, and message broker, supporting data types like strings, lists, sets, and hashes.
  2. What are the main advantages of Redis?
    • Answer: High performance (in-memory), versatile data structures, simplicity, scalability, and support for persistence and replication.
  3. How does Redis differ from traditional databases like MySQL?
    • Answer: Redis is in-memory (faster), key-value based, non-relational, and lacks complex querying (e.g., no SQL joins) compared to MySQL’s disk-based, relational model.
  4. What data types does Redis support?
    • Answer: Strings, Hashes, Lists, Sets, Sorted Sets, Bitmaps, HyperLogLogs, and Geospatial Indexes.
  5. What is the maximum size of a Redis key or value?
    • Answer: Up to 512 MB for both keys and values.

Configuration and Operations

  1. How do you install Redis locally?
    • Answer: Download from redis.io, extract, and run make && make install, or use Docker: docker run -d -p 6379:6379 redis.
  2. How do you connect to Redis using Redis CLI?
    • Answer: Run redis-cli for local (default localhost:6379), or redis-cli -h <host> -p <port> for remote.
  3. How do you list all keys in Redis?
    • Answer: Use KEYS * (avoid in production due to blocking); prefer SCAN for scalability: SCAN 0 MATCH *.
  4. How do you set and get a value in Redis?
    • Answer: SET key value to store, GET key to retrieve.
  5. What is the difference between SET and SETNX?
    • Answer: SET overwrites existing keys; SETNX (Set if Not Exists) only sets if the key doesn’t exist.

Persistence and Scalability

  1. How does Redis persist data?
    • Answer: Via RDB (snapshotting) for point-in-time backups and AOF (append-only file) for logging all write operations.
  2. What is the difference between RDB and AOF?
    • Answer: RDB saves a snapshot periodically (faster recovery, data loss risk); AOF logs every write (slower, more durable).
  3. How does Redis handle replication?
    • Answer: Uses a master-slave model; slaves replicate master data asynchronously with REPLICAOF <master>.
  4. What is Redis Sentinel?
    • Answer: A system for monitoring Redis instances, managing failover, and providing high availability.
  5. What is Redis Cluster?
    • Answer: A distributed Redis setup that shards data across multiple nodes using hash slots (16384 slots) for scalability.

Spring Boot Integration

  1. How do you integrate Redis with Spring Boot?
    • Answer: Add spring-boot-starter-data-redis dependency, configure spring.data.redis.host/port in application.properties, and use RedisTemplate.
  2. What is RedisTemplate in Spring Boot?
    • Answer: A Spring Data Redis class for interacting with Redis, providing operations like opsForValue(), opsForHash(), etc.
  3. How do you enable caching with Redis in Spring Boot?
    • Answer: Add @EnableCaching, set spring.cache.type=redis, and use @Cacheable, @CacheEvict, etc.
  4. How do you customize serialization in RedisTemplate?
    • Answer: Configure with a serializer like Jackson2JsonRedisSerializer in a @Bean:javatemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
  5. How do you handle connection pooling with Redis in Spring Boot?
    • Answer: Spring Boot auto-configures Lettuce or Jedis pooling; tweak via spring.data.redis.lettuce.pool.max-active (e.g., max-active=8).

Use Cases

  1. What are common use cases for Redis?
    • Answer: Caching, session storage, real-time analytics, leaderboards, pub/sub messaging, and rate limiting.
  2. How can Redis be used for caching?
    • Answer: Store frequently accessed data with TTL (e.g., SET key value EX 60) to reduce database load.
  3. How does Redis support pub/sub messaging?
    • Answer: Uses channels; PUBLISH channel message sends, SUBSCRIBE channel receives.
  4. How can Redis implement a leaderboard?
    • Answer: Use Sorted Sets with ZADD leaderboard score player and ZRANGE leaderboard 0 -1 WITHSCORES.
  5. How does Redis handle rate limiting?
    • Answer: Use atomic counters (INCR) with TTL (e.g., INCR user:rate EX 60) to track and limit requests.

Advanced Features

  1. What are Redis Hashes?
    • Answer: Key-value mappings within a single key; e.g., HMSET user:1 name “Alice” age “30”.
  2. How do you use Redis Sorted Sets?
    • Answer: Store elements with scores; e.g., ZADD set 1 “item1”, retrieve with ZRANGE.
  3. What are Redis Geospatial Indexes?
    • Answer: Store latitude/longitude pairs; e.g., GEOADD locations 13.361 52.516 “Berlin”, query with GEORADIUS.
  4. What is Redis Lua scripting?
    • Answer: Execute custom logic on the server; e.g., EVAL “return redis.call(‘SET’, KEYS[1], ARGV[1])” 1 key value.
  5. How does Redis handle transactions?
    • Answer: Use MULTI, followed by commands, then EXEC to execute atomically; e.g., MULTI, SET k1 v1, EXEC.

Performance and Monitoring

  1. Why is Redis so fast?
    • Answer: In-memory storage, single-threaded event loop, and optimized data structures.
  2. What is the Redis event loop?
    • Answer: A single-threaded, non-blocking mechanism handling client requests via multiplexing.
  3. How do you monitor Redis performance?
    • Answer: Use INFO command (e.g., INFO MEMORY, INFO STATS) or tools like Prometheus/Grafana.
  4. What is the SLOWLOG command?
    • Answer: Logs slow commands; e.g., SLOWLOG GET shows recent slow operations.
  5. How do you check memory usage in Redis?
    • Answer: INFO MEMORY → used_memory_human shows current usage.

Challenges and Limitations

  1. What are the limitations of Redis?
    • Answer: Memory-bound (data must fit in RAM), single-threaded (CPU-limited), no complex querying.
  2. How does Redis handle data larger than memory?
    • Answer: It doesn’t; you’d use eviction policies (e.g., LRU) or scale with clustering.
  3. What is the eviction policy in Redis?
    • Answer: Rules for removing keys when memory is full; e.g., volatile-lru (LRU on keys with TTL), set via maxmemory-policy.
  4. What happens if Redis runs out of memory?
    • Answer: Depends on maxmemory-policy: evicts keys or returns errors (e.g., OOM command not allowed).
  5. How do you secure Redis?
    • Answer: Set a password (requirepass in redis.conf), use TLS, restrict access with firewall rules.

Spring Boot-Specific Advanced

  1. How do you use Redis pub/sub in Spring Boot?
    • Answer: Configure RedisMessageListenerContainer and use redisTemplate.convertAndSend().
  2. How do you implement rate limiting with Redis in Spring Boot?
    • Answer: Use redisTemplate.opsForValue().increment() with TTL in a service method.
  3. How do you visualize Redis metrics in Spring Boot?
    • Answer: Use Actuator with micrometer-registry-prometheus, scrape with Prometheus, and visualize in Grafana.
  4. What’s the difference between Lettuce and Jedis in Spring Boot?
    • Answer: Lettuce is default, async, Netty-based; Jedis is sync, simpler, requires explicit dependency.
  5. How do you handle Redis connection failures in Spring Boot?
    • Answer: Configure retry policies (e.g., spring.data.redis.lettuce.pool.max-wait) or use Circuit Breaker (e.g., Resilience4j).

Tricky Questions

  1. Can Redis be used as a primary database?
    • Answer: Yes, for simple, high-speed use cases with persistence enabled, but lacks relational features.
  2. What’s the difference between EXPIRE and SETEX?
    • Answer: EXPIRE sets TTL on an existing key; SETEX sets a key with TTL in one command.
  3. How does Redis Cluster handle failover?
    • Answer: Uses master-slave replication; if a master fails, a slave is promoted via cluster consensus.
  4. What is the Redis SCAN command, and why use it?
    • Answer: Iterates keys non-blocking; safer than KEYS for large datasets.
  5. How do you optimize Redis performance?
    • Answer: Use pipelining, appropriate data structures, clustering, and tune maxmemory and eviction policies.

Tips for Interviews

  • Know Commands: Be ready to write basic Redis CLI commands (e.g., SET, HGETALL, ZADD).
  • Spring Boot Context: Relate answers to RedisTemplate, caching annotations, or metrics.
  • Practical Examples: Mention caching, pub/sub, or rate limiting use cases.
  • Trade-offs: Highlight Redis’ strengths (speed) vs. limitations (memory).

Leave a Reply

Your email address will not be published. Required fields are marked *