SlideShare a Scribd company logo
1 of 53
Designing for DynamoDB
Toby Hede
Engineering Manager
serverlesspatterns.io
@tobyhede
Hello
I’m Toby
The Plan
Some facts about DynamoDB
That one time I made a relational model in DynamoDB
How to do DynamoDB better
What is DynamoDB
Amazon DynamoDB is a key-value and document database that
delivers single-digit millisecond performance at any scale. It's a fully
managed, multi-region, multi-master database with built-in security,
backup and restore, and in-memory caching for internet-scale
applications.
Core Concepts
• Tables
• Items + Attributes
• Keys
• Partition Key
• Sort Key
• Secondary Indexes
• Read + Write Capacity
Flexible
Schema
Distributes
Data
A B C
A
A2
WriteRead
A1
Read
A2
Write
A2
Indexes
Alternate Partition and/or Sort Key
Index
Partition Sort
Capacity
ReadCapacity = ceil(ItemSize/4) * DesiredReadsPerSecond
WriteCapacity = ceil(ItemSize/4) * DesiredWritesPerSecond
Just let Amazon do it
Lets start modelling
Items
Keys
Attributes
How hard can it be?
Customer
Products
Orders
No Joins
Get all the Customers
For each Customer get the Orders
For each Order get the Products
Forget everything you may
know about relational
databases
Design to optimise query patterns
Two Rules
You probably only need one table
Design to optimise query
patterns
Design the schema for access, not data structure
Data structures evolve from the problem you are solving
You need to know the questions you will need to answer.
SELECT * FROM
A single query returns multiple entities
Single round-trip
A quick note on syntax
SELECT * FROM invoices WHERE id = ‘INV0001’
dynamodb get-item –table-name invoices --key ‘{ “id": {"S": "INV0001}'
dynamodb query --table-name invoices --key-condition-expression "id = :id"
--expression-attribute-values '{ ":id": {"S": "INV0001"}}'
An example from an actual thing we did
Track Invoices so a Customer can view and download
Invoices for
An Account
By Date
Id AccountNumber Date Status
INV0001 AC000001 2019-04-01 Paid
INV0002 AC000001 2019-05-01 Paid
INV0003 AC000002 2019-05-01 Due
SELECT * FROM invoices WHERE id = ‘INV0001’
Partition
Key
SELECT * FROM orders WHERE AccountNumber =‘AC000001’ORDER BY Date
No Key
Or Index
Account Number Date InvoiceNumber Status
AC000001 2019-04-01 INV0001 Paid
AC000001 2019-05-01 INV0002 Paid
AC000002 2019-05-01 INV0003 Due
SELECT * FROM invoices WHEREAccountNumber = ‘AC000001’
Sort KeyPartition
Key
Sort order is implicit
But what if …
I need to find a specific Invoice?
Account Number Date InvoiceNumber Status
AC000001 2019-04-01 INV0001 Paid
AC000001 2019-05-01 INV0002 Paid
AC000002 2019-05-01 INV0003 Due
SELECT * FROM invoices WHERE InvoiceNumber = ‘INV0001’
Global
Secondary
Index
Lets Model an Online Store
Customer => Order
Find an Order
Find all the Orders with a {status}
Find a Customer
Find all the Customers
Find all the Delivered Orders for a Customer
Find all the Orders with {status} for a {date}
What are our queries?
Id CustomerId Date Status
order-0001 customer-0001 2019-04-01 delivered
order-0002 customer-0001 2019-05-01 pending
order-0003 customer-0002 2019-05-01 ready
SELECT * FROM orders WHERE Id = ‘order-0001’
Partition
Key
SELECT * FROM orders WHERE customerId = ‘customer-0001’
SELECT * FROM orders WHERE status = ‘delivered’
SELECT * FROM orders WHERE date = ‘2019-05’
partition sort
partition and sort keys will be overloaded
Generic
Attributes
SELECT * FROM orders WHERE partition = ‘order-01’
find an order
partition sort
order-01 order
find a customer
partition sort
order-01 order
customer-01 customer
SELECT * FROM store WHERE partition = ‘customer-01’
Use a
Transaction
Transaction WriteAPI
find all orders for a customer
partition sort
order-01 order
customer-01 customer
customer-01 order-01
SELECT * FROM store WHERE partition = ‘customer-01’
find all customers
partition sort
customer-01 customer
customer-02 customer
customer-03 customer
SELECT * FROM store WHERE sort = ‘customer’
find all orders
partition sort
order-01 order
order-02 order
order-03 order
SELECT * FROM store WHERE sort = ‘order’
SELECT * FROM store WHERE ? = ‘?’
find all orders with {status}
partition sort
order-01 order
customer-01 customer
partition sort/gsi-1-partition gsi-1-sort
Add an Index
This is about to get very confusing
Primary Key == sort/gsi-1-partition
Sort Key === gsi-1-sort
SELECT * FROM gsi-1 WHERE
sort/gsi-1-partition = ‘order’AND
gsi-1-sort = ‘delivered '
find all orders with {status}
partition sort/gsi-1-partition gsi-1-sort
order-01 order ready
order-02 order delivered
order-03 order ready
partition sort/gsi-1-partition gsi-1-sort
order-0001 order ready
order-0002 order delivered
order-0003 order ready
find all the orders with {status} for {date}
Oh no
SELECT * FROM gsi-1 WHERE
sort/gsi-1-partition = ‘order’AND
gsi-1-sort BEGINS ‘delivered/2019-05 '
find all the orders with {status} for {date}
partition sort/gsi-1-partition gsi-1-sort
order-01 order delivered/2019-05-01
order-02 order ready/2019-05-01
order-03 order pending/2019-05-05
find all the orders with {status} for {date} range
SELECT * FROM gsi-1 WHERE
sort = ‘order’AND
gsi-1-sort BETWEEN ‘delivered /2019-05-01’AND ‘delivered/2019-05-03’
partition sort/gsi-1-partition gsi-1-sort
order-01 order delivered/2019-05-01
order-02 order ready/2019-05-01
order-03 order pending/2019-05-05
find all the orders for {date}
SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘delivered/2019-05'
SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘pending/2019-05'
SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘ready/2019-05'
partition sort/gsi-1-partition gsi-1-sort
order-01 order delivered/2019-05-01
order-02 order ready/2019-05-01
order-03 order pending/2019-05-01
find all the orders for a month
partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort
order-01 order ready 2019-05-01
order-02 order delivered 2019-05-02
order-03 order ready 2019-05-03
Indexes increase
cost
(latency and $)
SELECT * FROM gsi-2 WHERE
sort/gsi-1-partition/gsi-2-partition = ‘order’AND
gsi-2-sort BEGINS ‘2019-05 '
Add another
Index
partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort
order-01 order ready 2019-05-01
order-02 order delivered 2019-05-02
order-03 order ready 2019-05-03
We can’t combine
GSIs
SELECT * FROM sort-data-index WHERE
sort/gsi-1-partition = ‘order’AND
gsi-1-sort = ‘delivered ‘
gsi-2-sort = ‘2019-05 '
find all the orders with {status} for {date}
so of course …
partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort
order-01 order ready/2019-05-01 2019-05-01
order-02 order delivered/019-05-02 2019-05-02
order-03 order ready/2019-05-03 2019-05-03
Shape the data to the
query
THIS ALL SEEMS SO HARD ...
When to DynamoDB
serverless key-value state
low-latency
multi-region resiliency
scale
Just watch this
AWS re:Invent 2019:
Amazon DynamoDB Deep Dive:
Advanced Design Patterns for DynamoDB
And read this
Takeaways from AWS re:Invent 2019’s Amazon DynamoDB Deep
Dive: Advanced Design Patterns (DAT403)
And this
dynamodbguide.com
Thank You
serverlesspatterns.io
@tobyhede

More Related Content

Similar to Designing for DynamoDB - Serverless Sydney - Feb 2020

Important tables for sap sd
Important tables for sap sd Important tables for sap sd
Important tables for sap sd Roshan Prasad
 
KPIs for e-commerce startups
KPIs for e-commerce startupsKPIs for e-commerce startups
KPIs for e-commerce startupsMartin Loetzsch
 
Quick sap co configuration Internal Order
Quick sap co configuration Internal OrderQuick sap co configuration Internal Order
Quick sap co configuration Internal OrderCapgemini
 
A2 unit 7 june2007
A2 unit 7 june2007A2 unit 7 june2007
A2 unit 7 june2007c.west
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSONChris Saxon
 
Chapter 7 Working with two result sets
Chapter 7 Working with two result setsChapter 7 Working with two result sets
Chapter 7 Working with two result setsAlokSrivastava141
 
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdf
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdfSERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdf
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdfEldeebEldeeb
 
Final Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxFinal Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxElyada Wigati Pramaresti
 
Reporting Tips
Reporting TipsReporting Tips
Reporting Tipslkurriger
 
Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010Ron Moore
 
SCM-04 Disassembly and Reuse.pptx
SCM-04 Disassembly and Reuse.pptxSCM-04 Disassembly and Reuse.pptx
SCM-04 Disassembly and Reuse.pptxgaurav804049
 
ANIN360 : 80+ AddOns for Tally.ERP9
ANIN360 : 80+ AddOns for Tally.ERP9ANIN360 : 80+ AddOns for Tally.ERP9
ANIN360 : 80+ AddOns for Tally.ERP9Anita International
 
No. 320 Dividends No. 332 Income Summary No. .pdf
No. 320 Dividends No. 332 Income Summary No. .pdfNo. 320 Dividends No. 332 Income Summary No. .pdf
No. 320 Dividends No. 332 Income Summary No. .pdfconcordpharma
 
Report development technique
Report development techniqueReport development technique
Report development techniquevenkata karthik
 
Report development technique
Report development techniqueReport development technique
Report development techniquevenkata karthik
 
Document splitting in New GL in SAP
Document splitting in New GL in SAPDocument splitting in New GL in SAP
Document splitting in New GL in SAPRajesh Shanbhag
 

Similar to Designing for DynamoDB - Serverless Sydney - Feb 2020 (20)

Sap edi idoc
Sap edi idocSap edi idoc
Sap edi idoc
 
Important tables for sap sd
Important tables for sap sd Important tables for sap sd
Important tables for sap sd
 
KPIs for e-commerce startups
KPIs for e-commerce startupsKPIs for e-commerce startups
KPIs for e-commerce startups
 
Quick sap co configuration Internal Order
Quick sap co configuration Internal OrderQuick sap co configuration Internal Order
Quick sap co configuration Internal Order
 
A2 unit 7 june2007
A2 unit 7 june2007A2 unit 7 june2007
A2 unit 7 june2007
 
Normalization
NormalizationNormalization
Normalization
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSON
 
Chapter 7 Working with two result sets
Chapter 7 Working with two result setsChapter 7 Working with two result sets
Chapter 7 Working with two result sets
 
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdf
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdfSERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdf
SERVICE ENTRY SHEET IN SAP MATERIAL MANAGEMENT.pdf
 
Final Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxFinal Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptx
 
Reporting Tips
Reporting TipsReporting Tips
Reporting Tips
 
Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010Essbase Calculations A Visual Approach KScope 2010
Essbase Calculations A Visual Approach KScope 2010
 
SCM-04 Disassembly and Reuse.pptx
SCM-04 Disassembly and Reuse.pptxSCM-04 Disassembly and Reuse.pptx
SCM-04 Disassembly and Reuse.pptx
 
ANIN360 : 80+ AddOns for Tally.ERP9
ANIN360 : 80+ AddOns for Tally.ERP9ANIN360 : 80+ AddOns for Tally.ERP9
ANIN360 : 80+ AddOns for Tally.ERP9
 
Breakdowns Powerpoint
Breakdowns PowerpointBreakdowns Powerpoint
Breakdowns Powerpoint
 
Fiar
FiarFiar
Fiar
 
No. 320 Dividends No. 332 Income Summary No. .pdf
No. 320 Dividends No. 332 Income Summary No. .pdfNo. 320 Dividends No. 332 Income Summary No. .pdf
No. 320 Dividends No. 332 Income Summary No. .pdf
 
Report development technique
Report development techniqueReport development technique
Report development technique
 
Report development technique
Report development techniqueReport development technique
Report development technique
 
Document splitting in New GL in SAP
Document splitting in New GL in SAPDocument splitting in New GL in SAP
Document splitting in New GL in SAP
 

More from Toby Hede

The Serverless Revolution: AWS Lambda and the Serverless Framework in Action
The Serverless Revolution: AWS Lambda and the Serverless Framework  in ActionThe Serverless Revolution: AWS Lambda and the Serverless Framework  in Action
The Serverless Revolution: AWS Lambda and the Serverless Framework in ActionToby Hede
 
AWS Lambda in Practice
AWS Lambda in Practice  AWS Lambda in Practice
AWS Lambda in Practice Toby Hede
 
AWS Lamdba with JavaScript
AWS Lamdba with JavaScriptAWS Lamdba with JavaScript
AWS Lamdba with JavaScriptToby Hede
 
An Introduction to Rust Macros
An Introduction to Rust MacrosAn Introduction to Rust Macros
An Introduction to Rust MacrosToby Hede
 
Rails Rescue - Managing Technical Debt in Legacy Applications
Rails Rescue - Managing Technical Debt in Legacy ApplicationsRails Rescue - Managing Technical Debt in Legacy Applications
Rails Rescue - Managing Technical Debt in Legacy ApplicationsToby Hede
 
Introduction to Facebook Development
Introduction to Facebook DevelopmentIntroduction to Facebook Development
Introduction to Facebook DevelopmentToby Hede
 
Amazon Web Services
Amazon Web ServicesAmazon Web Services
Amazon Web ServicesToby Hede
 
On XML and the Future of the Web
On XML and the Future of the WebOn XML and the Future of the Web
On XML and the Future of the WebToby Hede
 

More from Toby Hede (9)

The Serverless Revolution: AWS Lambda and the Serverless Framework in Action
The Serverless Revolution: AWS Lambda and the Serverless Framework  in ActionThe Serverless Revolution: AWS Lambda and the Serverless Framework  in Action
The Serverless Revolution: AWS Lambda and the Serverless Framework in Action
 
AWS Lambda in Practice
AWS Lambda in Practice  AWS Lambda in Practice
AWS Lambda in Practice
 
AWS Lamdba with JavaScript
AWS Lamdba with JavaScriptAWS Lamdba with JavaScript
AWS Lamdba with JavaScript
 
An Introduction to Rust Macros
An Introduction to Rust MacrosAn Introduction to Rust Macros
An Introduction to Rust Macros
 
Rails Rescue - Managing Technical Debt in Legacy Applications
Rails Rescue - Managing Technical Debt in Legacy ApplicationsRails Rescue - Managing Technical Debt in Legacy Applications
Rails Rescue - Managing Technical Debt in Legacy Applications
 
MongoDb
MongoDbMongoDb
MongoDb
 
Introduction to Facebook Development
Introduction to Facebook DevelopmentIntroduction to Facebook Development
Introduction to Facebook Development
 
Amazon Web Services
Amazon Web ServicesAmazon Web Services
Amazon Web Services
 
On XML and the Future of the Web
On XML and the Future of the WebOn XML and the Future of the Web
On XML and the Future of the Web
 

Recently uploaded

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

Designing for DynamoDB - Serverless Sydney - Feb 2020

  • 1. Designing for DynamoDB Toby Hede Engineering Manager serverlesspatterns.io @tobyhede
  • 3. The Plan Some facts about DynamoDB That one time I made a relational model in DynamoDB How to do DynamoDB better
  • 4. What is DynamoDB Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It's a fully managed, multi-region, multi-master database with built-in security, backup and restore, and in-memory caching for internet-scale applications.
  • 5. Core Concepts • Tables • Items + Attributes • Keys • Partition Key • Sort Key • Secondary Indexes • Read + Write Capacity
  • 8. A
  • 13. Capacity ReadCapacity = ceil(ItemSize/4) * DesiredReadsPerSecond WriteCapacity = ceil(ItemSize/4) * DesiredWritesPerSecond Just let Amazon do it
  • 15. Customer Products Orders No Joins Get all the Customers For each Customer get the Orders For each Order get the Products
  • 16.
  • 17. Forget everything you may know about relational databases
  • 18. Design to optimise query patterns Two Rules You probably only need one table
  • 19. Design to optimise query patterns Design the schema for access, not data structure Data structures evolve from the problem you are solving You need to know the questions you will need to answer.
  • 20. SELECT * FROM A single query returns multiple entities Single round-trip
  • 21. A quick note on syntax SELECT * FROM invoices WHERE id = ‘INV0001’ dynamodb get-item –table-name invoices --key ‘{ “id": {"S": "INV0001}' dynamodb query --table-name invoices --key-condition-expression "id = :id" --expression-attribute-values '{ ":id": {"S": "INV0001"}}'
  • 22. An example from an actual thing we did Track Invoices so a Customer can view and download
  • 24. Id AccountNumber Date Status INV0001 AC000001 2019-04-01 Paid INV0002 AC000001 2019-05-01 Paid INV0003 AC000002 2019-05-01 Due SELECT * FROM invoices WHERE id = ‘INV0001’ Partition Key SELECT * FROM orders WHERE AccountNumber =‘AC000001’ORDER BY Date No Key Or Index
  • 25. Account Number Date InvoiceNumber Status AC000001 2019-04-01 INV0001 Paid AC000001 2019-05-01 INV0002 Paid AC000002 2019-05-01 INV0003 Due SELECT * FROM invoices WHEREAccountNumber = ‘AC000001’ Sort KeyPartition Key Sort order is implicit
  • 26. But what if … I need to find a specific Invoice?
  • 27. Account Number Date InvoiceNumber Status AC000001 2019-04-01 INV0001 Paid AC000001 2019-05-01 INV0002 Paid AC000002 2019-05-01 INV0003 Due SELECT * FROM invoices WHERE InvoiceNumber = ‘INV0001’ Global Secondary Index
  • 28. Lets Model an Online Store Customer => Order
  • 29. Find an Order Find all the Orders with a {status} Find a Customer Find all the Customers Find all the Delivered Orders for a Customer Find all the Orders with {status} for a {date} What are our queries?
  • 30. Id CustomerId Date Status order-0001 customer-0001 2019-04-01 delivered order-0002 customer-0001 2019-05-01 pending order-0003 customer-0002 2019-05-01 ready SELECT * FROM orders WHERE Id = ‘order-0001’ Partition Key SELECT * FROM orders WHERE customerId = ‘customer-0001’ SELECT * FROM orders WHERE status = ‘delivered’ SELECT * FROM orders WHERE date = ‘2019-05’
  • 31. partition sort partition and sort keys will be overloaded Generic Attributes
  • 32. SELECT * FROM orders WHERE partition = ‘order-01’ find an order partition sort order-01 order
  • 33. find a customer partition sort order-01 order customer-01 customer SELECT * FROM store WHERE partition = ‘customer-01’ Use a Transaction Transaction WriteAPI
  • 34. find all orders for a customer partition sort order-01 order customer-01 customer customer-01 order-01 SELECT * FROM store WHERE partition = ‘customer-01’
  • 35. find all customers partition sort customer-01 customer customer-02 customer customer-03 customer SELECT * FROM store WHERE sort = ‘customer’
  • 36. find all orders partition sort order-01 order order-02 order order-03 order SELECT * FROM store WHERE sort = ‘order’
  • 37. SELECT * FROM store WHERE ? = ‘?’ find all orders with {status} partition sort order-01 order customer-01 customer
  • 38. partition sort/gsi-1-partition gsi-1-sort Add an Index This is about to get very confusing Primary Key == sort/gsi-1-partition Sort Key === gsi-1-sort
  • 39. SELECT * FROM gsi-1 WHERE sort/gsi-1-partition = ‘order’AND gsi-1-sort = ‘delivered ' find all orders with {status} partition sort/gsi-1-partition gsi-1-sort order-01 order ready order-02 order delivered order-03 order ready
  • 40. partition sort/gsi-1-partition gsi-1-sort order-0001 order ready order-0002 order delivered order-0003 order ready find all the orders with {status} for {date} Oh no
  • 41. SELECT * FROM gsi-1 WHERE sort/gsi-1-partition = ‘order’AND gsi-1-sort BEGINS ‘delivered/2019-05 ' find all the orders with {status} for {date} partition sort/gsi-1-partition gsi-1-sort order-01 order delivered/2019-05-01 order-02 order ready/2019-05-01 order-03 order pending/2019-05-05
  • 42. find all the orders with {status} for {date} range SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BETWEEN ‘delivered /2019-05-01’AND ‘delivered/2019-05-03’ partition sort/gsi-1-partition gsi-1-sort order-01 order delivered/2019-05-01 order-02 order ready/2019-05-01 order-03 order pending/2019-05-05
  • 43. find all the orders for {date} SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘delivered/2019-05' SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘pending/2019-05' SELECT * FROM gsi-1 WHERE sort = ‘order’AND gsi-1-sort BEGINS ‘ready/2019-05' partition sort/gsi-1-partition gsi-1-sort order-01 order delivered/2019-05-01 order-02 order ready/2019-05-01 order-03 order pending/2019-05-01
  • 44. find all the orders for a month partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort order-01 order ready 2019-05-01 order-02 order delivered 2019-05-02 order-03 order ready 2019-05-03 Indexes increase cost (latency and $) SELECT * FROM gsi-2 WHERE sort/gsi-1-partition/gsi-2-partition = ‘order’AND gsi-2-sort BEGINS ‘2019-05 ' Add another Index
  • 45. partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort order-01 order ready 2019-05-01 order-02 order delivered 2019-05-02 order-03 order ready 2019-05-03 We can’t combine GSIs SELECT * FROM sort-data-index WHERE sort/gsi-1-partition = ‘order’AND gsi-1-sort = ‘delivered ‘ gsi-2-sort = ‘2019-05 ' find all the orders with {status} for {date}
  • 46. so of course … partition sort/gsi-1-partition/gsi-2-partition gsi-1-sort gsi-2-sort order-01 order ready/2019-05-01 2019-05-01 order-02 order delivered/019-05-02 2019-05-02 order-03 order ready/2019-05-03 2019-05-03 Shape the data to the query
  • 47. THIS ALL SEEMS SO HARD ...
  • 48.
  • 49. When to DynamoDB serverless key-value state low-latency multi-region resiliency scale
  • 50. Just watch this AWS re:Invent 2019: Amazon DynamoDB Deep Dive: Advanced Design Patterns for DynamoDB
  • 51. And read this Takeaways from AWS re:Invent 2019’s Amazon DynamoDB Deep Dive: Advanced Design Patterns (DAT403)

Editor's Notes

  1. DynamoDB is simultaneously the simplest and most complicated database money can buy. A simple key/value table with incredible reliability and essentially boundless capacity can be setup in seconds, but modelling complex access patterns requires careful and considered upfront design planning and consideration. If you have experience with relational systems, DynamoDB requires us to adopt new ways of thinking about how we design and structure our data. This is the story of some of the things I have learned by doing DynamoDB wrong.
  2. Eventually consistent reads
  3. Strongly consistent reads
  4. In RDBMS, you design for flexibility without worrying about implementation details or performance. Query optimization generally doesn't affect schema design, but normalization is very important. In DynamoDB, you design your schema specifically to make the most common and important queries as fast and as inexpensive as possible. Your data structures are tailored to the specific requirements of your business use cases.
  5. 23 access patterns using only THREE GSIs.