DynamoDB PrimaryKey, HashKey, SortKey (RangeKey)
Let's check out DynamoDB and explore PrimaryKey, hashKey, and SortKey. Also take as look at a visual.
Join the DZone community and get the full member experience.
Join For FreeLast week, I came across DynamoDB. Over the past few years, I have been fascinated by how the industry went from relational to NoSQL to NewSQL and then spread to all directions, collapsing into MySQL/Postgres, etc. The whole thing is both funny and fascinating.
From my past experience, whenever we used a KV store, we paid big time. Scalability comes with a check, you pay for losing features, and you gain by having high performance, so you have to tradeoff — Economy 101.
Anyway, I started reading about DynamoDB, which is a managed KV store, and noticed that there is complexity and powerfulness in its key structure and in its indexing. It took me some time to learn how it’s keysiness is working out and it took me some time to get how indexing works. I’m still unsure whether I got it or not. I want to share with you my visual understanding of it:
There are 3 basic building blocks or terms you should get familiar while first learning about DynamoDB, your data resides at:
- Table
- Item
- KV Attribute.
This is a 3 level hierarchy. It first belongs to a table (some use a single table), then to an item, and then your internal actual data values are in internal KV-attributes inside items.
You can fetch an item or items by its primary key. The way to fetch multiple items by a primary key query (which sounds weird at first), is to specify the hash key and then a range on the range key.
It’s as if your key is split in two.
Part 1: You have to specify it fully
Part 2: You can specify a range on it
More visually, as it's complex, this is the way I see it:
![](https://cdn-images-1.medium.com/max/1600/1*s2olDcZNY8uB2nJ2b6WjcQ.png)
So, what is happening above? Notice the following observations. As we said, our data belongs to Table, Item, and KVAttribute. Then, every item has a primary key. The way you compose that primary key is meaningful to how you can access the data.
If you decide that your PrimaryKey is simply a hash key, then great! You can get a single item out of it. If you decide, however, that your primary key is hashKey + SortKey, then you could also do a range query on your primary key because you will get your items by (HashKey + SomeRangeFunction (on range key)). So, you can get multiple items with your primary key query.
Note: I did not refer to secondary indexes.
Hopefully some fog was cleared! Let me know your thoughts in the comments.
Opinions expressed by DZone contributors are their own.
Comments