Redis

Redis Interview Questions and Answers

  1. What is Redis? – Redis is an in-memory, key-value data store known for high performance, supporting data structures such as strings, hashes, lists, sets, sorted sets.

  2. What data types does Redis support? – Strings, Lists, Sets, Sorted Sets (zsets), Hashes, Bitmaps, HyperLogLogs, Streams, and Geospatial indexes.

  3. How do you configure Redis to require a password?

    requirepass yourpassword
    
  4. What is the default Redis port?
    Answer: 6379

  5. How do you secure Redis from external access?

    • Bind Redis to 127.0.0.1

    • Use firewalls to block the port externally

    • Set requirepass

    • Rename or disable dangerous commands with rename-command

Section 2: Persistence and Backup

  1. Explain RDB vs AOF persistence. – RDB saves snapshots at intervals; AOF logs every write operation. AOF provides better durability; RDB is faster on recovery.

  2. How does Redis handle persistence if both AOF and RDB are enabled?
    Answer: Redis loads AOF at startup if both exist.

  3. How do you backup a Redis instance? – Copy dump.rdb and/or appendonly.aof files.

  4. What is copy-on-write (COW) in Redis? – Technique used during BGSAVE or BGREWRITEAOF where parent process forks a child; parent continues while child writes snapshot.

  5. How do you trigger manual persistence?

    SAVE  # synchronous save
    BGSAVE  # asynchronous save
    

Section 3: Replication and High Availability

  1. How does Redis replication work? – Replicas connect to master, sync DB snapshot, then receive ongoing changes.

  2. How to configure a replica?

    replicaof master_ip master_port
    
  3. How do you enforce read-only replicas?

    replica-read-only yes
    
  4. What is Redis Sentinel? – Sentinel monitors masters, performs failover, notifies clients.

  5. Explain failover delay settings in Sentinel. – Controls how long Sentinel waits before initiating failover.

Section 4: Clustering

  1. What is Redis Cluster? – Redis Cluster provides sharding and replication across multiple nodes for scalability and HA.

  2. Explain the role of hash slots. – 16,384 hash slots partition keys across nodes; each node owns a subset.

  3. How to check if a node is in a cluster?

    CLUSTER INFO
    
  4. How do you list cluster nodes?

    CLUSTER NODES
    
  5. What is CLUSTER FAILOVER? – Command to promote a replica to master.

Section 5: Key Management and Expiration

  1. How to set a key with expiration?

    SET mykey value EX 60
    
  2. How do you expire keys by pattern?

    redis-cli --scan --pattern "prefix:*" | xargs redis-cli DEL
    
  3. What happens when Redis memory is exhausted? – Redis evicts keys based on eviction policy.

  4. Explain eviction policies. – e.g., volatile-lru, allkeys-lru, noeviction, volatile-ttl.

  5. What is lazy freeing? – Keys are deleted asynchronously to avoid blocking main thread.

Section 6: Transactions and Atomicity

  1. Explain Redis transactions. – Group multiple commands with MULTI/EXEC; queued and executed sequentially.

  2. How to perform check-and-set (CAS)?

    WATCH mykey
    MULTI
    SET mykey newval
    EXEC
    
  3. What is DISCARD? – Cancels a transaction after MULTI.

  4. What happens if a command fails in MULTI? – Other commands still run; no rollback.

  5. How to atomically fetch and delete a key?

    GETDEL mykey
    

Section 7: Advanced Data Structures

  1. Explain sorted sets. – Like sets, but each member has a score for ordering.

  2. How does ZPOPMIN work? – Removes and returns member with smallest score.

  3. What is ZLEXCOUNT? – Counts members within a lex range.

  4. What is a Redis GEO command?

    GEOADD locations 13.361389 38.115556 "Palermo"
    GEORADIUS locations 15 37 200 km
    
  5. What is BITFIELD used for? – Operates on individual bits or ranges in a string.

Section 8: Streams and Pub/Sub

  1. What is Redis Streams? – A data structure for append-only log with IDs.

  2. How to read from a stream?

    XREAD COUNT 1 STREAMS mystream >
    
  3. Explain XREADGROUP. – Read from a stream as part of a consumer group.

  4. What is a pending entry? – A message delivered but not acknowledged in a consumer group.

  5. How to claim pending messages?

    XCLAIM mystream mygroup myconsumer 0 ID
    

Section 9: Monitoring and Debugging

  1. How do you measure latency?

    LATENCY DOCTOR
    LATENCY LATEST
    
  2. What is INFO commandstats? – Shows statistics per command.

  3. What is CLIENT LIST used for? – Displays connected clients and their info.

  4. How to check replication lag?

    INFO replication
    
  5. How to inspect memory usage of a key?

    MEMORY USAGE mykey
    

Section 10: Performance and Scaling

  1. What is MEMORY DOCTOR? – Analyzes memory for inefficiencies.

  2. How to handle hot keys? – Cache busting, sharding, or client-side caching.

  3. How to benchmark Redis?

    redis-benchmark
    
  4. What is WAIT command? – Ensures writes propagate to replicas.

  5. Explain Redis modules. – Loadable extensions to add new commands.

Section 11: Security and ACL

  1. How to rename dangerous commands?

    rename-command FLUSHALL ""
    
  2. How to enable ACL?

    ACL SETUSER alice on >password ~* +@all
    
  3. How to disable a user?

    ACL SETUSER alice off
    
  4. How does Redis handle unauthorized access? – Returns NOAUTH error.

  5. What is AUTH command? – Authenticate a client.

Section 12: Miscellaneous

  1. What is SWAPDB? – Swaps two databases.

  2. How does UNLINK differ from DEL?UNLINK deletes asynchronously.

  3. How to migrate keys?

    MIGRATE host port key dest-db timeout
    
  4. What is MODULE LOAD? – Loads a Redis module.

  5. What is RedisTimeSeries? – Module for storing time-series data.


Section 13: Redis Scripting and Lua

  1. What is Redis scripting? – Ability to run Lua scripts atomically in Redis.

  2. How do you execute a Lua script?

    EVAL "return redis.call('get', KEYS[1])" 1 mykey
    
  3. What are the advantages of Redis scripting? – Atomicity, reduced network round-trips, complex operations.

  4. Explain EVALSHA. – Executes a cached script by SHA1 hash instead of the full script.

  5. How do you preload a script?

    SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])"
    

Section 14: Redis Internals and Memory Management

  1. How does Redis handle memory allocation? – Uses jemalloc by default for efficiency.

  2. What is maxmemory-policy? – Defines eviction policy when maxmemory is reached.

  3. What is an object encoding? – Internal representation of Redis data (e.g., ziplist, hashtable).

  4. How to inspect object encoding?

    OBJECT ENCODING mykey
    
  5. Explain lazy deletion vs active expiration. – Lazy deletion removes expired keys upon access; active expiration scans keys periodically.

Section 15: Redis Troubleshooting and Recovery

  1. How do you fix LOADING Redis is loading the dataset? – Wait for load or restart if stuck.

  2. What causes OOM command not allowed? – Memory limit reached with noeviction policy.

  3. How to recover a corrupted AOF? – Use redis-check-aof --fix.

  4. How to recover a corrupted RDB? – Use redis-check-rdb.

  5. What is INFO persistence for? – Provides details about background saves.

Section 16: Redis Cluster Advanced Topics

  1. What is a cluster failover vs manual failover? – Automatic failover is triggered by failure detection; manual initiated via CLUSTER FAILOVER.

  2. Explain migrating slots.CLUSTER SETSLOT commands move ownership.

  3. How do you rebalance a Redis Cluster? – Use redis-cli --cluster rebalance.

  4. What is CLUSTER BUMPEPOCH? – Forces an epoch increment to resolve conflicts.

  5. How does Redis handle split-brain in cluster mode? – Majority vote prevents conflicting masters.

Section 17: Redis Pub/Sub Advanced

  1. What happens to published messages when no subscribers? – Messages are discarded.

  2. How to unsubscribe from a channel?

    UNSUBSCRIBE mychannel
    
  3. Can Pub/Sub be persisted? – No; messages are ephemeral.

  4. What is keyspace notification?

    CONFIG SET notify-keyspace-events Ex
    SUBSCRIBE "__keyevent@0__:expired"
    
  5. How to monitor all Pub/Sub channels?

    PSUBSCRIBE *
    

Section 18: Redis Development and Integration

  1. How do you connect to Redis in Python?

    import redis
    r = redis.Redis(host='localhost', port=6379)
    r.set('foo', 'bar')
    
  2. What Java library is popular for Redis? – Jedis or Lettuce.

  3. Explain Redis pipelining. – Send multiple commands without waiting for replies to reduce latency.

  4. What is Redis client-side caching? – Allows clients to locally cache data with invalidation support.

  5. How to use Redis with Node.js?

    const redis = require('redis');
    const client = redis.createClient();
    client.set('key', 'value');
    

Section 19: Redis Benchmarking and Tuning

  1. What is latency-monitor-threshold? – Threshold in microseconds for latency monitoring.

  2. How to profile command performance? – Use MONITOR or LATENCY GRAPH.

  3. How to disable saving snapshots? – Remove save lines in redis.conf.

  4. What does hash-max-ziplist-entries control? – Threshold to switch hash encoding.

  5. How to detect large keys?

    MEMORY USAGE key
    SCAN + MEMORY USAGE
    

Section 20: Redis Limitations and Alternatives

  1. What are Redis single-thread limitations? – Only one command processed at a time per instance.

  2. How to horizontally scale beyond Redis Cluster? – Use proxies like Twemproxy or client-side sharding.

  3. When is Redis not a good fit? – Large dataset requiring disk-based queries; complex joins.

  4. What is an alternative to Redis for persistence? – RocksDB, PostgreSQL, or Cassandra.

  5. How to migrate from Redis to another store? – Use DUMP and restore logic or ETL tools.


Scroll to Top