DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Fault-Tolerant Kafka Consumers in Spring Boot Using Retry, DLQ, and Idempotent Code Patterns
  • The $50,000 Vector Database You Don't Need
  • Ensuring Data Consistency in Distributed Systems With the Transactional Outbox Pattern
  • The Aggregate Reference Problem

Trending

  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  1. DZone
  2. Data Engineering
  3. Databases
  4. DynamoDB PrimaryKey, HashKey, SortKey (RangeKey)

DynamoDB PrimaryKey, HashKey, SortKey (RangeKey)

Let's check out DynamoDB and explore PrimaryKey, hashKey, and SortKey. Also take as look at a visual.

By 
Tomer Ben David user avatar
Tomer Ben David
·
Aug. 28, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
24.8K Views

Join the DZone community and get the full member experience.

Join For Free

Last 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:

  1. Table
  2. Item
  3. 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:

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. 

Relational database

Opinions expressed by DZone contributors are their own.

Related

  • Building Fault-Tolerant Kafka Consumers in Spring Boot Using Retry, DLQ, and Idempotent Code Patterns
  • The $50,000 Vector Database You Don't Need
  • Ensuring Data Consistency in Distributed Systems With the Transactional Outbox Pattern
  • The Aggregate Reference Problem

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook