Course contentsShow
← Software Engineering
Lesson 1398 of 1,93341. Scalability and Distributed SystemsFree lesson

Partitioning Strategies: Hash-Based

Applying hash functions to keys for uniform distribution, avoiding hotspots but sacrificing range query efficiency.

Partitioning Strategies: Hash-Based

You'll learn how hash-based partitioning distributes data uniformly across partitions using hash functions, eliminating hotspots at the cost of range query performance.

The Core Idea

Hash-based partitioning applies a hash function to each record's key (like a user ID or order ID) and uses the result to determine which partition stores that record. The hash function scrambles the input in a deterministic way, spreading keys evenly across available partitions regardless of their actual values.

Think of it like assigning students to dorm rooms by hashing their student ID numbers. Even if IDs 1000-1500 all enrolled together, the hash function ensures they're scattered across buildings A, B, C, and D roughly equally.

How It Works

When you insert or query data:

  1. Apply a hash function to the partition key: hash(key)
  2. Use modulo to map to a partition: partition_number = hash(key) % total_partitions
  3. Route the operation to that specific partition

For example, if hash("user_12345") = 7829 and you have 4 partitions: 7829 % 4 = 1 → data goes to partition 1.

Benefits and Trade-offs

Uniform distribution: Hash functions distribute data evenly, preventing any single partition from becoming overloaded (avoiding hotspots from range-based partitioning).

Sacrificing range queries: You cannot efficiently query ranges like "all users from A to M" because consecutive keys hash to random partitions. Each partition must be checked, eliminating the locality benefit that range-based partitioning provided.

Best for: Point lookups (single key queries) and workloads where even distribution matters more than range scanning.

Key Takeaway: Hash-based partitioning trades range query efficiency for uniform data distribution, using hash functions to scatter keys randomly across partitions and eliminate hotspots.