Databases 6 min read

Understanding Redis Rehash Mechanism and Implementation

This article explains how Redis uses a dual‑hashtable structure and a progressive rehash process to handle hash collisions, detailing the rehash conditions, migration steps, and performance implications for in‑memory key‑value storage.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Understanding Redis Rehash Mechanism and Implementation

Redis is a high‑performance in‑memory database that stores key‑value pairs using a dictionary implemented as a hash table. When the number of entries grows, hash collisions increase, so Redis performs a rehash to keep lookup efficiency and memory usage optimal.

The dictionary contains two hash tables: the active one (ht[0]) and a secondary table (ht[1]) used as a temporary target during rehashing. When the load factor reaches certain thresholds, Redis allocates ht[1] with an appropriate size (a power‑of‑two greater than the required capacity) and begins migrating entries.

Redis dictionary structure
Redis dictionary structure

Rehash conditions

When no BGSAVE or BGREWRITEAOF is running and load_factor ≥ 1, Redis expands the hash table.

When a BGSAVE or BGREWRITEAOF is running and load_factor ≥ 5, Redis also expands the hash table.

If load_factor < 0.1, Redis shrinks the hash table.

Rehash process

Allocate space for the secondary hash table (ht[1]) based on the number of used nodes.

Initialize a rehash index (rehashidx) to 0; -1 indicates no rehash in progress.

During normal operations (add, delete, lookup, update), Redis moves a small batch of entries from ht[0] at rehashidx to ht[1] and increments rehashidx.

The serverCron function also performs incremental rehash work (e.g., 100 elements per millisecond) until all entries are migrated, after which rehashidx is set to -1.

Example steps:

Rehash starts and ht[1] is initialized.

Initialize ht[1]
Initialize ht[1]

Key k2 is rehashed.

Rehash k2
Rehash k2

Rehash completes and ht[0] is cleared.

Rehash complete
Rehash complete

The incremental rehash avoids a large, blocking operation, but during rehash a lookup may need to check both ht[0] and ht[1], potentially doubling the access cost for keys that have already moved.

Conclusion

This progressive rehash strategy reduces the computational and memory overhead of a full rehash, while ensuring Redis maintains high performance even as the dataset scales.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceredishash tableIn-Memory DatabaseRehash
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.