AWS Builder Center

Hashing vs B+ trees: how DynamoDB and RDS find your data

Why can a DynamoDB partition key only be matched exactly, while a sort key supports ranges? The answer is the hashing vs B+ tree trade-off from your DBMS and data structures syllabus.

Series: From Textbook to Cloud: GATE CS Concepts in AWS (4 articles)

  1. 2
    Hashing vs B+ trees: how DynamoDB and RDS find your data This article
Comp Sci student with overflowing curiosity
In Part 1, I mapped six GATE CS topics to AWS services. This week I'm going deeper on one pair every GATE aspirant knows well: hashing and B+ trees. Once you see them in DynamoDB and RDS, a lot of AWS design advice suddenly makes sense.

The textbook trade-off

Here's the comparison from our syllabus:

Hashing
B+ tree
Exact-match lookup
O(1) average
O(log n)
Range queries ("between 10 and 50")
Not supported efficiently
Efficient, because leaf nodes are linked in sorted order
Data order
Scattered
Sorted
Hashing is fast but forgets order. A B+ tree is slightly slower per lookup but keeps everything sorted. Keep this table in mind for the rest of the article.

DynamoDB: hashing first, then sorting

Every DynamoDB table has a partition key, and optionally a sort key.
The partition key is hashed. DynamoDB passes the partition key through an internal hash function, and the result decides which physical partition stores the item. This is the hashing chapter, applied to distributed storage.
That explains a rule that confuses many beginners: a  Query  must match the partition key exactly. You can't ask for "partition keys between A and M", because hashing destroyed the ordering, just like in a hash table.
The sort key keeps order. All items with the same partition key are stored together, sorted by the sort key. So within one partition key, you can use range conditions like  between ,  begins_with ,  >  and  < . It's the "sorted, range-friendly" side of the table above.
Example: for a student-results table:
  • Partition key:  student_id  (hashed, exact match)
  • Sort key:  exam_date  (sorted, so you can query a range)
"Get all results for student 101 between January and March" is one efficient  Query . "Get all results in March for every student" isn't, because it would need a  Scan  that reads the whole table.

Hot partitions: the load factor problem in disguise

In hashing, a bad hash function or skewed keys pile entries into the same bucket. DynamoDB has the same problem: each partition has a throughput limit, so if most requests hit the same partition key, that partition becomes hot and requests get throttled, even if the table as a whole has spare capacity.
The fix is what our textbook says about good hashing: choose keys with high cardinality and even distribution.  student_id  spreads well.  exam_status  (only "pass" or "fail") doesn't.

Secondary indexes: when you need a different order

What if you need to query by something other than your keys?
  • Global Secondary Index (GSI): a different partition key and sort key, like building a second hash-plus-sorted structure on other attributes. Reads from a GSI are eventually consistent, which links nicely to the consistency models from Part 1.
  • Local Secondary Index (LSI): the same partition key with a different sort key. It must be defined when the table is created.
Both are the DBMS idea of a secondary index: extra structures maintained on writes so certain reads become fast.

RDS: the B+ tree you studied, almost exactly

If you run MySQL or PostgreSQL on Amazon RDS, you're using B+ trees (PostgreSQL's default index type is a B-tree) directly:
  • MySQL InnoDB stores table rows in a clustered B+ tree index ordered by the primary key. Secondary indexes are separate B+ trees that point back to the primary key.
  • That's why range queries like  WHERE marks BETWEEN 60 AND 80  are efficient on an indexed column: the database descends the tree once, then walks the linked leaves.
So the GATE question "what's the minimum height of a B+ tree of order p with n keys?" is literally about how many disk pages your database reads per lookup.

The one-line summary

Partition key = hashing (fast, exact match only). Sort key and RDS indexes = sorted structures (range-friendly).
If you remember the textbook trade-off, you already know half of DynamoDB data modelling.

Try this GATE-style question

A DynamoDB table uses  city  as the partition key, and 90% of requests are for  city = "Mumbai" . What problem will occur, and which hashing concept does it correspond to?
Answer in the comments. I'll reply to everyone.
Next week, Part 3: TCP/IP and the OSI layers. What actually happens when you open an AWS-hosted URL.

Series: From Textbook to Cloud: GATE CS Concepts in AWS (4 articles)

  1. 2
    Hashing vs B+ trees: how DynamoDB and RDS find your data This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article