AWS DynamoDB Interview Questions: The Ultimate Guide
Introduction to DynamoDB
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. As a non-relational database, DynamoDB gives you flexibility with your data models and reliable performance at any scale. It‘s an excellent choice for use cases like gaming, ad tech, IoT, and many other applications that need low-latency data access at a massive scale.
At a high level, DynamoDB stores data in tables. Each table contains multiple items, and each item consists of one or more attributes. You can think of items as rows and attributes as columns in a spreadsheet.
DynamoDB provides two read/write capacity modes for each table:
- On-demand mode – DynamoDB instantly accommodates your workloads as they ramp up or down to any previously reached traffic level. You pay only for the reads and writes your application performs.
- Provisioned mode – You specify the number of reads and writes per second that you expect your application to require. You can use auto scaling to automatically adjust your table‘s provisioned capacity based on the actual traffic patterns.
Some of the key advantages of using DynamoDB include:
- Serverless – There are no servers to provision, patch, or manage
- Distributed – Data is automatically replicated across multiple availability zones for high availability and durability
- Secure – Provides encryption at rest and in transit
- Event-driven – Integrate with AWS Lambda to execute custom logic based on table updates
Now let‘s dive deeper into core DynamoDB concepts that are essential to understand for interviews.
Core DynamoDB Concepts
Tables, Items, and Attributes
A DynamoDB table is a collection of items, and each item is a collection of attributes. There is no limit on the number of items you can store in a table. An attribute is a fundamental data element that does not need to be broken down any further. Attributes can be scalar (single value) or complex (list, map, set).
Primary Keys
When you create a table, you must specify the primary key of the table in addition to the table name. DynamoDB supports two types of primary keys:
– Partition Key – A simple primary key consisting of a single attribute known as the partition key. DynamoDB uses the partition key‘s value as input to an internal hash function that evenly distributes data across partitions for scalability.
– Partition Key and Sort Key – A composite primary key composed of two attributes: a partition key and a sort key. DynamoDB uses the partition key value as input to an internal hash function that evenly distributes data across partitions. All items with the same partition key are stored together, in sorted order by sort key value.
Secondary Indexes
You can create one or more secondary indexes on a table to provide additional access patterns to your data. DynamoDB supports two types of indexes:
– Global secondary index – An index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table across all partitions.
– Local secondary index – An index that has the same partition key as the base table but a different sort key. A local secondary index is "local" in the sense that every partition of a local secondary index is scoped to a base table partition that has the same partition key value.
Read Consistency
DynamoDB supports eventually consistent and strongly consistent reads.
– Eventually Consistent Reads – The response might not reflect the results of a recently completed write operation. However, eventually consistent reads are significantly cheaper than strongly consistent reads.
– Strongly Consistent Reads – The response will always reflect the most up-to-date data. DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful.
Provisioned Throughput
Provisioned throughput is the maximum amount of capacity that an application can consume from a table or index. Read capacity units (RCU) and write capacity units (WCU) are the units of measure for provisioned throughput.
– One RCU provides one strongly consistent read per second, or two eventually consistent reads per second, for items up to 4 KB in size.
– One WCU provides one write per second for items up to 1 KB in size.
Partitions and Data Distribution
DynamoDB stores data in partitions. A partition is an allocation of storage for a table, backed by solid state drives (SSDs) and automatically replicated across multiple Availability Zones within an AWS Region.
To manage table and index data, DynamoDB divides a table‘s items into multiple partitions and distributes them across multiple servers using the following process:
- DynamoDB uses the partition key value as input to an internal hash function that determines the partition in which the item will be stored
- DynamoDB stores all items with the same partition key value physically close together for efficient access
As a table grows, DynamoDB splits partitions to maintain performance. You don‘t have to manage partitions yourself—DynamoDB handles that for you behind the scenes.
DynamoDB API Operations
DynamoDB provides API operations for creating and managing tables, reading and writing data, querying and scanning tables, and more. Here are some of the commonly used operations:
Creating and Managing Tables
– CreateTable – Creates a new table
– DescribeTable – Returns information about a table
– ListTables – Returns a list of all tables associated with the current account and endpoint
– UpdateTable – Modifies the provisioned throughput settings, global secondary indexes, or DynamoDB Streams settings for a table
– DeleteTable – Deletes a table and all of its items
Reading Data
– GetItem – Retrieves a single item from a table by primary key
– BatchGetItem – Retrieves up to 100 items from one or more tables
– Query – Retrieves all items that have a specific partition key
– Scan – Retrieves all items in a table or a secondary index
Writing Data
– PutItem – Creates a new item or replaces an existing item in a table
– UpdateItem – Modifies attributes of an existing item
– DeleteItem – Deletes a single item from a table by primary key
– BatchWriteItem – Puts or deletes up to 25 items in one or more tables
Advanced DynamoDB Features
Beyond the core functionality, DynamoDB offers several advanced features for more sophisticated use cases:
DynamoDB Streams
DynamoDB Streams is an optional feature that captures data modification events in a table. Each event represents a single data modification in the table and contains the "before" and "after" images of the modified items. You can use DynamoDB Streams together with AWS Lambda to create triggers that automatically react to events in your table.
Global Tables
Global tables provide a fully managed solution for deploying a multi-region, multi-master database. When you create a global table, you specify the AWS regions where you want the table to be available. DynamoDB performs all of the necessary tasks to create identical tables in these regions and propagate ongoing data changes to all of them.
Transactions
DynamoDB transactions simplify the developer experience of making coordinated, all-or-nothing changes to multiple items both within and across tables. Transactions provide atomicity, consistency, isolation, and durability (ACID) guarantees.
On-Demand Capacity
On-demand is a flexible capacity mode for DynamoDB tables that allows you to pay only for the read and write requests your application performs on your tables. DynamoDB instantly accommodates your workloads as they ramp up or down to any previously observed traffic level.
Auto Scaling
DynamoDB auto scaling dynamically adjusts provisioned throughput capacity in response to actual traffic patterns. This enables tables or global secondary indexes to increase their provisioned read and write capacity to handle sudden spikes in traffic without throttling.
Backup and Restore
DynamoDB allows you to create full backups of your tables for long-term retention and archival. You can back up tables from a few megabytes to hundreds of terabytes of data. Table backups are asynchronous, and they don‘t affect performance or availability of your production applications.
Encryption at Rest
DynamoDB encrypts all user data stored at rest using encryption keys stored in AWS Key Management Service (AWS KMS). This adds an additional layer of data protection by securing your data from unauthorized access to the underlying storage.
Time to Live (TTL)
Time to Live (TTL) lets you set a specific timestamp to delete expired items from your tables automatically. With TTL enabled on a table, DynamoDB deletes expired items in the background with no additional cost and without affecting read or write traffic to the table.
Best Practices and Common Mistakes
To get the most out of DynamoDB, it‘s important to follow best practices and avoid common pitfalls:
Best Practices
– Design your tables with your queries in mind. Align your access patterns with your table‘s primary key and secondary indexes.
– Choose the right partition key. Aim for a large number of distinct partition key values and items that are evenly distributed across those partition key values.
– Use eventually consistent reads whenever possible for better performance and lower costs. Reserve strongly consistent reads for when you absolutely need the latest data.
– Understand your workload‘s access patterns and provision the right amount of read and write capacity. Over-provisioning wastes money while under-provisioning risks throttling.
– Prefer queries over scans whenever possible. Queries are more efficient as they look up items by primary key.
– Use parallel scans rather than large, sequential scans to avoid consuming excessive read capacity.
– Cache frequently accessed items in memory to reduce the read load on tables.
– Monitor your tables and auto scale provisioned capacity to adapt to changing demand.
– Test your workload at scale before going into production to identify potential issues.
Common Mistakes
– Choosing a suboptimal partition key that doesn‘t distribute data evenly
– Over-normalizing or under-normalizing your data model
– Using scans excessively instead of leveraging queries with appropriate indexes
– Forgetting to provision enough read or write capacity for your workload
– Not considering eventually consistent reads where applicable
– Using transactions unnecessarily and impacting performance
– Neglecting to set a reasonable TTL for expired data
– Not leveraging DynamoDB Streams and Lambda for reactive processing
– Trying to use DynamoDB like a relational database rather than embracing its key-value nature
Latest Updates
AWS regularly adds new features and enhancements to DynamoDB. Here are some recent updates:
- Partiql – A SQL-compatible query language for DynamoDB that makes it easier to run ad hoc queries and integrates with a broader ecosystem of tools
- Kinesis Data Streams for DynamoDB – Automatically captures item-level changes in a DynamoDB table and replicates the changes to a Kinesis data stream
- Multi-Region Transactions – Enables ACID transactions spanning multiple AWS regions
- Contributor Insights – Provides visibility into your DynamoDB tables‘ most frequently accessed and throttled keys
- Export to S3 – Allows you to export DynamoDB table data to an S3 bucket in JSON, CSV, or Ion format
Example Architectures
DynamoDB powers numerous applications across startups and enterprises. Here are a couple real-world examples of DynamoDB architectures:
Netflix
Netflix uses DynamoDB as a key-value store in its infrastructure for scenarios requiring low latency and high throughput. For instance, the popular "Continue Watching" feature relies on DynamoDB to store the state of every active video session. Whenever a user pauses a show, the application writes the current position to DynamoDB. When the user resumes watching, the application fetches the last viewed offset from DynamoDB and continues playing from there.
Lyft
Lyft uses DynamoDB to store real-time GPS locations and movements of drivers and riders. The Lyft app continuously sends location updates from each driver‘s phone to DynamoDB. When a rider requests a ride, the system queries DynamoDB to find nearby available drivers. All this happens within milliseconds at a massive scale.
Sample Interview Questions
Finally, here are some sample DynamoDB interview questions ranging from basic to advanced:
Basic
1. What is DynamoDB and what are its key features?
2. How does DynamoDB differ from traditional relational databases?
3. What are the two types of primary keys in DynamoDB?
4. What is the difference between provisioned and on-demand capacity modes?
5. How does DynamoDB ensure high availability and durability of data?
Intermediate
1. What are the two types of secondary indexes in DynamoDB and when would you use each?
2. How does DynamoDB distribute data across partitions and what are the implications for performance?
3. What are the differences between a query and a scan in DynamoDB?
4. How do you handle transactions across multiple items or tables in DynamoDB?
5. What are some best practices for designing your DynamoDB tables for optimal performance?
Advanced
1. How can you use DynamoDB Streams and Lambda to build reactive, event-driven architectures?
2. What are the trade-offs between using transactions and designing around an eventually consistent model in DynamoDB?
3. How would you design a DynamoDB data model to support a social network‘s friend graph traversals?
4. How can you optimize costs while ensuring adequate performance for spiky, unpredictable workloads in DynamoDB?
5. What are some common anti-patterns or pitfalls to avoid when using DynamoDB at scale?
Acing a DynamoDB interview requires a solid grasp of core concepts, an understanding of advanced features and best practices, and the ability to design efficient data models and architectures. Hopefully this comprehensive guide has equipped you with the knowledge and resources to tackle your next DynamoDB interview with confidence. Remember, there‘s no substitute for hands-on experience, so be sure to practice working with DynamoDB to solidify your skills. Best of luck!