Prev Next

Database / REDIS

Explain the internal working of Redis's hash table resizing (rehashing)?

Redis's core keyspace (and large Hash-type values) are backed by a hash table, and as entries are added or removed, the table needs to grow or shrink to keep lookups close to O(1) — but a naive full rehash (allocate a new table, move every entry, free the old one) would briefly block every other operation on a table with millions of entries, which conflicts directly with Redis's single-threaded, low-latency design.

flowchart TD A[Load factor crosses a threshold, growth or shrink needed] --> B[Allocate a second, differently-sized hash table] B --> C[Rehashing flag set: both old and new tables now active simultaneously] C --> D[Each subsequent command incrementally moves one bucket's worth of entries from old to new table] D --> E{All buckets migrated?} E -- No --> F[Old table still holds unmigrated entries; lookups check both tables during this period] F --> D E -- Yes --> G[Old table freed, new table becomes the sole active table]

This incremental rehashing spreads the cost of a full table resize across many individual commands instead of pausing everything for one large operation, which is what lets Redis resize its hash tables even under heavy load without a noticeable latency spike. During the migration window, any lookup for a key has to check both tables (since it might not have been migrated yet), which is a small, bounded per-operation cost rather than one large blocking one.

Why doesn't Redis perform a full hash table rehash in one blocking step?
What must a lookup do while incremental rehashing is in progress?

More Related questions...

What is Redis? What is a Redis key? What is the purpose of the EXPIRE command? List few Redis Datatypes. What is a Redis Sorted Set? Advantages of Redis. What programming languages does Redis support? What is a Redis Hash used for? What is a Redis Set data type used for? Explain Replication in Redis. What is the purpose of the INCR command? Explain about REDIS security. What is a Redis Stream? Expand REDIS. Define a Redis Bitmap? In which language Redis is developed? What is the purpose of Redis Pub/Sub? Difference between SET and MSET command in REDIS. What are Redis transactions? Explain LPUSH command in REDIS. What is RDB persistence in Redis? Limitations of REDIS. What is AOF persistence in Redis? REDIS is fast, but is it also durable? Difference between Memcached and REDIS. What is the purpose of the SELECT command in Redis? What is the purpose of the TTL command? What is SADD command in REDIS? Define eviction policies in Redis? Mention few LIST operations in REDIS. What is Redis Sentinel used for? Mention Spring Boot Drivers for REDIS. Explain about redisson client for redis. What is Redis Cluster? How do you connect to Redis using the CLI? Why is Redis often faster than a traditional relational database for caching? What is a Cache Penetration Problem? What is Valkey, and how does it relate to Redis? How I/O Multiplexing Works in Redis? What is the difference between RDB and AOF persistence? How does Redis achieve high throughput with a mostly single-threaded design? Why should you configure an eviction policy for a Redis instance used as a cache? How does Redis Cluster shard data across nodes? When should you use Redis Sentinel instead of Redis Cluster? What happens when a Redis master fails in a Sentinel-managed setup? Explain the execution flow of a Redis transaction using MULTI/EXEC? How can you optimize Redis memory usage for large datasets? How do you troubleshoot high memory usage in a Redis instance? Why is the maxmemory-policy setting important for a cache-only Redis deployment? Explain the lifecycle of a key with a TTL set in Redis? How does Redis handle atomicity for multi-key operations? What is the difference between a Redis List and Sorted Set for queues? How do you implement a distributed lock using Redis? How does Redis Streams support consumer groups? Which is better and why: Redis Pub/Sub or Redis Streams for event delivery? How do you integrate Redis as a session store in a web application? Explain the internal working of Redis's hash table resizing (rehashing)? How do you configure Redis persistence for a production deployment? What is the difference between Redis Cluster and client-side sharding? How does Redis support Lua scripting for atomic operations? When would you choose Redis Streams over a message broker like Kafka? How do you secure Redis access using ACLs? Why should the KEYS command be avoided in production? How do you configure Redis for cache-aside pattern usage? Explain the execution flow of a Redis Cluster request with a MOVED redirection? How does Redis handle replication lag between a master and its replicas? Why doesn't Redis guarantee strong consistency by default across replicas? How do you mitigate a Cache Avalanche in a Redis-backed system? What is the difference between Redis's diskless replication and disk-based replication?
Show more question and Answers...

Apache Cassandra Interview Questions

Comments & Discussions