What Does Local secondary index Mean?
On This Page
Quick Definition
A local secondary index is like having an extra index in a book that helps you look up information using a different detail, but only within the same section. It lets you search for data in a database using a different attribute than the main key, but it is stored in the same storage space as the main table. This means you can run queries on additional columns without having to copy data to another table, but the index is limited to items that share the same main key.
Commonly Confused With
A global secondary index (GSI) has a different partition key and sort key from the base table. It can be queried without specifying the base table's partition key, allowing you to look up items across all partitions. A local secondary index (LSI) has the same partition key as the base table, so it is limited to a single partition. GSIs only support eventually consistent reads, while LSIs support both strongly consistent and eventually consistent reads.
If you want to find all orders with a total above $100 regardless of customer, use a GSI on OrderTotal. If you want to find orders for customer X with a total above $100, use an LSI on OrderTotal with CustomerID as the partition key.
In a relational database like MySQL, a secondary index is any index other than the primary key index. It can be created on any column, and queries can use it without being restricted to a single partition (because relational databases do not use partition keys in the same way as DynamoDB). Local secondary indexes in NoSQL databases are a specialized concept tied to the partition/sort key model, not found in traditional SQL databases.
In MySQL, you can create an index on the 'city' column and query all rows where city = 'New York' in one query. In DynamoDB, an LSI would require you to also specify the partition key, so you could only query within one customer's data.
A composite primary key consists of a partition key and a sort key that together uniquely identify an item. This is the primary key of the base table itself. A local secondary index uses the same partition key but a different sort key. The composite primary key defines the base table's data organization, while the LSI provides an additional sort order on top of the existing partitioning.
A table with CustomerID (partition) and OrderDate (sort) has a composite primary key. An LSI on the same table might use CustomerID (partition) and OrderTotal (sort). The composite primary key is required for uniqueness; the LSI is optional for query flexibility.
Must Know for Exams
Local secondary indexes appear in several major cloud certification exams, most notably the AWS Certified Solutions Architect – Associate, AWS Certified Developer – Associate, and AWS Certified Database – Specialty exams. In these exams, you will encounter scenario-based questions where you need to decide whether to use a Local Secondary Index (LSI) or a Global Secondary Index (GSI). The exam objectives for those certifications include sections on designing high-performing and cost-optimized database architectures, and understanding indexing strategies is a direct part of that.
For example, in the AWS Solutions Architect exam, you might see a question like: "A company has a DynamoDB table with CustomerID as the partition key and OrderDate as the sort key. They want to query all orders for a specific customer sorted by order total. What is the most efficient way to achieve this?" The correct answer would be to create a local secondary index with OrderTotal as the alternate sort key, because the query is limited to a single customer (single partition). The distractor options might include creating a GSI, scanning the entire table, or using Elasticsearch. The exam tests your understanding that an LSI is efficient for single-partition queries with an alternate sort key.
Similarly, in the Developer exam, questions often focus on performance and consistency. You might be asked: "Which type of index supports strongly consistent reads?" The answer is LSI (within the same partition), while GSI only supports eventually consistent reads. Traps can include answers that suggest you can use a GSI for strongly consistent reads, or that LSIs can be used without specifying a partition key in the query. The exam expects you to know that a Query operation on an LSI requires the partition key.
Another common question pattern involves the size limitations and the number of indexes. For example: "A DynamoDB table has three local secondary indexes. Can you add two more?" Yes, because the maximum is five. But if the question asks about data size per partition, you need to remember the 10 GB limit including all LSIs. The exams might also test the difference in projected attributes: when you project all attributes into an LSI, you avoid table fetches but increase storage costs. Choosing the right projection strategy is a valid exam objective.
for general IT certifications, local secondary indexes are a specific topic within the broader data storage domain. While they are most prominent in AWS exams, the concept also appears in discussions about other NoSQL databases like Apache Cassandra (where similar concepts are called secondary indexes, though with different behavior). Being able to explain the partitioning limits, consistency models, and query constraints of an LSI is essential for answering scenario-based questions correctly. Ignoring these details can lead to selecting a suboptimal design, which is a common mistake in both exams and real-world implementations.
Simple Meaning
Imagine you have a big filing cabinet with many drawers, and each drawer is labeled with a customer ID number. Inside each drawer, all the files are sorted by date. If you want to find all files for a specific customer that were created after a certain date, you can easily flip through the files in that drawer because they are already in date order. That is the main way you organize your files.
Now, what if you also want to find all files for that same customer where the total order amount is greater than one hundred dollars? Without any extra help, you would have to look at every single file in that drawer, one by one, to check the amount. That would be slow and tedious.
A local secondary index solves this problem. It is like adding a second set of tabs inside the same drawer, but this time the tabs are sorted by order amount instead of by date. So for that same customer ID (the same drawer), you can quickly jump to the section where all orders are above one hundred dollars. The key point is that this second sorting system only works within that one drawer. You cannot use it to look across different customers. This is exactly what a local secondary index does in a database: it lets you sort and search data using a different attribute, but only for items that share the same primary key. It is fast and efficient because it avoids scanning every record, but it is limited to a single partition of data.
Full Technical Definition
In database systems, particularly in NoSQL databases like Amazon DynamoDB, a local secondary index (LSI) is an index that has the same partition key as the base table but a different sort key. The base table is organized by a primary key, which can be a simple primary key (just a partition key) or a composite primary key (partition key and sort key). The LSI uses the same partition key but allows you to define an alternate sort key. This alternate sort key can be any other attribute (scalar data type) from the base table.
When you create an LSI, you specify an alternate sort key attribute. The index is maintained automatically by the database engine. Every time a write operation (insert, update, or delete) occurs on a base table item, the database engine also updates the associated LSI entries. The LSI stores a copy of the base table items, but only the attributes that are projected into the index. You can control which attributes are projected: only keys (KEYS_ONLY), all attributes from the base table (ALL), or a specific set (INCLUDE). Projecting all attributes can lead to higher storage costs but reduces the need for additional read operations to fetch data from the base table.
In terms of standards and protocols, there is no single protocol that governs local secondary indexes. They are an implementation detail of specific database products. For Amazon Web Services (AWS), DynamoDB LSIs are a central part of the data modeling strategy. The AWS SDKs and the DynamoDB API provide operations such as Query and Scan, which can be performed against the LSI. A Query against an LSI requires you to specify the partition key, because the index is organized by partition. You cannot scan an entire LSI without a partition key; scanning is limited to items within one partition.
From a real IT implementation perspective, LSIs are often used for access patterns that require filtering or sorting on multiple attributes within a single customer or entity. For example, an e-commerce application might store orders with CustomerID as the partition key and OrderDate as the sort key. A local secondary index could have OrderTotal as the alternate sort key, allowing queries like "get all orders for customer X with order total greater than Y." This avoids expensive table scans and reduces read costs. However, LSIs have limitations: you can have a maximum of five LSIs per base table, the alternate sort key attribute must be of a scalar type (string, number, binary), and the index is limited to items within the same partition key. There is also a 10 GB size limit per partition key value when accounting for both the base table and all local secondary indexes combined.
a local secondary index is a powerful optimization tool for database queries that need to sort or filter by non-primary attributes, but only within the context of a single partition. It is widely used in cloud-native applications where read performance and cost efficiency are critical.
Real-Life Example
Think of a large library where each shelf is dedicated to a single author. On each shelf, the books are arranged alphabetically by title. This is your main way of finding books: you go to the author's shelf, then look for the title. If you want to find all books by that author published after the year 2000, you can just scan the shelf quickly because the books are already in title order, but you still have to check the publication year on each book.
Now imagine the librarian adds color-coded stickers on the spines of the books, and these stickers are organized by publication year. The stickers are placed on the books on the same shelf. So for a given author, you can now look at the stickers and immediately see which books were published after 2000, without having to pull out each book. The stickers are your local secondary index. They only work within that author's shelf. You cannot use the stickers to find books by different authors that were published after 2000, because each author's shelf has its own set of stickers. To do that, you would need a separate index that covers the entire library, which would be a global secondary index.
In this analogy, the author is the partition key, the book title is the main sort key, and the publication year is the alternate sort key defined in the local secondary index. The stickers (the index) are maintained as new books are added or removed. This makes your search within one shelf very efficient, but you lose the ability to search across shelves using that same alternate key. Understanding this limitation is crucial when designing your database queries.
Why This Term Matters
In practical IT contexts, using the right indexing strategy can make the difference between a responsive application and one that times out or costs too much. Local secondary indexes are especially important in cloud databases like DynamoDB, where read and write capacity can be expensive. Without an LSI, a query that needs to filter or sort on a non-key attribute would force a full table scan of an entire partition, consuming read capacity units (RCUs) unnecessarily. For high-traffic applications, this can quickly degrade performance and inflate costs.
LSIs allow for more flexible data access patterns without requiring data duplication or complex application-level sorting. For instance, in a user activity logging system, you might have UserID as the partition key and Timestamp as the sort key. If you also need to query by activity type (e.g., "show all login events for user X"), creating an LSI with activity type as the alternate sort key makes that query efficient. Without it, you would have to scan all events for that user and filter them in application code, which is slower and less elegant.
Another reason LSIs matter is consistency. Local secondary indexes in DynamoDB support strongly consistent reads, whereas global secondary indexes (GSIs) only support eventually consistent reads. For applications that require immediate consistency after a write, such as banking or order processing, LSIs are the correct choice when the query scope is limited to a single partition. However, this performance comes with trade-offs: you cannot have more than five LSIs per table, and the total data size per partition (including the base table and all LSIs) cannot exceed 10 GB. If your data for a single partition key grows beyond that, you need to consider sharding or redesigning your key schema.
Finally, understanding LSIs is a core competency for database architects and cloud engineers. Many certification exams, including the AWS Certified Solutions Architect and Developer exams, include questions that test your ability to choose between a local secondary index and a global secondary index based on access patterns and consistency requirements. Misusing indexes can lead to exam failures and real-world production issues. So, knowing why LSIs matter helps you make informed design decisions and pass exams.
How It Appears in Exam Questions
In certification exams, questions about local secondary indexes typically fall into three categories: design/architecture, performance/cost optimization, and troubleshooting. In design questions, you are given a business requirement and a data model, and you must choose the most appropriate indexing solution. A typical question might state: "A social media application stores user posts with UserID (partition key) and PostTimestamp (sort key). The app needs to display a user's posts sorted by the number of likes, but only for a specific user. Which DynamoDB feature should be used?" The correct answer is a local secondary index with LikesCount as the alternate sort key. The exam will often include distractors like a GSI (which would allow cross-user queries but is not required here) or a Scan with filtering (which is less efficient).
In performance and cost questions, the exam presents a scenario where read or write capacity is high, and you must reduce costs. For instance: "A DynamoDB table is used to store order data. The current application uses a Scan operation to find all orders for a customer with a specific order status. The Scan consumes too many RCUs. What is the most cost-effective solution?" The answer involves creating an LSI on the order status attribute, so that queries can use the index instead of scanning. The question might also ask about the trade-offs of projecting all attributes versus only keys. You might need to calculate the RCU savings based on item size and number of items.
Troubleshooting-type questions present a problem: "Users report that queries using a local secondary index are returning inconsistent results. The index is defined correctly, and data is being written. What could be the cause?" The answer often points to eventually consistent reads being used when the application requires strong consistency, or that the query is not specifying the partition key. Another troubleshooting scenario: "An LSI query returns no items even though there are matching items in the base table. What is the most likely reason?" The answer could be that the index is not marked as KEYS_ONLY or that the projected attributes do not include the data being read, forcing a table fetch that fails because the item is too large or the index is not configured to include the necessary attributes. However, remember that LSIs always reflect the base table data, so the most likely reason is that the partition key in the query does not match any items in that partition, or the alternate sort key condition is incorrect.
Finally, questions may ask you to identify limitations. For example: "A DynamoDB table has a composite primary key (CustomerID, OrderDate). You want to add an LSI on OrderTotal. Which of the following is a constraint?" The correct answer would be that the alternate sort key must be a scalar attribute (string, number, or binary), and that you cannot have more than five LSIs per table. Understanding these constraints helps you avoid invalid design choices on exams.
Practise Local secondary index Questions
Test your understanding with exam-style practice questions.
Example Scenario
You are building a customer support ticket system for a company. Each ticket has a unique TicketID, but the primary access pattern is based on the customer who opened the ticket. You decide to use a DynamoDB table with CustomerID as the partition key and TicketCreationDate as the sort key. This allows you to easily retrieve all tickets for a specific customer in chronological order.
Now, your support team needs a new feature: they want to see all tickets for a specific customer, but sorted by ticket priority (high, medium, low) instead of by date. Without an index, the application would have to retrieve all tickets for that customer (using the partition key) and then sort them in memory by priority. For a customer with thousands of tickets, this would be slow and could cause timeouts.
You decide to create a local secondary index on the same table. You specify CustomerID as the partition key (same as the base table) and Priority as the alternate sort key. You choose to project only the key attributes (KEYS_ONLY) to save storage, knowing that you can fetch additional attributes from the base table if needed. Now, when the support team wants to see tickets for customer X sorted by priority, the application sends a Query request to the LSI with CustomerID = X and a ScanIndexForward parameter set to True (for ascending order). Because the index is already sorted by priority, the database returns the items in the correct order almost instantly.
This scenario demonstrates the core value of an LSI: it provides an alternative sort order without requiring a separate table or expensive application-side sorting. However, note that this index cannot help you find all high-priority tickets across all customers. For that, you would need a global secondary index. The LSI is tightly coupled to a single partition. So, you must carefully analyze your access patterns to decide whether an LSI or a GSI is appropriate. In this case, since the query is always scoped to a single customer (single partition), the LSI is the perfect solution.
Common Mistakes
Assuming a local secondary index can be queried without specifying a partition key.
A local secondary index has the same partition key as the base table. You must provide the partition key value in the Query operation because the index is organized by partition. Without it, the database cannot determine which physical partition to search.
Always include the partition key in your Query request to an LSI. If you need to scan across partitions, consider a global secondary index or a table Scan.
Using a local secondary index to query across multiple partition key values (e.g., all items with a certain attribute regardless of customer).
An LSI only stores data within the same partition as the base table. It cannot provide an efficient way to find items with the same alternate sort key across different partition keys. That would require a full scan of every partition, which defeats the purpose of an index.
If you need to query across partitions, create a global secondary index (GSI) on that attribute. A GSI can be queried without specifying the base partition key, allowing cross-partition lookups.
Believing that a local secondary index can have a different partition key than the base table.
By definition, a local secondary index shares the same partition key as the base table. Only the sort key differs. If you want a different partition key, you are describing a global secondary index.
Verify that any index with the same partition key is classified as local; otherwise it is a global index. Always check the documentation to avoid confusion.
Thinking that strongly consistent reads are available on all indexes, including GSIs.
Only local secondary indexes support strongly consistent reads. Global secondary indexes only support eventually consistent reads. This is a fundamental design difference.
If your application requires immediate consistency after a write for queries on that index, use an LSI. If you must use a GSI, design your application to tolerate eventual consistency.
Exam Trap — Don't Get Fooled
{"trap":"A question states: 'You need to query items in a DynamoDB table by an attribute that is not the primary key, and the query should be strongly consistent. What do you use?' The answer choices include Local Secondary Index and Global Secondary Index.
Many learners pick Global Secondary Index because they think 'global' implies stronger capabilities.","why_learners_choose_it":"Learners often associate the word 'global' with being more powerful or better in every way. They overlook the fact that GSIs do not support strongly consistent reads, while LSIs do.
The trap exploits the misconception that a more comprehensive-sounding index (global) must be superior.","how_to_avoid_it":"Memorize the consistency capabilities: LSI supports both eventual and strong consistency; GSI supports only eventual consistency. Also recall that LSIs are limited to a single partition, while GSIs span partitions.
So for a single-partition query needing strong consistency, the only correct answer is LSI. Always check the consistency requirement in the question."
Step-by-Step Breakdown
Identify the access pattern
Determine that you need to query items within a single partition (same partition key) but sorted or filtered by a different attribute. For example, you need to retrieve all orders for customer X sorted by order total.
Define the alternate sort key
Choose the attribute from the base table that you want to use as the sort key in the index. This attribute must be of a scalar type (string, number, or binary). For the order total example, you would select the 'order_total' attribute.
Create the local secondary index
During table creation or via an update operation, define the LSI with the same partition key as the base table and the chosen alternate sort key. Also specify the attributes to project (e.g., KEYS_ONLY, ALL, or INCLUDE). The database will build and maintain the index automatically.
Write data operations
Every time you insert, update, or delete an item in the base table, the database engine synchronously updates the LSI. This ensures that the index is always consistent with the base table data. For large partitions, this can consume additional write capacity.
Query the index
To use the LSI, perform a Query operation specifying the index name, the partition key value (same as base table), an optional condition on the alternate sort key, and the scan direction (forward or reverse). The database returns items from the index, sorted by the alternate sort key, without scanning the entire base table partition.
Practical Mini-Lesson
In practice, using a local secondary index requires careful planning around your access patterns and resource constraints. The most critical decision is choosing which attribute to use as the alternate sort key. You must ensure that this attribute is present in every item that might be queried using the index, because if an item does not have the attribute, it will not appear in the index. This means that sparse data can cause your index to be incomplete, leading to missing results. For example, if you create an LSI on 'discount_percentage' but only a few orders have discounts, queries on that index will only return those few items. This might be acceptable, but you must be aware of it.
Another practical consideration is the projection of attributes. If you choose to project only keys (KEYS_ONLY), the index is small and cheap, but when you query it, you will need to fetch additional attributes from the base table by performing a separate GetItem for each item. This can increase latency and read costs. On the other hand, projecting all attributes (ALL) makes the LSI larger and more expensive to store, but queries become faster and cheaper because the data is already in the index. There is a trade-off between storage cost and read performance. Professionals often use INCLUDE to project a specific set of frequently accessed attributes, balancing performance and cost.
What can go wrong? One common issue is hitting the 10 GB per partition key limit. Remember that the base table and all LSIs count toward this limit for each partition key. If a single customer accumulates more than 10 GB of order data (including index copies), future writes will fail with a 'Partition key size limit exceeded' error. To avoid this, you might need to redesign your partition key (e.g., use a composite partition key like CustomerID_Year) or use a GSI instead. Another issue is that LSIs cannot be added to a table that already has a primary key defined as a simple primary key (partition key only). LSIs require a composite primary key (partition key and sort key) on the base table. If your table has only a partition key, you cannot create an LSI.
Finally, monitoring is essential. Use CloudWatch metrics to track the number of throttled write events for the table and its indexes. If writes are being throttled because of the additional write capacity required by LSIs, you may need to increase the write capacity units or reconsider your indexing strategy. LSIs are a powerful but nuanced tool. They can dramatically improve query performance but come with constraints that force you to think deeply about your data model. Professionals should always prototype and test with realistic data sizes before deploying to production.
Memory Tip
LSI: Same Partition, Different Sort, Strong Consistency.
Covered in These Exams
Current Exam Context
Current exam versions that test this topic — use these objectives when studying.
DVA-C02DVA-C02 →220-1101CompTIA A+ Core 1 →Related Glossary Terms
5G is the fifth generation of cellular network technology, designed to deliver faster speeds, lower latency, and support for many more connected devices than previous generations.
802.1Q is the networking standard that allows multiple virtual LANs (VLANs) to share a single physical network link by tagging Ethernet frames with VLAN identification information.
AAA (Authentication, Authorization, and Accounting) is a security framework that controls who can access a network, what they are allowed to do, and tracks what they did.
Frequently Asked Questions
Can I create a local secondary index on a table that has only a partition key and no sort key?
No, a local secondary index requires the base table to have a composite primary key (partition key and sort key). If your table has only a partition key, you cannot create an LSI. You would need to modify the table's primary key, which often requires creating a new table and migrating data.
How many local secondary indexes can I have per DynamoDB table?
You can have up to five local secondary indexes per DynamoDB table. This is a hard limit set by AWS. If you need more than five alternate sort orders, consider using global secondary indexes or redesigning your data model.
Does a local secondary index support strongly consistent reads?
Yes, local secondary indexes support both strongly consistent reads and eventually consistent reads. This is one of the key differences from global secondary indexes, which only support eventually consistent reads.
What happens to a local secondary index when I delete an item from the base table?
When you delete an item from the base table, the database automatically removes the corresponding entry from all local secondary indexes. This ensures that the index remains consistent with the base table. The operation is immediate and synchronous.
Can I change the alternate sort key of a local secondary index after the index is created?
No, you cannot modify the schema of an existing local secondary index. To change the alternate sort key, you must delete the index and create a new one with the desired key. You can use the AWS Management Console, CLI, or SDK to perform this operation, but it may require some downtime or data migration depending on your application's tolerance.
Does a local secondary index increase write costs?
Yes, because every write to the base table (insert, update, delete) also triggers a write to each LSI. This consumes additional write capacity units (WCUs) proportional to the size of the index entry. The exact cost depends on the number of indexes and the projection type. You should account for this when estimating costs.
Summary
A local secondary index (LSI) is a database indexing structure that allows efficient queries on an alternate sort key within a single partition. It shares the same partition key as the base table but uses a different sort key, enabling you to retrieve items sorted or filtered by that alternate attribute without scanning the entire partition. This concept is critical in NoSQL databases like Amazon DynamoDB, where read and write costs are tied to data access patterns.
The LSI is a powerful tool for optimizing specific query patterns, but it comes with constraints: it requires a composite primary key, it is limited to five per table, and the total data size per partition key (including all LSIs) cannot exceed 10 GB. It supports strongly consistent reads, which makes it suitable for applications requiring immediate data consistency after writes. However, it cannot be used for cross-partition queries, and the alternate sort key attribute must be present in the items you want indexed.
For certification exams, mastering LSIs means understanding their difference from global secondary indexes, knowing the consistency models, and being able to apply them in scenario-based questions. Common mistakes include trying to query an LSI without a partition key or assuming it can be used for global lookups. Remember the memory tip: 'Same Partition, Different Sort, Strong Consistency.' By keeping these principles in mind, you will be able to design efficient data models and answer exam questions with confidence.