Improve DynamoDB Performance with DAX

Be A Better Dev
25 May 202020:24

Summary

TLDRThis video introduces DynamoDB Accelerator (DAX), a managed caching layer for DynamoDB that offers microsecond-level performance, significantly faster than standard DynamoDB operations. The script covers DAX's use cases, benefits, and limitations, including eventual consistency issues. It also provides an in-depth look at DAX's architecture, explaining how it handles read and write operations, and concludes with best practices for monitoring, scaling, and avoiding common pitfalls when using DAX in applications.

Takeaways

  • πŸš€ DynamoDB Accelerator (DAX) is a fully managed caching layer for DynamoDB that improves performance by providing microsecond response times.
  • πŸ’‘ DAX is suitable for applications with consistent or burst traffic on the same set of keys, especially those with limited write patterns.
  • πŸ“Š Caching optimizes performance by storing frequently accessed data in memory, reducing the need for repetitive database queries.
  • πŸ”„ DAX supports various operations including get, query, scan, update, delete, and put, as well as LSI and GSI operations.
  • πŸ“ˆ DAX clusters consist of EC2 nodes that can be scaled horizontally (adding more nodes) and vertically (adding more resources to nodes).
  • πŸ–₯️ DAX clusters typically have one master node for writes and multiple read replicas to serve read requests.
  • ⚠️ Using DAX introduces eventual consistency, meaning there could be a delay in data updates between the cache and the database, potentially serving stale data.
  • πŸ” Avoid caching queries if you need consistent data, as the cached query results may not reflect the latest updates.
  • πŸ”§ Monitor and scale your DAX nodes according to your application's workload to ensure optimal performance and cost-efficiency.
  • πŸ”— Sharing DAX clusters across multiple tables or applications can lead to cascading failures, so it's important to be cautious with this setup.

Q & A

  • What is DynamoDB Accelerator (DAX)?

    -DynamoDB Accelerator, also known as DAX, is a fully managed, highly available caching layer for DynamoDB that delivers microsecond-level performance for read and write operations.

  • What is the basic function of a cache?

    -A cache is a data storage layer that contains a subset of data from the main database. It serves to optimize performance by allowing for quicker data retrieval from in-memory storage rather than from the database itself.

  • What is the difference between local and remote caches?

    -A local cache is stored in the memory of the machine it serves, while a remote cache involves multiple nodes working together to act as a distributed cache, similar to how DAX operates.

  • How does DAX improve performance compared to standard DynamoDB?

    -DAX improves performance by providing microsecond-level response times for read operations, which is significantly faster than the typical milliseconds range for standard DynamoDB get item operations.

  • What are the scalability options for DAX?

    -DAX is both horizontally and vertically scalable. Horizontal scalability is achieved by adding more nodes to the cluster, while vertical scalability involves increasing the CPU, memory, or performance of the existing nodes.

  • Which operations does DAX support for data retrieval and modification?

    -DAX supports get, query, and scan operations for data retrieval, and update, delete, and put operations for data modification. It also supports caching for LSI (Local Secondary Index), GSI (Global Secondary Index), and any range key operations during queries.

  • What are the use case scenarios where DAX is suitable?

    -DAX is suitable for applications with consistent or burst-like traffic on the same set of keys, limited write patterns, and those requiring microsecond-level response times. It is also suitable for read-intensive applications that can tolerate eventual consistency.

  • What is the concept of eventual consistency in the context of DAX?

    -Eventual consistency means that there may be scenarios where the data in the DAX cache is inconsistent with the data in the DynamoDB table, leading to the possibility of serving stale content.

  • What is the recommended approach when using DAX for applications that require high data accuracy?

    -For applications requiring high data accuracy, it is recommended to use the DynamoDB table as the source of truth and avoid relying solely on the DAX cache, especially for critical operations.

  • What are some best practices when using DAX?

    -Best practices include understanding the implications of caching queries, monitoring and scaling nodes based on workload, being aware of single points of failure when sharing a cluster across multiple tables, and treating the DynamoDB table as the source of truth when in doubt.

  • What is the role of the 'item cache' in DAX?

    -The item cache in DAX is responsible for storing mappings of keys to values. It is updated transactionally whenever there are modifications to the items in the DynamoDB table, ensuring that subsequent lookups can be served from the cache.

  • How does the 'query cache' in DAX work?

    -The query cache in DAX works by caching a combination of criteria and a list of results. When a query request comes in, DAX first checks if the criteria have been previously queried. If not, it retrieves the data from DynamoDB and stores it in the cache for faster access in subsequent queries.

  • What is the potential issue with using the query cache in DAX?

    -The potential issue with using the query cache is that it may serve stale data. If any of the items in the cached results change in the DynamoDB table while the cache entry is still valid, the next query using the same criteria may receive outdated results from the cache.

Outlines

00:00

πŸš€ Introduction to DynamoDB Accelerator (DAX)

The video script introduces the concept of DynamoDB Accelerator, known as DAX, which is a highly efficient caching layer for DynamoDB. The speaker expresses excitement about discussing DAX due to their familiarity with it. The video aims to provide an overview of DAX, discuss its suitable use cases, explain its operational mechanism, delve into its inner workings, and conclude with best practice advice. Before diving into DAX, the script mentions a tool called 'Dino bass' designed to simplify working with DynamoDB, offering a free trial and a link in the description for further exploration. The foundation of the discussion is set by explaining the basic concept of caching, its benefits, and how it functions as an in-memory data storage layer to improve performance by reducing database retrieval times.

05:03

πŸ›  Understanding DAX and Its Scalability

This paragraph delves into what DAX isβ€”a fully managed, highly available caching layer for DynamoDB that significantly boosts performance to microsecond levels. It contrasts the typical performance of DynamoDB with the enhanced performance provided by DAX. The script explains that DAX is scalable both horizontally and vertically, with nodes consisting of EC2 machines that work together to serve cached content. It clarifies that one node acts as the master for write operations, while others serve as read replicas. DAX supports common DynamoDB operations like get, query, scan, update, delete, and put, and also caches Local Secondary Index (LSI) and Global Secondary Index (GSI) operations. The transition from standard DynamoDB to DAX is straightforward, typically requiring only a change in the SDK and endpoint. The cost of using DAX is based on a per-node basis, with no additional costs incurred.

10:05

πŸ€” Assessing Suitability for DAX Use

The script discusses the suitability of DAX for different applications, noting that while DAX offers performance benefits, it also has limitations that may make it unsuitable for certain use cases. Ideal candidates for DAX are applications with consistent or bursty traffic on a set of keys that do not change frequently, requiring microsecond-level response times and being able to tolerate eventual consistency. Eventual consistency is explained as a scenario where data in the cache may become inconsistent with the DynamoDB table, leading to potential issues. The paragraph advises against using DAX for write-intensive applications, as caches are more beneficial for read-intensive scenarios with high read and limited write operations.

15:06

πŸ” Deep Dive into DAX's Internal Mechanisms

The script provides an in-depth look at how DAX operates, starting with a high-level overview of setting up a DAX cluster in front of a DynamoDB table to reduce read capacity costs and improve performance. It explains the process of read and write operations within DAX, including cache misses and the propagation of data from the master node to read replicas. The paragraph then zooms in on the low-level implementation of DAX, detailing the two internal caches: the item cache, which stores key-value mappings and is updated transactionally with table modifications, and the query cache, which stores query results based on criteria. The script highlights the potential issues with query caching, such as serving stale data due to delays in updating the cache in response to table changes.

20:07

πŸ“˜ Best Practices for Using DAX

The final paragraph offers best practice advice for using DAX. It warns against caching queries due to the risk of serving inconsistent data and emphasizes the importance of understanding the implications of using caches for mission-critical information. It also stresses the need for monitoring and scaling DAX nodes based on workload, as well as being cautious about sharing a DAX cluster across multiple tables to prevent potential cascading failures. The advice concludes with a reminder to always consider the DynamoDB table as the source of truth in cases of doubt, to ensure fault tolerance and the best possible user experience.

πŸ“Ί Conclusion and Future Content Tease

The video script concludes with a prompt for viewers to like and subscribe to stay updated with upcoming content, including a teaser for a future video on DynamoDB schema design. The speaker thanks the audience and signs off, indicating the end of the current video session.

Mindmap

Keywords

πŸ’‘DynamoDB Accelerator (DAX)

DynamoDB Accelerator, known as DAX, is a fully managed, highly available caching layer for DynamoDB that delivers microsecond-level performance. It is a core subject of the video, which discusses its benefits, use cases, and how it works. The script mentions DAX's ability to significantly improve the performance of get item operations, which typically take several milliseconds in DynamoDB, to a much faster microsecond level.

πŸ’‘Caching

Caching is a fundamental concept in the script, defined as an optimization technique that stores a subset of data in memory for quicker retrieval. The video explains that caching reduces the need to access the database repeatedly for the same data, thus enhancing performance. The script uses the example of a country database to illustrate how caching can save a trip to the database for faster data retrieval.

πŸ’‘In-memory Cache

An in-memory cache is a data storage layer that resides in the RAM of a local machine, allowing for rapid data access. The video script discusses how in-memory caching, such as with DAX, can provide faster performance for data retrieval operations compared to accessing data directly from a database. It is a key component of how DAX achieves its high-speed performance.

πŸ’‘Dino Bass

Dino Bass is an application mentioned in the script that is designed to ease the process of working with DynamoDB. It includes features for performing tedious tasks and visualizing data intuitively. The script suggests that Dino Bass can be a helpful tool for users working with DynamoDB, offering a seven-day free trial for users to evaluate its usefulness.

πŸ’‘Microsecond-level Performance

The term 'microsecond-level performance' refers to the extremely fast response times provided by DAX, which is a significant improvement over the typical millisecond response times of DynamoDB. The video emphasizes this performance boost as a key advantage of using DAX for read-intensive applications.

πŸ’‘Horizontal and Vertical Scalability

Horizontal and vertical scalability are concepts related to the ability of a system to handle increased load by adding more resources. In the context of the video, DAX is described as scalable both horizontally, by adding more nodes to a cluster, and vertically, by enhancing the capabilities of existing nodes with more CPU, memory, or faster-performing memory.

πŸ’‘Eventual Consistency

Eventual consistency is a concept where data may not immediately reflect the latest updates across all copies. The script warns about the implications of eventual consistency when using DAX, as it can lead to scenarios where the cache serves stale data. This is a critical consideration when deciding if DAX is suitable for an application that requires up-to-date data.

πŸ’‘Read Replicas

Read replicas are copies of the data that can handle read requests in a database system. The video script explains that in a DAX cluster, one node acts as the master for writes, while other nodes serve as read replicas, providing the ability to distribute read requests and enhance performance.

πŸ’‘Item Cache

The item cache is one of the two internal caches maintained by DAX, as detailed in the script. It stores mappings of keys to values, ensuring that get item operations are served quickly from the cache. The script uses the item cache to illustrate how DAX maintains consistency between the cache and the DynamoDB table during updates.

πŸ’‘Query Cache

The query cache is the second type of internal cache in DAX, which is responsible for caching results of query operations. However, the script cautions against its use due to the potential for serving stale data. The video explains the mechanics of the query cache and why it can be problematic, especially when updates occur on the DynamoDB table that are not immediately reflected in the cache.

Highlights

Introduction to DynamoDB Accelerator (DAX) and its ability to significantly improve performance for DynamoDB operations.

DAX, also known as DynamoDB Accelerator, is a fully managed, highly available caching layer for DynamoDB.

DAX delivers microsecond-level performance, a stark contrast to the typical millisecond response times of DynamoDB.

DAX's scalability allows for both horizontal and vertical scaling to meet varying performance demands.

DAX's node architecture consists of a master node for writes and multiple read replica nodes.

DAX supports common DynamoDB operations like get, query, scan, as well as update, delete, and put.

The introduction of Dino Bass, a tool designed to simplify working with DynamoDB and visualize data.

Caching basics explained, including the concept of a cache as a data storage layer containing a subset of data.

The importance of caching for optimizing database operations and reducing round-trip costs.

DAX's use case suitability for applications with consistent or burst-like traffic on the same set of keys.

Limitations of DAX, including eventual consistency and scenarios where data in the cache may be stale.

Best practices for using DAX, emphasizing the importance of understanding eventual consistency implications.

The recommendation to monitor and scale DAX nodes based on workload to optimize performance and cost.

Potential issues with sharing a DAX cluster across multiple tables and the risk of cascading failures.

The advice to use DynamoDB as the source of truth and not rely solely on DAX for critical data operations.

An overview of how DAX works internally, including the item cache and query cache mechanisms.

The problem of serving stale data from the query cache due to delays in updating the item cache.

The conclusion emphasizing the need for fault tolerance and delivering the best experience for customers when using DAX.

Transcripts

play00:00

hello everyone this is the video where I

play00:03

tell you about dynamo DB accelerator

play00:05

which is also known as Dax and I got to

play00:08

tell you I'm really super excited about

play00:10

this video because I've used Dax quite a

play00:12

bit in the past so I'm quite familiar

play00:14

with some of the low-level details that

play00:16

I'm very excited to share with you today

play00:18

so this video is gonna consist of an

play00:20

overview of what Dax is a small talk

play00:23

about what use cases Dax is suitable for

play00:26

followed by an overview of how it works

play00:28

than a deep dive on how it works and

play00:30

finally some talk about best practices

play00:33

but before I get into that I just

play00:35

briefly want to mention a neat tool I

play00:36

came across when working with dynamo DB

play00:39

and the tool it's called Dino bass Dino

play00:42

bass is an application that's devoted to

play00:44

making your life easier when working

play00:46

with dynamo DB how it includes features

play00:48

that allow you to perform some normally

play00:50

tedious dynamo tasks as well as

play00:52

visualize your data in a pretty

play00:54

intuitive way and Dino bass has a seven

play00:56

day free trial so I recommend you try it

play00:57

out to see if it's right for you you can

play00:59

find a link in the description section

play01:01

below so now getting into the meat of

play01:03

the video I really want to just jump

play01:05

right into Dax but before we do that I

play01:07

think we need to set the stage and

play01:08

quickly talk about what caching is at

play01:11

its most basic level so first let's talk

play01:14

about what is a cache why is it useful

play01:16

so a cache is in its most basic form

play01:19

it's just a data storage layer that

play01:21

contains a subset of your data so

play01:24

imagine you have a country database

play01:25

where you have country IDs and then some

play01:27

details about those country objects

play01:30

typically when we try to retrieve data

play01:32

from a database there's a round-trip

play01:34

cost of making that connection to the

play01:36

database making the query and finally

play01:38

getting those results back caching is an

play01:41

optimization technique that allows for

play01:44

duplication of this data and that

play01:46

duplicated data sits in memory typically

play01:49

on a local machine and instead of

play01:52

reading that data from the database

play01:53

itself your application will have

play01:55

business logic to first attempt to read

play01:58

it from your in-memory cache before you

play02:00

go and get it from the database so it

play02:02

essentially serves as a mechanism to

play02:04

save a trip to the database and deliver

play02:07

some quicker performance for your get

play02:08

operations so like I mentioned a cache

play02:11

entry is usually stored in memory

play02:13

to quickly retrieve and serve content

play02:16

and caches can be local or remote you

play02:19

can have just a local cache that's

play02:20

serving memory directly our that machine

play02:23

you can have a remote cache which is

play02:25

something more similar to how Docs works

play02:27

where you have multiple nodes that are

play02:29

working as a unit to act as a cache and

play02:32

we're gonna get more into those details

play02:33

later so Before we jump into Dax

play02:36

specifically let's just go over again

play02:38

how this works

play02:39

so say for instance you have an ojs app

play02:41

and that's hosted on an ec2 machine in

play02:44

AWS and you have requests that are

play02:47

coming in for a country ID for instance

play02:50

what your application would do is first

play02:52

check your cache for the country ID

play02:54

that's being queried on if the item is

play02:57

in the cache it would serve it right

play02:58

back right away if the item was not in

play03:00

the cache then you would go to the

play03:02

database retrieve that data there and

play03:04

before you returned the row back to the

play03:07

caller

play03:08

you put that row in the cache so that

play03:10

next time when a retrieval operation

play03:13

comes in that row will be in the cache

play03:15

so you don't have to make another trip

play03:16

to the database they got the record

play03:18

again finally to return that value back

play03:21

to your clients so this was a brief

play03:23

overview about how caches work but the

play03:26

next logical question is how does Dax

play03:28

fit into this whole picture

play03:29

we know that Dax is for optimization we

play03:32

know DynamoDB is a database

play03:34

what does Dax do and how does it make my

play03:36

life easier I'm sure you're all asking

play03:37

this question now so what is Dax

play03:40

so Dax is a fully managed highly

play03:43

available caching layer for dynamodb it

play03:47

delivers microsecond level performance

play03:49

and if you've worked with dynamo in the

play03:51

past you know that a typical level of

play03:53

performance that you get on a getitem

play03:55

operation is something in the realm of

play03:58

five to eight milliseconds I would say

play04:00

on the lower end and if you're doing

play04:02

something a bit more bulky with query

play04:04

operations that are retrieving multiple

play04:06

rows then maybe like a couple hundred

play04:08

milliseconds maybe 50 milliseconds if

play04:10

you're lucky but that's the kind of

play04:12

performance that you're going to get out

play04:14

of normal and DynamoDB Dax takes that to

play04:17

the next level and delivers microsecond

play04:19

level performance for you so you

play04:21

definitely get a boost in the

play04:23

performance department at the cost of

play04:25

some limitations of

play04:27

Dax and that's what I want to kind of

play04:29

talk about in the next few slides here

play04:31

so Dax is horizontally and vertically

play04:34

scalable and like I mentioned Dax

play04:36

consists of nodes and each node is an

play04:39

ec2 machine and a Dax cluster works

play04:42

together with these nodes that are in

play04:43

the cluster to serve some highly

play04:46

available content that's duplicated

play04:48

across all of your notes so Dax is

play04:50

horizontally scalable in the sense that

play04:52

you can easily add more nodes to your

play04:55

cluster and it's vertically scalable in

play04:57

the sense that you can throw more CPU or

play04:59

more memory or faster performing memory

play05:02

also at your nodes to improve

play05:04

performance in the vertical dimension

play05:06

just keep in mind one node will always

play05:09

serve as your master which is the ledger

play05:11

for writes and the other nodes that are

play05:14

in your cluster will serve as your read

play05:15

replicas and we'll get more into that in

play05:17

the next few slides as well so Dax by

play05:20

default supports get query and scan

play05:23

operations on the data retrieval side

play05:25

and update delete and put operations on

play05:28

the data modification side it also

play05:30

supports caching for LSI GSI operations

play05:34

and any range key operations that you're

play05:37

doing when performing queries it allows

play05:40

for easy migration from just your

play05:42

standard vanilla DynamoDB to Dax

play05:45

typically you just have to switch out

play05:47

your SDK that you're using and then you

play05:49

just switch the endpoint to point to the

play05:51

Dax cluster as opposed to the Dynamo

play05:53

table and finally like I was mentioning

play05:56

before your cache is hosted on multiple

play05:58

nodes in a cluster which are actually

play06:00

just memory optimized ec2 instances and

play06:03

the cost model is that you just pay per

play06:05

node there's no additional costs that

play06:07

are incurred as a result of using Dax

play06:09

you just pay for the hardware so now I

play06:11

want to move on to what applications is

play06:14

Dax suitable for because like I

play06:16

mentioned it gives you all these

play06:17

performance boost it's all does all

play06:19

these great things for you makes things

play06:20

really fast so you can do all this

play06:23

optimization in your application but

play06:25

unfortunately all this performance boost

play06:27

does come at a cost and the limitations

play06:30

that arise out of using Dax sometimes

play06:32

make it such that certain applications

play06:34

are suitable candidates so let's talk a

play06:36

little bit about that so when should you

play06:39

use it when should you not use it

play06:40

so you should use it if you have an

play06:43

access pattern such that you have

play06:44

consistent or burst like traffic on the

play06:47

same set of keys ideally those keys

play06:50

don't change very often so you have

play06:52

limited write patterns on those keys

play06:55

also obviously I've kind of mentioned a

play06:58

lot about performance here so if you do

play07:00

require that microsecond level response

play07:02

times then you should definitely look

play07:04

into using Dax I'm going to get more

play07:06

into the limitations next thirdly if you

play07:09

if your use case can tolerate eventual

play07:11

consistency so what eventual consistency

play07:13

means in pretty basic terms is that

play07:17

you're going to have scenarios or cases

play07:20

when you have data that exists for a

play07:23

specific key on your DynamoDB table that

play07:26

is inconsistent with what exists on your

play07:29

cache so say for instance you have a

play07:31

dynamodb table where your ID is ABC and

play07:34

the value is 1 in your dynamic table

play07:37

that's the rollout as it as it exists

play07:39

ABC with value 1 it's possible due to

play07:43

the way that Docs works that if that row

play07:46

gets modified the modification may not

play07:48

propagate into the Dax cluster so that

play07:51

when a query takes place on the Dax

play07:53

cluster it will potentially be serving

play07:55

up stale content so this is one of the

play07:58

limitations that you have to factor in

play07:59

when you're deciding whether or not you

play08:01

want to use it because this can cause

play08:03

some very very difficult to debug

play08:05

scenarios and if you're not aware of it

play08:07

from the get-go it'll cause some very

play08:09

complex bugs in your application and

play08:13

finally you should use it when your

play08:15

application is read intensive and not

play08:18

write intensive like I kind of alluded

play08:20

to before you have one master with

play08:23

multiple read replicas if you have a

play08:25

very read intensive application your

play08:27

master node will very quickly become

play08:29

overburdened with traffic and write

play08:32

intensive applications just don't really

play08:34

make sense for caches caches are

play08:36

typically used for applications with a

play08:38

lot of reads and accesses on your data

play08:40

with not a lot of writes so so far we've

play08:43

talked about what caches are what Docs

play08:45

is a little bit about how it works and

play08:48

kind of if your use case is suitable for

play08:50

it now I kind of want to jump into a

play08:52

little bit of an overview to start about

play08:54

how

play08:54

DOX works under the hood followed by a

play08:56

more comprehensive deep dive and that'll

play08:58

kind of really show you some of the

play09:00

low-level details about why eventual

play09:02

consistency can be problematic for

play09:04

applications so let's move on now into

play09:08

how it works from an overview so say you

play09:11

have a transactions table and this

play09:12

dynamo table stores credit card

play09:14

transactions in this case and say you

play09:16

decide to set up a Dax cluster that's

play09:19

going to sit in front of your dynamo

play09:20

table because for whatever reason you

play09:22

have a very high reed pattern on this

play09:25

table and your read capacity usage is

play09:27

starting to get expensive right you

play09:29

don't want to be paying 10000 read

play09:31

capacity units on your transactions

play09:33

table you're looking for something

play09:35

that's going to be cheaper and also give

play09:37

you a performance boost so you say okay

play09:39

let's use a Dax cluster so you set up

play09:41

your docks cluster in the console and

play09:43

say we specify three nodes in our Dax

play09:45

cluster by defaults Dax will assign one

play09:49

to be the master and the other two nodes

play09:51

to be read replicas which are going to

play09:53

contain copies of the masters cache so

play09:57

behind the scenes any rights that occur

play10:00

will automatically first go to the

play10:02

master and then propagate their data

play10:04

into the read replicas so you have this

play10:06

very natural sharing of information

play10:08

that's going on between the nodes in

play10:11

your cluster so Dax will also set up a

play10:14

load balancer with an endpoint so that

play10:17

whenever a request comes in it's kind of

play10:19

a round-robin approach into determining

play10:21

which node is going to actually serve

play10:23

the request could be your master it

play10:25

could be one of the other two reed

play10:26

replicas so say for instance if a

play10:29

request comes in to your cache and say

play10:32

it's a read operation so you're trying

play10:34

to find maybe

play10:35

transaction ID t1 let's just write that

play10:38

down you're looking for t1 in your

play10:41

transactions table how is that going to

play10:43

work so you query t1 against the Dax

play10:46

endpoint you're gonna check the cache to

play10:48

see if t1 exists in the cache cluster

play10:51

and the case that it does you just

play10:53

return it back to the client right away

play10:54

in the case that it doesn't you need to

play10:57

go and retrieve that data from your

play10:59

transactions table so it'll go and query

play11:02

your dynamo DB database retrieve that

play11:05

data insert it into your docks

play11:07

so that it's available for our next

play11:09

query that comes in before finally

play11:11

returning it back to your clients so

play11:14

that's how it works from the read

play11:15

perspective we had two operations we had

play11:17

what we call a cache miss when we found

play11:19

out that the entry didn't exist in the

play11:21

cache and then we had to go and retrieve

play11:23

it from the source of truth before we

play11:25

populated it so that was the read flow

play11:27

so how does it work for the right flow

play11:29

so for the right flow if there's a right

play11:32

to your DynamoDB table that write will

play11:35

be transactional in nature your first

play11:37

write will occur on the Dynamo table

play11:40

itself the second step to be performed

play11:42

is to write that same row into your

play11:45

cache so you get that data primed in

play11:47

your cache because in all likelihood if

play11:49

you're writing an item to your table

play11:51

it's very likely that you're gonna read

play11:53

that item from that table shortly

play11:55

thereafter so that's the right flow of

play11:58

how data gets into the cache when data

play12:00

is modified in the source of truth so

play12:04

that was a quick overview from a

play12:06

high-level perspective

play12:07

now let's talk about the low-level

play12:09

implementation of Dax and how it works

play12:11

from the most basic levels so how it

play12:14

works from the advanced perspective so

play12:16

again we have our transactions table and

play12:19

say we decide to set up our cluster

play12:21

again and this is kind of a zoomed in

play12:23

view of a Dax cluster say we're just

play12:26

looking at a particular node say it's

play12:28

our master node in this case just to

play12:30

make our lives a little bit simpler so

play12:32

behind the scene Dax will maintain two

play12:35

sets of internal caches that are shared

play12:38

across your Dax cluster so the first one

play12:40

is called an item cache and the item

play12:43

caches responsibility is to store

play12:45

mappings of keys to values so your keys

play12:49

being the ID of the item and the value

play12:52

being the item itself

play12:53

so if you add t1 that's the key in this

play12:56

example and the contents of that row say

play12:59

it's a purchase transaction and it's a

play13:02

$50 value and it took place on a certain

play13:04

date all that metadata is the value so

play13:08

we have a mapping between the key which

play13:10

was t1 and the value or the object which

play13:13

is a transaction in this case so the

play13:15

intention of this cache is to maintain

play13:17

this mapping whenever updates occur on

play13:20

your

play13:20

table so say for instance if a request

play13:23

comes in it's a get item API and it's

play13:26

pointed to your docs cluster say our

play13:28

we're using c1 here that's our partition

play13:31

key and our range key is date we will

play13:33

first like I mentioned before attempt to

play13:35

see if it's in the item cache if it's in

play13:37

the cache we return right away if it's

play13:39

not we go and look up that item in the

play13:42

dynamo table before we finally store it

play13:44

in our item cache with the c1 and date

play13:48

hashed as the key and the actual value

play13:51

stored alongside that hash key so that's

play13:55

the item caches responsibility so

play13:57

anytime any get item operations occur

play13:59

they're gonna always look at the item

play14:01

cache and any time any modifications

play14:03

occurred to the items on your table

play14:05

they're gonna be transactionally updated

play14:07

into your item cache so the next time a

play14:09

lookup occurs that value will be sitting

play14:12

in your item cache so the second cache

play14:14

that occurs is what's called the query

play14:16

cache and the query cache is what drives

play14:18

as you would expect your query

play14:20

operations but it works in a similar but

play14:23

also a very different way so the query

play14:25

cache consists of an a mapping between

play14:27

an ID and a list of objects because as

play14:31

we know when we submit a query we submit

play14:34

a key that we're interested in and also

play14:36

a criteria in the range key case maybe

play14:39

it's your range key is date and you only

play14:42

want things that are within the last

play14:43

seven days so how the query cache works

play14:46

is slightly different so when a request

play14:49

comes in with this combination say c1

play14:51

plus a date criteria the same flow of

play14:55

operations will happen will first look

play14:57

into the cache see if we have this

play15:00

combination of criteria ever queried for

play15:03

before if we don't then we'll go and

play15:05

retrieve that data and it finally will

play15:08

store that data inside the cache for

play15:11

later use so if someone calls again

play15:13

within the next 5 minutes or so then

play15:15

we'll just retrieve from the cache a B

play15:18

and C and serve up that data very very

play15:21

quickly so obviously with this

play15:23

implementation you get very exceptional

play15:25

performance you don't have to actually

play15:27

perform a query on your dynamo table

play15:29

instead you're just looking in a cache

play15:31

and it's an over the 1 lookup so you get

play15:33

that data

play15:34

back right away but I've encouraged you

play15:36

to do a thought exercise here about why

play15:38

this is a problem and maybe you want to

play15:40

pause it to think about it a little bit

play15:42

because these rows right they're

play15:44

replicated a b and c they're replicated

play15:47

in the query cache the a is also

play15:49

replicated in the item cache and then we

play15:52

have a transactions database which is

play15:55

the authority on this data so I want you

play15:58

to pause for a second if you can try to

play16:00

think of a scenario when you update

play16:01

items in this table problems with what

play16:04

data you serve back so maybe you want to

play16:07

take a pause take a minute or so to

play16:08

think about that right now so hopefully

play16:10

you thought about this problem and let's

play16:12

walk through why this set up is an issue

play16:14

and why you should probably never be

play16:16

using query caches so think about this

play16:19

case write the set up that I've already

play16:21

described to you we had a previous

play16:22

lookup where we did see 1 and 8 and we

play16:25

retrieved item aim okay

play16:26

we've already recognized the fact that

play16:29

if a update occurs on the Dynamo table

play16:32

there is a delay between when that

play16:35

update is in the Dynamo table and when

play16:38

that update is reflected in the item

play16:40

cache right so that's problem number one

play16:43

there's an inherent delay between these

play16:44

two tables that problem is enhanced in

play16:47

the query cache scenario because what

play16:50

query cache is do is they cache a

play16:52

criteria and a list of results so if any

play16:57

of these results if a B or C changes

play17:00

while this row exists in your query

play17:04

cache then the next time a query comes

play17:06

in for C one plus this specific criteria

play17:09

this data may have already changed on

play17:12

the DynamoDB table and you will never

play17:14

know it so this is part of the reason

play17:16

that caches on the query side are very

play17:19

very ill-advised you have the very high

play17:22

likelihood to serve up stale data so be

play17:25

very very careful about working with

play17:27

caches when you're using queries so this

play17:30

was a more in-depth look about how it

play17:32

works behind the scenes now I kind of

play17:35

want to go into best practices things

play17:37

you should know about when you're using

play17:38

Dax or maybe you're thinking about using

play17:41

it in your next project so the first one

play17:43

is directly tied to my previous point

play17:45

and that's to

play17:47

don't cache queries unless you

play17:49

understand the implications so

play17:50

understand that you may have

play17:51

inconsistent data if you're using your

play17:54

dynamo table as a source of truth that

play17:56

is for some very mission critical

play17:58

information maybe it's like credit card

play18:00

transactions then you don't want to be

play18:02

using query caches you may not even want

play18:04

to use caches in general since by

play18:06

definition they introduced eventual

play18:08

consistency a second one is from the

play18:11

monitoring perspective and insta monitor

play18:13

and scale your notes accordingly based

play18:15

on workload as you know you're using ec2

play18:18

machines you're paying for that

play18:19

always-on functionality I don't think

play18:22

there's auto scaling for Docs yet but if

play18:24

there is there'll be a great new feature

play18:25

but as a result you really need to

play18:27

monitor your usage pattern I would

play18:29

suggest starting with the lowest level

play18:31

type of hardware see if that fits your

play18:33

use case and then maybe you can add more

play18:35

nodes to serve as read replicas or beef

play18:37

up your replicas to be more intensive so

play18:41

they can serve more traffic and the

play18:43

third point is to be aware of single

play18:46

point of failures if you decide to share

play18:48

your cluster across multiple tables this

play18:50

is a kind of a neat feature you can sit

play18:52

you can share your cluster across

play18:54

multiple DynamoDB tables so you can

play18:56

store all amounts of data in there and

play18:58

they could be totally unrelated the

play19:00

problem with this is that if you're

play19:01

sharing this across different services

play19:03

or different use cases it's possible

play19:05

that a read or write access pattern to

play19:08

one table can cause a cascading failure

play19:11

in your cache that can all of a sudden

play19:13

start impacting your other application

play19:15

that happens to also use the cache but a

play19:18

different section of the cache

play19:20

however if it starts failing it doesn't

play19:21

matter failures failure there's no

play19:23

section so failure there's no partition

play19:25

you know failure it's either up or down

play19:27

right so in this case you can have

play19:29

certain parts of your application

play19:31

impacting other parts of your

play19:32

application so just be aware of doing

play19:35

this if you decide to take this kind of

play19:37

shortcut and the final one which has to

play19:39

do with eventual consistency is when in

play19:42

doubt use your dynamo to be record as

play19:44

the source of truth don't perform

play19:46

operations where you read from the docs

play19:48

cluster use it as a source of truth and

play19:50

then write to your dynamo table like we

play19:53

said there's eventual consistency so the

play19:55

likelihood of that data being out of

play19:57

date at some point is no I don't want to

play19:59

say hi but it's

play20:00

possible so keep that in mind when

play20:02

you're designing your application be

play20:04

fault tolerant but also try to deliver

play20:06

the best experience possible for your

play20:09

customers

play20:09

so hopefully you like this video I have

play20:11

many more on dynamodb including this one

play20:13

on the right here about DynamoDB schema

play20:15

design also please don't forget to Like

play20:18

and subscribe so that you don't miss out

play20:19

on next week's video thanks so much

play20:22

folks and I'll see you next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
DynamoDBCachingPerformanceDAXDatabaseMicrosecondOptimizationAWSEC2ScalingConsistency