DynamoDB Single Table Data Models: Explainer Video - Maximize AWS DynamoDB Performance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to be looking at dynamodb more specifically i'm going to be focusing on how you can create single table models in dynamodb and i'm going to be doing all this with a real world example hi my name is abhi bhargav i am the ceo of a global application security company called v45 this is my personal channel and i talk about everything from tech to the cloud to security all the code and the tools that i've used for this video are in the descriptions please like subscribe and share this content if you find it useful so dynamodb is a very popular nosql database from amazon's aws now one of the things that you should keep in mind when you're using dynamodb especially to build out real-world applications is that you need to model them right initially when i used to work with dynamodb i didn't think about modeling at all and that was the biggest mistake i made if you really want to create real-world applications that are performant and efficient to work with then you need to model your databases and one of the models that we're going to be looking at is probably the most efficient and performant of all which is the single table model now regardless of the data sets regardless of the views you have all of your data would fit into a single table and that's one of the best ways to get maximum performance out of dynamodb what i'm going to be doing now is we're going to be looking at a single table model and we're going to be implementing it from scratch from the get go from defining the use cases for the application to writing the code in dynamodb and actually running that code against dynamodb so you're going to look at the whole length and breadth of doing a single table model in this video so stay tuned so let's talk about our use case a little bit before we go into our dynamodb model itself so we have developers creating multiple apps now each of these apps goes into a vm as soon as a developer commits this app to a repository that repository pulls it and creates a virtual machine with all the necessary code and deploys all the necessary code now once that virtual machine is created our code actually takes a snapshot of that virtual machine and stores it on our cloud provider now that's pretty simple and it's great especially if you're only managing a few applications but what if you're managing a lot of different applications that means that it creates a lot of different vms it creates a lot of different snapshots with the cloud provider in many cases you will have multiple versions of the same app getting logged as a snapshot in the cloud provider which previously resulted in hundreds of different snapshots and images being stored in that cloud provider so we needed a system to manage this better which is why we came up with this when we wanted to use dynamodb for this so what we wanted to do was that we wanted to have an api that we could query that would talk to dynamodb that would give us the latest image so let's say the developer creates a latest application that would get logged in our api and of course through that to dynamodb the developer would be able to query that from the api which would be fetched from dynamodb and then test out the vm so launch it into a particular vm test out the vm and check whether it works correctly and if that works correctly approve that as the latest image that needs to be loaded every time that application is loaded now this is great because you can create multiple versions of the same app so let's say you create one version on january 1st let's say march 1st to create another version of the same app you can log that as the latest version and that becomes the latest version that all our developers use and that becomes very easy for us to roll out and you can also keep track of the previous versions right that's one of the better things as well so let's say this application or this version of the application doesn't work out for some reason you can always go back to the snapshot of a previous version of that same application and that is all stored in dynamodb so it becomes much more easy for us at this point to actually manage this entire system so let's start modeling or at least start to define our model for the single table dynamodb set that we have right so what we're going to do in this part is we're going to identify the attributes that are going to go into the the table and then we're also going to look at the most important aspect of dynamodb modeling which is access patterns we're going to look at both these things and see how we can make it more efficient so let's write out this in a file let's call this model.md i'm just gonna and this is gonna be available in the descriptions as well so i'm gonna say single table model dynamo db now let's look at the attributes first now we're going to go name based in this so when we go go look at the attributes we're going to go by image name or snap so i'm going to be using the term snapshot and image interchangeably right so i'm going to use image or snapshot they both mean the same thing now image is what we're going to be looking at so image name is the thing that we want to access first so image name is going to be a string now when we store the vm the snapshot or the image in the cloud it gives us an image id so let's say so that also needs to be stored once it's there in the cloud that is an integer right so that's an integer value we also have something called a configuration parameter so every snapshot when we want to restore a snapshot and actually run it as an app on a virtual machine we need a configuration for that virtual machine 2gb 4gb 2vcpu stuff like that so that's going to be a string and we're also going to have a timestamp so we're going to have something called a timestamp which is going to be in my case i'm going to use uh unix epoc so that's going to be an integer you can also use iso formatted date string which can also be a string but i am going to use epoch in this case right now we have the attributes now it's pretty simple these are the attributes we want to be able to capture we may want to capture other attributes but i'm not going to get into that because it's not necessarily very relevant from the perspective of our demo today right so let's look at the access patterns now access patterns are very important when you're doing dynamo db modeling especially single table modeling because you are modeling this based on the kind of ways you are accessing that data and that's why it's very important to write down your access patterns what we like to do at v45 is not only do we write down the access patterns but we also write down changes to those access patterns over time in a doc like a google doc or some shared collaborative documentation that we can actually access and then make changes to it as the model evolves over time so that's what we want to do now this is not the model this just is helping us identify what we need from the database for our applications use cases to get fulfilled let's look at the select aspects right so select is essentially where we are querying the database right so when querying the database we want to be able to select stuff from the database so the first thing we want is to be able to get a the latest version of a given image name so if if we say that hey give me application a or application b we want to be able to get the latest version or the latest image or the latest snapshot of that particular application by name right so name it's not id for me the name is more important because the developer would select by name so my access pattern is going to be by name so in this case you'll see that we're going to get that we may also need to list out all the different image names that we have so get the list of all give of all image names in the system right so this is another thing again we need to have a list of all now the other thing we might also want to select for is we want to check whether there are existing images of the same name so we want to check whether an existing image exists of the same name whether it has versions in the same name let's say we're loading a completely new image into the database that's fine but let's say there's already an existing image we need to version manage that image at that point we don't need to create another entry so perhaps that's going to be the case so we're going to say get an existing image by name right so this these are the three select access patterns that we have now you can also choose to model the insert and delete and all of those access patterns but for the purposes of for the purposes of this video and for the demo i'm just gonna stick to the select patterns and we're going to infer some of the other patterns from the select based patterns that we have so now that we've captured our attributes and our access patterns let's actually start getting down to a model right now one of the things that i love to do when i'm modeling is use an excel spreadsheet or a google spreadsheet in this case i'm going to be using a google spreadsheet that i'm also going to be sharing with all of you you can find that in the description as well now the first thing let's do is let's start with the attributes itself now that we have the attributes i'm going to create two mandatory fields that i need for every dynamodb table i'm going to use the pk and the sk so the pk is essentially your primary key and the sk is your secondary key all right let's start modeling let's take up our first access pattern and see if we can make it fit in fact let's actually take up all three of our access patterns and see if we can start modeling based on that so let's start with our first one which is get the latest version of a given image name now in dynamodb like i said you have the primary key and the sort key now the secondary key is actually called the sort key it could also be called a range key you could use it you could use different names so i'm going to be using a composite key system right in a composite key system one of the things you should understand is that the combination of the primary key and the secondary key or the salt key has to be unique the combination of these two values have to be unique if you are only using a primary key and the dynamodb it's also possible to only use a primary key only the primary key has to be unique in this case since we're doing a single table model it's always better to have a composite key because you can have a combination of values that can be unique and that can also give you different perspectives of that single table model so let's start with our first example let's say the first one is get the latest version of a given image name now let us say our we capture our image name as the primary key and the latest version as v0 right now this works and we'll also start to get our attributes in let's talk about our attributes we have the config attribute we have the image id attribute and we also have the created on timestamp right now if i do so v0 happens to be the latest version v1 v2 v3 so this one can the latest one can be this the previous the v1 can be this fetch v2 can be this fetch the image name so essentially what you can do here is you can say hey get me the primary key of the image name and the sort key based on the v0 which is the latest version of that particular so you can actually do a get for this this also this access pattern also works for the third one which is get an existing image by name so you can say hey get me v2 off this image so if i say v2 you will see that you can get it right however you will see that this doesn't work for the second access pattern which is list all the image names on the system this access pattern doesn't work with this because remember to list you have to be able to list out you need to know the combination of this so when i say this i mean the primary key you need the primary key for sure and you need maybe a begins with on the secondary care to be able to query and get a list but since you don't have the image name you're not going to be able to fit it into this use case so this is a problem now we have an issue we do have a single table model but out of our three access patterns this works with only two of our access patterns now can we make this work with our second access pattern as well absolutely let's continue further now we've seen that this particular model works with the first access pattern which is get the latest version of a given image name however it does not necessarily work with this because if i have to when i query this i can obviously get the latest version of the latest image name so what i do is i essentially do query by image name which is the pk so i do the pk which is image name and sk which is version zero version zero being the latest version right so version one two three four those are all versions that are under version zero so you might have the version one that is version zero also so which means that let's say the first uh snapshot or image that you commit that will become version one and version zero at the same time next one will become version two and version zero so that's how our logic is gonna go but this works but the second one does not work when i try and query this by the second one i'm not going to be able to query it because if i try and list i don't know the image names to list so and remember i have to query in dynamodb one of the things i have to do is i have to query with the combination of both this and this the primary key and the salt key so if i have to query in the with the combination of the primary key and sort key i will not be able to do this i will not be able to actually use this access pattern and one of the other things you should remember in dynamodb is that you can't do a loosely scoped query on the primary key so i can't say that fetch me all the primary keys that are like this or begins with this i can do that with the sort key so i can say fetch me all the images that are v0 right that i can do but i cannot do a combination of this and it won't be unique so i have a problem though so this model definition is not going to work so what i'm going to do is convert this i'll change this out i'm going to remove this stuff and i'm going to change this out let's see what we're going to do that will fit both these use cases so i'm going to say image now this is going to be a hard string image that's why i'm going to i'm going to uh bolden it and i'm also going to make it dark green so this is a this is going to be the primary key image it's just going to be the world image right it's not going to be the image name so here in the sk i'm going to say v 0 0 hash image name so this is going to be the image name right v0 hash image name and i can actually see i can do v1 hash image name v2 hash image name and of course i can have all my attributes which is image id config and of course created on right now this is great because i can say hey db fetch me this specific image name i can query this parameter or i can say so let's take let's make this a real world simulation of sorts right so if i do v0 hash app underscore or app abc and same thing with let's make this image id1001 config 2bcpu 8gb right and create it on 2020 whatever so i'm just going to do it like this right so this this is actually a timestamp so i'm just but i'm just doing it because i don't have timestamp so so that would be uh v0 v1 i'm gonna make it again app abc 2 let's actually yeah that's fine 1 0 0 2 is fine we can again copy this paste it here we can just copy this paste it here we can just do small change to this we can actually make this 16 and 1 0 0 3 so let's say this is the latest version of app abc so this right and i can change the attribute to here as well 1 0 3 thing here and this we can also do created on this right so now if i let's take another application right let's say i add another application app xyz okay now this is two zero zero one and let's say it has one version x y z two zero zero one i'm just gonna copy and i'm just going to copy the same value here as well now let's say i want to fetch all images let's say i want to fetch actually this was wrong i'm just going to change this out let's say i want to fetch all images latest images in our api right or in our database it will fetch this and this i can fetch this by saying give me all images of type v0 or that starts with pk image and sk v0 hash or v0 even so it will give me a list it's going to give me a list of all that let's say i want to query a specific image i can say give me image of v0 of app abc it will fetch this id but if i want to do image of all of these v0 apps i can do that i can do version control let's say i want to fetch v1s of something or be one of another entry i can do that so this way i am able to actually manage my entire table in a very very easy way and my model is very straightforward because i can it's addressed most of my at least the first two of my access patterns that i've actually planned for now let's look at the third access pattern get an existing image by name so let's say so in this case what it's going to be is going to pk of image and let's change this to v0 of hash app abc right so this is going to be our access pattern now this is going to be sk dot begins with i'm going to explain this in a moment so you see what's happening here is we are querying the pk and sk so you're looking at the queries that i'm going to be writing when i'm writing my code so the first one is pretty simple you're querying the specific pknsk and you're getting that one item you're doing a get now get is the fastest fetch so it would it would obviously do it the fastest the second one is a query which is also pretty quick and remember the objective that we want to do is avoid scans any which way we can we want to avoid scans because scans is this heavy query which is not very performant so we want to avoid scan so we're only trying to use get and query wherever we can all right so this is pk image happens if we see in this this of course we're saying pk image escape begins with v0 or v0 hash right so this way it gives us all the latest images in that database and we can paginate and do all of that stuff i'm not going to get into that the next one is going to see whether let us see if it fits into our thing so let's say we want to query v2 of something right so let's say we want to query v2 of a particular image so can we do that with our same model i think so let's look at how we could do that we can fetch images with the pk which is the standard stuff and sk we can do an equals check it say v2 hash app abc that way we can get that specific image by name and existing image by name so now we actually have our entire model that we've done in a single table dynamodb so we don't have to go outside of this dynamodb table at all to fetch anything we don't have to go to another table we don't have to join data from any table nothing of the sun it's much faster and remember we have avoided the use of scan we're only using get and query so it's much much faster than you know other access models that we've had especially with multiple tips so you don't need multiple tables to manage this at all now let's start writing some code to actually put this model into place and see whether our model actually fits now remember there may be a few things we may still have not taken care of so we're going to actually address that as we go along as well so we've mostly i think we've mostly thought of the issues but there may be one or two issues that we've not thought of let's actually go ahead and figure it out so what i'm going to do is i'm going to write some python code where i'm going to implement all of the functionalities that we thought of right so first we're going to create some images and then we're going to fetch the image by name then we're going to get the latest version of any image or and get a list of all images right so our latest version of all these images that's what we're going to do so let's start with our first one which is create image we have created a table here called single table and the single table right now is not populated but we want it of course to be populated over time and if you look at the schema of the single table it has two attributes mandatory attributes which is your pk and your sk which is your primary key and your salt key that's what you've said as pk and sk and the primary key is called the hash key and the sk is called the range key first thing which is create image and we're going to pass in an image name and an image id so the first thing we want to do is check if there is an image that already exists with the same name and if if there is an image that already exists with the same name we are just going to have to increment a version of that image if there is no image of the same name obviously we create a v0 entry and a v1 entry for that image name now let's see how we do that so first we have to do a check i'm just going to so first we're going to check for the image name image exists right then we are going to increment version or create new image right so that's basically what we want to do now let's see how we can do that how do we check for a new image we need to check whether that image name exists right now if i look back on my and you if i look back on my model you will see that it's not exactly going to work right you still see that it's not exactly going to work because hey how do we actually check for that image name existing how do we check for that image name existing we can check again based on v0 and v1 and then see whether we can add it but let's say we want to fetch all the version numbers so let's say we there is an image that already exists and we need to increment that version at some other point how do we manage that and you'll see that this is what's going to trip us up a little bit and this is where our model has failed a little bit we've still not thought of another access pattern which is how do we get the older versions of existing images now we've thought of three access patterns in the first part but now as we get into it we realize that oh we've not thought of this we need to include this access pattern in our model now the good thing is we can do that we've still not gone ahead and built our application so that's a good thing to think about so one of the things you should always do is sit together and do access pattern modeling very very well before you go down the path of single table modeling right so now you will see that this works for v0 you want to fetch the latest versions this works perfectly fine it also works for list of latest versions but it does not work when we want to fetch the older versions of the same apps so what do we do here now instead of this we can make this two different access patterns within the same table so we can now say image name here for the pk and for wherever there is v1 we can remove the additional parameter right so we can say can you remove the additional parameter and we can say hey give me the image name so now what we did is we have actually changed our access pattern a little bit so that we can fetch the older versions of the same image so let's say we have an image called app abc so this would become app abc which is the primary key and we have older versions of app abc and we have the latest version of app abc which still fits into our existing pattern we're not going to change our existing model for one of the patterns that we have but we still have this fetching the older versions of the patterns with this which is not going to work right so that's why we've made this change in fact what we should do is we should add an underscore and i'll explain why we're adding the underscore now what we can do here in this pattern is say hey give me all the late all the versions of app abc so you can say give me image with pk app abc that has an sk beginning with v underscore and all the that we can count the number of entries that come back in that list and increment to that list if it's already in the if it's an existing image right if there is no existing image then we can of course create v1 and then create a v0 entry so for every single time that we need to check we need to make sure that we have a list of all of this if there are any existing images and if there are no existing images then we create the v1 entry and we create a v0 entry like so right that's something we can do v0 meaning the latest version and v1 v2 v3 meaning the we are managing the historical copies of that version as we go along of that application as we go along so now let's actually do that so first what we want to do is check this so we check whether the image exists by querying pk off by the image name which you have sent and we are going to query the sk dot begins with v underscore right so that we are getting all the sks with versions assigned to it so it's querying this access pattern now let's actually get this to work now we have uh so now what we did is we have actually changed our access pattern a little bit so that we can fetch the older versions of the same image so let's say we have an image called app abc so this would become app abc which is the primary key and we have older versions of app abc and we have the latest version of app abc which still fits into our existing pattern we're not going to change our existing model for one of the patterns that we have but we still have this fetching the older versions of the patterns with this which is not going to work right so that's why we've made this change in fact what we should do is we should add an underscore and i'll explain why we're adding the underscore now what we can do here in this pattern is say hey give me all the late all the versions of app abc so you can say give me image with pk app abc that has an sk beginning with v underscore and all the that we can count the number of entries that come back in that list and increment to that list if it's already in the if it's an existing image right if there is no existing image then we can of course create v1 and then create a v0 entry so for every single time that we need to check we need to make sure that we have a list of all of this if there are any existing images and if there are no existing images then we create the v1 entry and we create a v0 entry like so right that's something we can do v0 meaning the latest version and v1 v2 v3 meaning the we are managing the historical copies of that version as we go along of that application as we go along so now let's actually do that so first what we want to do is check this so we check whether the image exists by querying pk off by the image name which you have sent and we are going to query the sk dot begins with we underscore right so that we are getting all the sks with versions assigned to it so it's querying this access pattern let's actually start getting our existing images and checking whether we have any images i've added a couple of lines of code here to refer to the specific table that i want to talk to so i've defined this as a border 3d source and bottom 3 is the aws sdk for python so that's called bottom 3 and we have also referred to a particular table in this case called our single table now let's actually get our list of existing images so what we're going to do is we're going to first write a query now remember we need to write a kind of loosely scoped query not too loosely scoped we're still going a little more specific but there is a possibility that since we're not doing a get we are doing a loosely scoped composite query so we're checking for the image name and then sk begins with a v underscore right so that's what we're doing so we're going to do table dot query key condition expression so we're going to say pk dot equals image name now remember we're passing the image name and sk dot begins with v underscore zero right now if items in existing images and existing images don't get items so what we want to do is we want to check whether there are any items that are returned then of course you are going to increment the value else you are going to add a new entry right so let's see if there are any items that are returned if there are any items returned then we want to count right we want to count the list so we want to get len so we want to get num versions which is equal to existing images the length of existing images dot get items okay so we're going to get the length of that and we're going to add another entry to our database that has the latest version of that so that is going to be the num plus one so our version is going to be so let's say we have four versions already then it's going to add the fifth version if it does not have any version then obviously it's going to go to the else loop so if it has already one version it's going to create a second version it has two versions it's going to create a third version that's what we wanted to do okay so numbers in length of these items that have that have come back from our table and then what we're going to do is we are going to make sure that we have some value that is returned so currently we have the num versions what we are going to do is add another entry so table dot create item it's going to be item i'm just going to create the pk and the sk for now so the pk is going to be image name and the sk is going to be so this is going to be the pknsk the sk is going to be v underscore and let's actually format this sk a bit i'm going to say num versions plus one right so this is going to be the v underscore conversions plus one i'm not going to add any other attributes to this we could add the created on and all that but we're just doing it as a demo so you could add the other attributes but i'm not doing that so we need to add this and we also need to update the version with the latest one so let's actually create the image id right so let's actually add the image id to this as well we're also going to add the image id to this which is image id whatever the user passes is the image id we're going to add to this okay and we're also going to create the same thing as the v0 right now the v0 is going to actually adhere to our model so let's look at our model for v0 so our v0 model looks like this we are going to create a static image pk and then we are going to add a v0 with the image name and the image id that's what we want to do so this is going to be the image all right so let's move back to our code we're going to create a pk with the static image and we are going to say v 0 and of course we're going to change this to image name instead of doing this we are going to change it to our image name so instead of num versions we are going to say image name right so that's what we're going to do now let's say there are no items that are returned so at this point let's say there are no items that have returned at all obviously we need to still create some entries so we're just going to copy these entries over and we are going to create a v1 entry and a v0 entry with that same image id so we are not going to do any formatting here of course we are still going to do formatting here because we want a v0 so if there are no existing items or no existing versions of that application we're just going to create a v1 entry which is the first version of that application and then we're going to create a v0 which is the latest version so the first version in this case is the latest version okay so let's actually see how this works for us all right so now we've written some code to create our image now this code should create not only check whether there's an existing image it should also make sure that if there is no existing image it adds the image and if there is an existing set of images it just increments the version of those existing set of images last time there was a small bug i had actually not added the key you know the key class or created a key object with it that that's the reason the code failed but anyway so now i think this should work now let's open up the terminal and actually start to run this application and i'm going to use the ipython i so that's interactive shell with this particular code now when i do create image with an image name let's call it app abc is the image name and id1001 you should see that it does not have a create item put it up oh it looks like i made a error again this is put item underscore item put item i thought it was create item my bad let's put item and again put all right now hopefully this should work when i create item you'll see that it created it probably created the item now let's look at our table and see what it actually did here if you look at our data let's scan our data and you'll see that it's created two items as we thought right so now it's you see that we created two entries the first version has been created the pk is app abc with v1 imageid1001 however it also creates the latest version now just remember the first version is also the latest version right in this case so v 0 hash app abc image this right so 1 0 0 1. now let's create another version of the same app so let's create another image of app abc this time with image id1002 so let's say our developer has made a change to app abc and has committed a new snapshot of that right so new snapshot it should now let's see what happens to our data if you scan your data you will see now that our v0 the latest version has changed to the v2 value so it's it's found the v1 it clearly knows that v1 is the is the current version it increments it to v2 and with the latest image id and now we have app abc the latest version of app abc is the one that was created from the v2 value of this so if i just create another value like this let's say v3 you'll see now that it creates the another entry with v3 of that particular application now this has actually been done so we've actually been successfully able to add our entire image with that version control logic automatically built in right this is really cool we actually have version managed images just by pushing in this through our api so imagine that this is an api i'm just creating versions and i'm just dumping it in and it automatically handles the versioning right so it goes based on this and it identifies whether there are existing applications and it adds another version to that existing application so you still have records that are maintained at the same time you still have the latest version so if somebody gets the latest version they're pulling out this particular version of the app which is the latest image id right so this is really cool stuff we've been able to get up and running with this model pretty quickly now let's implement the other access patterns as well so now we're done with the create image access pattern now let's do list latest images right so if i want to list the latest images of all types right so let's do it that way so now what we're going to do is we're going to query this again so for querying this i'm going to use another query so list images is going to be table dot query key condition expression key of pk dot eq image remember we are querying with the static image image and we are querying the sk so we are querying the sort key of sk dot begins with and we're going to use v 0 hash so it begins any image that begins with v 0 hash list it up okay so now you will see that we should have our list images so let's again do the same thing as we did before in fact now let's add another app to our list as well just to my python i ttp single table now let's create another image instead of app abc let's create a xyz2001 right so we've created a new app as well so if you go back to our table you should see that we have our new app showing up here it has a v1 right now so we have two applications in fact let's add another application just to give it a list asdf let's create another app called asdf let's use the id3001 so we have three applications and let's also add some versions to all of this asdf let's add another version to it as well now let's pull the latest images right that's what we want to do so now if i do list you will see that we have our latest images listed out all the latest images are the only ones that are listed out so you'll see that our image v0 v0 of asdf and v0 of xyz so now you have exact access to what you want right so you want a specific you want to list let's say you want to populate a drop down with all the latest images you just have to do it and the point the reason why we have this composite key like this v0 hash whatever you can essentially just a string split it and represent it in a drop down you don't have to obviously represent it like this you can just bring string split it and show that up as the app that you want and internally automatically you can refer to it through that particular api so it's really convenient and you can see the access patterns are really fast as well you'll see that it comes back pretty quickly even at scale you'll see that dynamodb gives you a really good read speed on these kind of single table modeled tables right so let's look at another use case of ours that is getting the image by version specifically so the latest image by name def get latest image by name image name okay so here we just have a single get now this is a very quick query it should obviously be very performant and quite efficient so we're just going to do table dot get specific i'm going to call this specific table dot get key is equal to pk primary key is equal to image and the sk is equal to v0 hash and the format i'm just going to use f strings for this just making it a little easier dimensioning right so it's just going to give me the v0 hash image name it's going to get this if it has any so if item so i'm just going to print out the specific dot item specific dot get key item all right so if there are any items that will print it out otherwise it's going to print none so let's see how this works latest need to make that supposed to be get item let me just rework that and you'll see that we get the latest avc so let's say we get we want the asdf get the latest asdf version as well so this as you can see is really convenient right now we are able to we are able to query multiple viewpoints within the same table we don't have to join across multiple tables and make it very complicated it's really simple now the last access pattern that we want is to also be able to get a specific specific image by name and version so i'm just making these very contrived function names please don't use this as the basis to do it so i'm going to say version now this is going to query our v1 v2 v3 type of focus right so this is older version and i'm going to use another get table get item query in this case we're going to do something a little different we're going to say so the image name is going to be the pk that we're going to query the is something that we're going to query so you see what i'm doing here is we're querying the image name and the version underscore 0 1 2 not 0 it's going to be 1 2 3 4 whatever is the not the latest version right so that's what this is going to be querying and of course we are going to print out the specific items and let's actually get this query and you see we get the version three of that application so as you can see this was pretty quick right we were able to implement all of these different access patterns very quickly in dynamodb and we were also able to get it working because one we had modeled it very clearly beforehand we didn't start with those unnecessary you know questions and ambiguities regarding the database itself and then we were able to actually get a model that works for us within the same table we didn't have to split it up so all of this is in a single double model you're able to query it much more efficiently than you could let's say you were let's say you had two different tables for this it would have been much much more difficult to query and at the same time we have completed completely avoided the use of scans we have just used queries and get item requests and we've completed a completely avoided the use of these very very non-performant queries like scan right that's one of the benefits of using a single table model so i hope you enjoyed that video on single table model and i hope you like the demo the code is as always available in the description but i'm going to leave you with a few things before i end the video one i really learned all of this from looking at rick holy hand stock at multiple aws re invent so if you're looking for additional use cases or if you're looking for more examples and a more technical demonstration of what dynamodb can do you should look at those stocks that's in the show notes in the description below as well also you can look at i use dyna dyno base for this particular video but you should look at using nosql workbench which is a free tool from amazon that can help you do modeling as well so definitely take a look at that and thank you and i'm going to see you in subsequent videos thank you very much if you like that video you should like and subscribe to my channel i'm planning to put out a lot more content on the cloud on kubernetes and security as we go along so thank you very much and look forward to seeing you in another video
Info
Channel: Abhay Bhargav
Views: 1,315
Rating: undefined out of 5
Keywords: amazon web services, dynamodb data modeling, cloud tutorials, dynamodb, aws re:invent, aws dynamodb, dynamodb tutorial, dynamodb deep dive, dynamodb basics, dynamodb query, dynamodb design, dynamodb primary key and sort key, aws dynamodb data modeling, dynamodb query vs scan, dynamodb tutorial python
Id: GAIyM_0BIOw
Channel Id: undefined
Length: 47min 14sec (2834 seconds)
Published: Tue Oct 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.