{"componentChunkName":"component---src-templates-blog-js","path":"/ctf/aws-formation/tp8-dynamodb","result":{"data":{"markdownRemark":{"html":"<h1>TP8 - DynamoDB</h1>\n<h2>1. Introduction to DynamoDB</h2>\n<h3>Definition</h3>\n<p>DynamoDB = fast flexible NoSQL database service</p>\n<p>Services with :</p>\n<ul>\n<li>consistent</li>\n<li>\n<p>single-digit milisecond latency at any scale</p>\n<ul>\n<li>stored in SSD Storage </li>\n</ul>\n</li>\n</ul>\n<p>Supports 2 data models:</p>\n<ol>\n<li>document </li>\n<li>key-value </li>\n</ol>\n<p>It is serverless, integrate with lambda\nIt avoids single point of failure (spread accross 3 distinct data centers)</p>\n<h3>2 read consistency models :</h3>\n<ol>\n<li>Eventual (defaults) : best read performance, actualisation within a second</li>\n<li>Strongly </li>\n</ol>\n<h3>Tables</h3>\n<ul>\n<li>item = column</li>\n<li>attributes = row</li>\n</ul>\n<p>Documents can be writtent in JSON, HTML or XML</p>\n<ul>\n<li>key value = userID</li>\n<li>value = 123543</li>\n</ul>\n<h3>Primary keys</h3>\n<p>DynamoDB stores and retieves data based on a primary key</p>\n<p>2 types of primary keys :</p>\n<ol>\n<li>\n<p>partition keys = unique attributes</p>\n<ul>\n<li>output of the hash funciton detemrine the location of the stored data</li>\n<li>no itmes can have the same partition keys</li>\n</ul>\n</li>\n<li>\n<p>composite key = partition keys (userID) + sort key (timestamp of the post)</p>\n<ul>\n<li>allow you to store multiple items with the same Partition keys</li>\n</ul>\n</li>\n</ol>\n<p><img src=\"/assets/TP8-DynamoDB-image-20200722202256124.png\" alt=\"TP8-DynamoDB-image-20200722202256124\"></p>\n<h3>Access control</h3>\n<ul>\n<li>authentification and Access Control is managed using AWS IAM</li>\n<li>you can create an <strong>IAM user</strong> within your AWS account which has specific permission sto access and create DynamoDB tables</li>\n<li>you can create an <strong>IAM role</strong> which enabes you o obtaine teporary access keys which can be used to access DynamoDB</li>\n<li>you can use a special <strong>IAM Condition</strong> to restrict user access to only their own records</li>\n</ul>\n<p>You can add a condition to an IAM Policy to allow access only to items where the partition key value matches their UserID</p>\n<p><img src=\"/assets/TP8-DynamoDB-image-20200722202833266.png\" alt=\"TP8-DynamoDB-image-20200722202833266\"></p>\n<p><em>Partiton key = Leading Key</em></p>\n<h3>remember</h3>\n<ul>\n<li>DynamoDB = low latency NoSQL database</li>\n<li>Consist of tables Items and Attributes</li>\n<li>Support document and key value data model</li>\n<li>support format are JSON XML HTML</li>\n<li>2 types of primary key - Partition Key and combination of</li>\n<li>2 consistency models : Strongly and Eventually</li>\n<li>\n<p>Access is controlled using IAM policies</p>\n<ul>\n<li>Fine grainded access control using IAM condition parameter : <strong>dynamodb:LeadingKeys</strong> to allow access only the items where the partiton key value matches their user ID </li>\n</ul>\n</li>\n</ul>\n<h2>2. Creating a DynamoDB Table Lab</h2>\n<h3>Steps</h3>\n<ol>\n<li>Create IAM Service Role for EC2 for DynamoDB Full Access</li>\n<li>Create EC2 (by configuring instance initializing a php website)</li>\n<li>Connect to EC2 by ssh</li>\n<li>Install [AWS SDK for PHP version 2] (<a href=\"https://docs.aws.amazon.com/aws-sdk-php/v2/guide/installation.html\">https://docs.aws.amazon.com/aws-sdk-php/v2/guide/installation.html</a>) by downloading the <a href=\"https://getcomposer.org/download/\">composer</a></li>\n<li>Change the region of uploaddate.php</li>\n<li>Create the DynamoDB with IPaddress/dynamoDB/createTables.php</li>\n</ol>\n<h3>how to interact with the database using the command line ?</h3>\n<ul>\n<li>\n<p>we will use the IAM service role to interact with DB and make queries</p>\n<ul>\n<li>aws dynamodb get-item --table-name XXX --region XXX --key XX </li>\n<li>using the <code>--key '{\"Id\" : {\"N\" : \"205\"}}'</code></li>\n</ul>\n</li>\n</ul>\n<h2>3. Indexes Deepdive</h2>\n<h3>index definition</h3>\n<ul>\n<li>In SQL database : an index is a data structure which allows to perform fast queris on specific columns in a table (column selection)</li>\n<li>\n<p>In DynamoDB : 2 types of index</p>\n<ul>\n<li>\n<p>Local Secondary index</p>\n<ul>\n<li>can only be created at the table creation but not after</li>\n<li>same partition key  than the original table but a different sort key -> different view</li>\n<li>increase the time queries based on this sort key</li>\n</ul>\n</li>\n<li>\n<p>Gloabal Secondary Index </p>\n<ul>\n<li>can be created at and after table creation</li>\n<li>different partition and sort key</li>\n<li>increase the time queries of all data</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n<p><img src=\"/assets/TP8-DynamoDB-image-20200723011044767.png\" alt=\"TP8-DynamoDB-image-20200723011044767\"></p>\n<h2>4. Scan vs Query API Call</h2>\n<p>A query and a scan <em>return all the attibutes</em> of the items but you can use <em>projection expression</em> to select a specific attributes.</p>\n<h3>Query</h3>\n<p>A query finds items in a table based on the primary key attributes and a distinct value to search for.</p>\n<ul>\n<li>Result are always sorted by the sort key (numeric order, then ASCII character)</li>\n<li>Reverse the ascending order is possile with <em>ScanIndexForwardparameter=F</em> </li>\n</ul>\n<p>By default queries are <em>eventually consistent</em></p>\n<h3>Scan</h3>\n<p>A scan operation examines every item in the table\nBy default <em>returns all the attributes</em></p>\n<h3>Comparison</h3>\n<table>\n<thead>\n<tr>\n<th></th>\n<th>Query</th>\n<th>Scan</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Efficiency</td>\n<td>+</td>\n<td>-</td>\n</tr>\n<tr>\n<td>Dump the entire table ?</td>\n<td>no</td>\n<td>yes</td>\n</tr>\n<tr>\n<td>Can use up the provisioned throughput ?</td>\n<td>no</td>\n<td>yes</td>\n</tr>\n</tbody>\n</table>\n<h3>improve performance</h3>\n<ul>\n<li>setting smaller page size</li>\n<li>larger number of small operation</li>\n<li>avoid using scan operation</li>\n</ul>\n<h4>of scan</h4>\n<p>share your datable into segments and scan them in parallel</p>\n<h2>5. DynamoDB Provisioned Throughput</h2>\n<p>DynamoDB Provisioned Throughput is measured in Capacity Units</p>\n<p>When you create a tabl, you specify your requirements in terms of Read Capacity Units and Write Capacity Units.</p>\n<ul>\n<li>1 x Write capacity Unit = 1 x 1Kb write per second</li>\n<li>\n<p>1 x Read capacity Unit </p>\n<ul>\n<li>= 1 x 4Kb <em>Strongly</em> consistent read</li>\n<li>= 2 x 4Kb <em>Eventually</em> consistent read</li>\n</ul>\n</li>\n</ul>\n<h3>steps to know how many do we need</h3>\n<ol>\n<li>calculate how many Read CU needed for each read : <strong>Size of each item / 4 Kb</strong></li>\n<li>Rounded-up</li>\n<li>Multiply by the number of read per second</li>\n</ol>\n<h2>6. DynamoDB On Demand Capacity</h2>\n<ul>\n<li>\n<p>Charges aply for :</p>\n<ul>\n<li>reading</li>\n<li>writting</li>\n<li>storing</li>\n</ul>\n</li>\n<li>on demand, you do not need to specify your requirements</li>\n<li>DynamoDB instantly scales up and down based on the activity of you applications</li>\n<li>great for unpredictable workloads</li>\n<li>you want to pay for only what you use</li>\n</ul>\n<h3>pricing model shoul I use ?</h3>\n<p><img src=\"/assets/TP8-DynamoDB-image-20200723013721046.png\" alt=\"TP8-DynamoDB-image-20200723013721046\"></p>\n<p>you can change of pricing model once a day.</p>\n<h2>7. DynamoDB Accelerator (DAX)</h2>\n<h3>definition</h3>\n<p>*<em>DAX</em> = is a fully managed clustered in-memory cached for DynamoDB</p>\n<p>Delivers up to a <em>10x</em> read performance improvments = microsecond performance for millions of request per sec (Christmass or Black Friday) -> Ideal for Read-Heavy and bursty workloads</p>\n<h3>how it works</h3>\n<p>If the item is not available (cache miss) then DAX performs an Eventually Consistent GetItem operation against DynamoDB</p>\n<h3>NOT suitable for</h3>\n<ul>\n<li>NOT suitable for application requiring <em>strongly consistent</em> reads</li>\n<li><em>write</em> intensive application</li>\n<li>few read operations</li>\n<li>application that do not need microseconds response times</li>\n</ul>\n<h2>9. DynamoDB Transactions</h2>\n<p>ACID transactions : Atomic (single unit, all or nothing operations) Consistent (must let datatable in a valid state) Isolated (not dependency in transaction) Durable (when a transaction have been commited it will remain in the data table)</p>\n<p>Read or write multiple items across multiple tables as an all or nothing operations.</p>\n<h2>10. DynamoDB TTL</h2>\n<h3>definition</h3>\n<p>TTL : <strong>Time To Live</strong> defines an expiry time for your data</p>\n<p>It is relevant to remove old data (session data event logs ...) and so reduce cost storage data</p>\n<p>It is expressed as POSIX (Unix Time = )\n<a href=\"www.EpochConverter.com\">EpochConverter</a>\nL'heure Unix ou heure Posix (aussi appelée Unix timestamp) est une mesure du temps basée sur le nombre de secondes écoulées depuis le <strong>1er janvier 1970 00:00:00 UTC</strong>, hors secondes intercalaires.\nLes quatre premières lettres forment l’acronyme de Portable Operating System Interface (interface portable de système d'exploitation), et le X exprime l'héritage UNIX. </p>\n<h3>select items on TTL</h3>\n<p>steps :</p>\n<ol>\n<li>check your IAM user permissions (<em>aws iam get-user</em>)</li>\n<li>create a sessionData table (<em>aws dynamodb create-table</em>)</li>\n<li>populate sessionData table (<em>aws dynamodb batch-write-item</em>)</li>\n</ol>\n<h2>11. DynamoDB Streams</h2>\n<h3>definition</h3>\n<p>It s a time ordered sequece (or streams).\nSo any modification at the item level (insert update, delete) will be saved in the DDB streams with a encrypted log during <strong>24h</strong>.\nThey are used to trigger event or lambda event based on a change of the DDB table\nBy default the primary key is recorded.\nBefore and After images can be captured.</p>\n<p><img src=\"/assets/TP8-DynamoDB-image-20200723020935605.png\" alt=\"TP8-DynamoDB-image-20200723020935605\"></p>\n<p>The DDB endpoint is different than DDB streams endpoint</p>\n<h2>12. Provisioned Throughput Exceeded &#x26; Exponential Backoff</h2>\n<p>If you see <strong>Provisioned Throughput Exceeded error</strong> it means the number of request is too high</p>\n<h3>Provisioned Throughput Exceeded <em>exception</em></h3>\n<p>If you request rate is too high for the read / write capacity provisionned on you DDB table. SDK will automatically retries requests untill successful</p>\n<h3>exponential backoff</h3>\n<p>If we do not use SDK, we can :</p>\n<ul>\n<li>reduce the request frequency</li>\n<li>use the exponential backoff</li>\n</ul>\n<p>Exponential backoffs improves flow by retrying request using progressively longer waits (10 20 40 80 ms...)\nIf the waiting exceed 1min, your  request may exced the throughtput of your red/Write capacity. </p>","frontmatter":{"date":"July 31, 2020","path":"ctf/aws-formation/tp8-dynamodb","title":"TP8 - DynamoDB","tags":["cloud","ec2","aws"],"categorie":"ctf","thumbnail":"/assets/alex-machado-80sv993luki-unsplash.jpg"},"fields":{"readingTime":{"text":"8 min read"}}},"file":{"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAGFz11jKaIn/8QAGhAAAgMBAQAAAAAAAAAAAAAAAAECERMSMf/aAAgBAQABBQLm3gxwaJTxe1i8/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECElH/2gAIAQMBAT8BUdKn/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAECAQE/Abqv/8QAGhAAAQUBAAAAAAAAAAAAAAAAAQACEBESIv/aAAgBAQAGPwJckOjObVBtR//EABwQAQACAgMBAAAAAAAAAAAAAAEAETFBIWFxkf/aAAgBAQABPyEVB9ih2jUSxfkFQDkWX4Ju5grU/9oADAMBAAIAAwAAABDjD//EABYRAQEBAAAAAAAAAAAAAAAAAAEAIf/aAAgBAwEBPxAA1M//xAAWEQEBAQAAAAAAAAAAAAAAAAABIRD/2gAIAQIBAT8QWYTP/8QAGhABAQEAAwEAAAAAAAAAAAAAAREAIUFxwf/aAAgBAQABPxAYkFlcFzRnCFCH3zCCggiKI5gk6hL1lpwEhs0zEABv/9k=","aspectRatio":1.4970059880239521,"src":"/static/11cdcb302d032fa2db3a46428a8f74c9/a7715/alex-machado-80sv993luki-unsplash.jpg","srcSet":"/static/11cdcb302d032fa2db3a46428a8f74c9/8f7df/alex-machado-80sv993luki-unsplash.jpg 250w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/0f3a1/alex-machado-80sv993luki-unsplash.jpg 500w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/a7715/alex-machado-80sv993luki-unsplash.jpg 1000w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/37d86/alex-machado-80sv993luki-unsplash.jpg 1500w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/a41d1/alex-machado-80sv993luki-unsplash.jpg 2000w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/6a059/alex-machado-80sv993luki-unsplash.jpg 6000w","sizes":"(max-width: 1000px) 100vw, 1000px"}}}},"pageContext":{"slug":"ctf/aws-formation/tp8-dynamodb","featuredImage":"alex-machado-80sv993luki-unsplash.jpg"}}}