|
| 1 | +--- |
| 2 | +Title: Index management best practices for Redis Query Engine |
| 3 | +alwaysopen: false |
| 4 | +categories: |
| 5 | +- docs |
| 6 | +- develop |
| 7 | +- stack |
| 8 | +- oss |
| 9 | +- kubernetes |
| 10 | +- clients |
| 11 | +linkTitle: RQE index management |
| 12 | +weight: 3 |
| 13 | +--- |
| 14 | +## Introduction to managing Redis Query Engine indexes |
| 15 | + |
| 16 | +The Redis Query Engine (RQE) is a powerful tool for executing complex search and query operations on structured, semi-structured, and unstructured data. Indexes are the backbone of this functionality, enabling fast and efficient data retrieval. |
| 17 | +Proper management of these indexes is essential for optimal performance, scalability, and resource utilization. |
| 18 | + |
| 19 | +This guide outlines best practices for managing RQE indexes throughout their lifecycle. It provides recommendations on: |
| 20 | + |
| 21 | +- Planning and creating indexes to suit your query patterns. |
| 22 | +- Using index aliasing to manage schema updates and minimize downtime. |
| 23 | +- Monitoring and verifying index population to ensure query readiness. |
| 24 | +- Optimizing performance through query profiling and memory management. |
| 25 | +- Maintaining and scaling indexes in both standalone and clustered Redis environments. |
| 26 | +- Versioning, testing, and automating index management. |
| 27 | + |
| 28 | +## Why index management matters |
| 29 | + |
| 30 | +Indexes directly impact query speed and resource consumption. |
| 31 | +Poorly managed indexes can lead to increased memory usage, slower query times, and challenges in maintaining data consistency. |
| 32 | +By following the strategies outlined in this guide, you can: |
| 33 | + |
| 34 | +- Reduce operational overhead. |
| 35 | +- Improve application performance. |
| 36 | +- Ensure smooth transitions during schema changes. |
| 37 | +- Scale efficiently with your growing datasets. |
| 38 | + |
| 39 | +## Plan your indexes strategically |
| 40 | + |
| 41 | +Planning your indexes strategically requires understanding your application’s query patterns and tailoring indexes to match. |
| 42 | +Begin by identifying the types of searches your application performs—such as full-text search, range queries, or geospatial lookups—and the fields involved. |
| 43 | +Categorize fields based on their purpose: searchable fields (e.g., [`TEXT`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#text-fields" >}}) for full-text searches), filterable fields (e.g., [`TAG`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#tag-fields" >}}) for exact match searches), and sortable fields (e.g., [`NUMERIC`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#numeric-fields" >}}) for range queries or sorting). |
| 44 | +Match field types to their intended use and avoid indexing fields that are rarely queried to conserve resources. Here's the list of index types: |
| 45 | + |
| 46 | +- [`TEXT`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#text-fields" >}}): use `TEXT` for free-text searches and set weights if some fields are more important. |
| 47 | +- [`TAG`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#tag-fields" >}}): use `TAG` for categorical data (e.g., product categories) that benefit from exact matching and filtering. |
| 48 | +- [`NUMERIC`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#numeric-fields" >}}): use `NUMERIC` for numeric ranges (e.g., prices, timestamps). |
| 49 | +- [`GEO`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#geo-fields" >}}): use `GEO` for geospatial coordinates (e.g., latitude/longitude). |
| 50 | +- [`GEOSHAPE`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#geoshape-fields" >}}): use `GEOSHAPE` to represent locations as points, but also to define shapes and query the interactions between points and shapes (e.g., to find all points that are contained within an enclosing shape). |
| 51 | +- [`VECTOR`]({{< relref "/develop/interact/search-and-query/basic-constructs/field-and-type-options#vector-fields" >}}): use `VECTOR` for high-dimensional similarity searches. |
| 52 | + |
| 53 | +See [these pages]({{< relref "/develop/interact/search-and-query/query" >}}) for discussions and examples on how best to use these index types. |
| 54 | + |
| 55 | +Next, simulate queries on a sample dataset to identify potential bottlenecks. |
| 56 | +Use tools like [`FT.PROFILE`]({{< baseurl >}}/commands/ft.profile) to analyze query execution and refine your schema if needed. |
| 57 | +For example, assign weights to `TEXT` fields for prioritizing results or use the `PREFIX` option of [`FT.CREATE`]({{< baseurl >}}/commands/ft.create) to limit indexing to specific key patterns. Note that you can use multiple `PREFIX` clauses when you create an index (see [below](#index-creation)) |
| 58 | +After creating the index, validate its performance with real queries and monitor usage with the available tools: |
| 59 | + |
| 60 | +- [`FT.EXPLAIN`]({{< baseurl >}}/commands/ft.explain) and [`FT.EXPLAINCLI`]({{< baseurl >}}/commands/ft.explaincli) allow you to see how Redis Query Engine parses a given search query. `FT.EXPLAIN` returns a structured breakdown of the query execution plan, while `FT.EXPLAINCLI` presents a more readable, tree-like format for easier interpretation. These commands are useful for diagnosing query structure and ensuring it aligns with the intended logic. |
| 61 | +- [`FT.INFO`]({{< baseurl >}}/commands/ft.info) provides detailed statistics about an index, including the number of indexed documents, memory usage, and configuration settings. It helps in monitoring index growth, assessing memory consumption, and verifying index structure to detect potential inefficiencies. |
| 62 | +- [`FT.PROFILE`]({{< baseurl >}}/commands/ft.profile) runs a query while capturing execution details, which helps to reveal query performance bottlenecks. It provides insights into processing time, key accesses, and filter application, making it a crucial tool for fine-tuning complex queries and optimizing search efficiency. |
| 63 | + |
| 64 | +Avoid over-indexing. Indexing every field increases memory usage and can slow down updates. |
| 65 | +Only index the fields that are essential for your planned queries. |
| 66 | + |
| 67 | +## Index creation {#index-creation} |
| 68 | + - Use the [`FT.CREATE`]({{< baseurl >}}/commands/ft.create) command to define an index schema. |
| 69 | + - Assign weights to `TEXT` fields to prioritize certain fields in full-text search results. |
| 70 | + - Use the `PREFIX` option to restrict indexing to keys with specific patterns. |
| 71 | + Using multiple PREFIX clauses when creating an index allows you to index multiple key patterns under a single index. This is useful in several scenarios: |
| 72 | + - If your Redis database stores different types of entities under distinct key prefixes (e.g., `user:123`, `order:456`), a single index can cover both by specifying multiple prefixes. For example: |
| 73 | + |
| 74 | + ```bash |
| 75 | + FT.CREATE my_index ON HASH PREFIX 2 "user:" "order:" SCHEMA name TEXT age NUMERIC status TAG |
| 76 | + ``` |
| 77 | + |
| 78 | + This approach enables searching across multiple entity types without needing separate indexes. |
| 79 | + |
| 80 | + - Instead of querying multiple indexes separately, you can search across related data structures using a single query. This is particularly helpful when data structures share common fields, such as searching both customer and vendor records under a unified contacts index. |
| 81 | + |
| 82 | + - Maintaining multiple indexes for similar data types can be inefficient in terms of memory and query performance. By consolidating data under one index with multiple prefixes, you reduce overhead while still allowing for distinct key organization. |
| 83 | + |
| 84 | + - If your data model evolves and new key patterns are introduced, using multiple `PREFIX` clauses from the start ensures future compatibility without requiring a full reindexing. |
| 85 | + - Data loading strategy: load data into Redis before creating an index when working with large datasets. Use the `ON HASH` or `ON JSON` options to match the data structure. |
| 86 | + |
| 87 | +## Index aliasing |
| 88 | + |
| 89 | +Index aliases act as abstracted names for the underlying indexes, enabling applications to reference the alias instead of the actual index name. This approach simplifies schema updates and index management. |
| 90 | + |
| 91 | +There are several use cases for index aliasing, including: |
| 92 | + |
| 93 | +- Schema updates: when updating an index schema, create a new index and associate the same alias with it. This allows a seamless transition without requiring application-level changes. |
| 94 | +- Version control: use aliases to manage different versions of an index. For example, assign the alias products to `products_v1` initially and later to `products_v2` when the schema evolves. |
| 95 | +- Testing and rollback: assign an alias to a test index during staged deployments. If issues arise, quickly switch the alias back to the stable index. |
| 96 | + |
| 97 | +Best practices for aliasing: |
| 98 | + |
| 99 | +- Always create an alias for your indexes during initial setup, even if you don’t anticipate immediate schema changes. |
| 100 | +- Use clear and descriptive alias names to avoid confusion (e.g., `users_current` or `orders_live`). |
| 101 | +- Make sure that an alias points to only one index at a time to maintain predictable query results. |
| 102 | +- Use aliases to provide tenant-specific access. For example, assign tenant-specific aliases like `tenant1_products` and `tenant2_products` to different indexes for isolated query performance. |
| 103 | + |
| 104 | +Tools for managing aliases: |
| 105 | + |
| 106 | +- Assign an alias: [`FT.ALIASADD`]({{< baseurl >}}/commands/ft.aliasadd) `my_alias my_index` |
| 107 | +- Update an alias: [`FT.ALIASUPDATE`]({{< baseurl >}}/commands/ft.aliasupdate) `my_alias new_index` |
| 108 | +- Remove an alias: [`FT.ALIASDEL`]({{< baseurl >}}/commands/ft.aliasdel) `my_alias` |
| 109 | + |
| 110 | +Monitoring and troubleshooting aliases: |
| 111 | + |
| 112 | +- Use the `FT.INFO` command to check which aliases are associated with an index. |
| 113 | +- Make sure your aliases always points to valid indexes and are correctly updated during schema changes. |
| 114 | + |
| 115 | +## Monitor index population |
| 116 | + |
| 117 | +- Use the `FT.INFO` command to monitor the `num_docs` and `indexing` fields, to check that all expected documents are indexed. |
| 118 | + ```bash |
| 119 | + FT.INFO my_new_index |
| 120 | + ``` |
| 121 | +- Validate data with sample queries to ensure proper indexing: |
| 122 | + ```bash |
| 123 | + FT.SEARCH my_new_index "*" |
| 124 | + ``` |
| 125 | +- Use `FT.PROFILE` to analyze query plans and validate performance: |
| 126 | + |
| 127 | + ```bash |
| 128 | + FT.PROFILE my_new_index SEARCH QUERY "your_query" |
| 129 | + ``` |
| 130 | + - Implement scripts to periodically verify document counts and query results. For example, in Python: |
| 131 | + |
| 132 | + ```python |
| 133 | + import re |
| 134 | + def check_index_readiness(index_name, expected_docs): |
| 135 | + r = redis.StrictRedis(host='localhost', port=6379, decode_responses=True) |
| 136 | + info = r.execute_command('FT.INFO', index_name) |
| 137 | + num_docs = int(info[info.index('num_docs') + 1]) |
| 138 | + return num_docs >= expected_d |
| 139 | + if check_index_readiness('my_new_index', 100000): |
| 140 | + print("Index is fully populated!") |
| 141 | + else: |
| 142 | + print("Index is still populating...") |
| 143 | + ``` |
| 144 | +
|
| 145 | +## Monitoring index performance |
| 146 | +
|
| 147 | +- Use the `FT.PROFILE` command to analyze query performance and identify bottlenecks. |
| 148 | +- Regularly monitor memory usage with the [`INFO`]({{< baseurl >}}/commands/info) `memory` and `FT.INFO` commands to detect growth patterns and optimize resource allocation. |
| 149 | +
|
| 150 | +## Index maintenance |
| 151 | +
|
| 152 | +- If schema changes are required, create a new index with the updated schema and reassign the alias once the index is ready. |
| 153 | +- Use [Redis key expiration]({{< relref "/develop/use/keyspace#key-expiration" >}}) to automatically remove outdated records and keep indexes lean. |
| 154 | +
|
| 155 | +### [`FT.ALTER`]({{< baseurl >}}/commands/ft.alter) vs. aliasing |
| 156 | +
|
| 157 | +Use `FT.ALTER` when you need to add new fields to an existing index without rebuilding it, minimizing downtime and resource usage. However, `FT.ALTER` cannot remove or modify existing fields, limiting its flexibility. |
| 158 | +
|
| 159 | +Use index aliasing when making schema changes that require reindexing, such as modifying field types or removing fields. In this case, create a new index with the updated schema, populate it, and then use `FT.ALIASUPDATE` to seamlessly switch queries to the new index without disrupting application functionality. |
| 160 | +
|
| 161 | +## Scaling and high availability |
| 162 | +
|
| 163 | +- In a clustered Redis setup, make sure indexes are designed with key distribution in mind to prevent query inefficiencies. |
| 164 | +- Test how indexes behave under replica promotion to ensure consistent query behavior across nodes. |
| 165 | +
|
| 166 | +## Versioning and testing |
| 167 | +
|
| 168 | +- When changing schemas, create a new version of the index alongside the old one and migrate data progressively. |
| 169 | +- Test index changes in a staging environment before deploying them to production. |
| 170 | +
|
| 171 | +## Cleaning up |
| 172 | +
|
| 173 | +- Use the [`FT.DROPINDEX`]({{< baseurl >}}/commands/ft.dropindex) command to remove unused indexes and free up memory. Be cautious with the `DD` (Delete Documents) flag to avoid unintended data deletion. |
| 174 | +- Make sure no keys remain that were previously associated with dropped indexes if the data is no longer relevant. |
| 175 | +
|
| 176 | +## Documentation and automation |
| 177 | +
|
| 178 | +- Document your index configurations to facilitate future maintenance. |
| 179 | +- Use scripts or orchestration tools to automate index creation, monitoring, and cleanup. |
0 commit comments