SASI index in Cassandra and How it differs from normal indexing
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction to Indexing in Cassandra
Apache Cassandra is a distributed NoSQL database system designed to handle large amounts of data across many commodity servers, ensuring high availability with no single point of failure. One common requirement in any database is efficient data retrieval, for which indexes are used. Cassandra provides several indexing mechanisms, including the primary index, secondary indexes, and the Storage Attached Index (SASI).
Understanding Cassandra's Indexing Mechanisms
Secondary Index
A secondary index allows for querying or filtering data by columns other than the primary key. Secondary indexes in Cassandra are stored on each node and do not significantly affect write performance. However, they are limited in functionality and efficiency in various larger-scale use cases.
Key Characteristics of Secondary Index:
- Local to Node: Each node manages its index for the data it stores with no coordination between different nodes.
- Read Performance: A query involving secondary indexes can lead to performance overhead since it might require querying multiple nodes.
- Write Performance: Additional impact on write performance due to the maintenance of extra data structures for indexing.
- Use Cases: Suitable for small clusters or specific tables where query efficiency outweighs potential latency issues.
SASI (Storage Attached Index)
SASI indexes are a more advanced, experimental indexing feature introduced to overcome the limitations of traditional secondary indexing methods. SASI aims to enhance performance and provide more flexible query capabilities.
Key Features of SASI:
- Wide Range of Data Types:
- Supports text, numeric, and time data types.
- Provides enhanced full-text search capabilities and efficient prefix, suffix, and substring queries for text data.
- Efficient Search:
- Allows complex queries such as
LIKE,CONTAINS, and range expressions due to enhanced indexing strategies.
- Column-Level Indexing:
- Provides the ability to manage index creation on specific columns, reducing unnecessary resource overhead.
- Configurable Analyzers:
- SASI lets users define custom analyzers to fine-tune text processing based on language or application requirements, giving it an edge for multilingual data handling.
- Write Performance:
- Typically more efficient when compared to traditional secondary indexes, as it doesn't require the same level of cross-node querying.
- Indexing Methods:
- Supports different indexing strategies like
PREFIXandCONTAINSto optimize text searches.
SASI vs. Traditional Secondary Indexes
Here’s a comparison table summarizing the key differences between SASI and traditional secondary indexes:
| Feature / Capability | Secondary Index | SASI Index |
| Local to Node | Yes | Yes |
| Data Types Supported | Limited (primarily for equality checks) | Wide (text, numerical, time) |
| Read Efficiency | Affected by simultaneous multi-node queries | Typically requires less cross-node querying due to local storage & processing |
| Write Impact | Higher due to maintaining additional data structures | Less impact from overhead due to more efficient data structure management |
| Search Capabilities | Limited (equality checks) | Extended (wildcards, ranges, and full-text) |
| Customizability | Minimal | High (supports custom analyzers) |
| Ideal Usage Scenarios | Small clusters, well-defined queries on known data | Text-heavy datasets, complex text-based search queries |
Additional Details and Considerations
Performance Considerations
While SASI can greatly improve query performance, especially for text-heavy applications, they do not come without some trade-offs. They can consume more disk space and memory due to their complex data structures. Care should be taken to avoid creating unnecessary indexes and to ensure that their use cases justify their overhead.
Syntax Example of SASI Creation
To illustrate a simple setup, here’s how you can create a SASI index on a text column:
This CQL statement creates a SASI index on the text_column allowing for text analysis and partial matches using the CONTAINS mode.
When to Use SASI
- When dealing with requirements for full-text search capabilities, complex queries, or needing specific column indexing.
- In use cases where performance on read operations is critical, and resources can be allocated to maintain the indexes.
- When requiring complex filter conditions or multilingual text processing.
Conclusion
SASI indexes in Cassandra provide advanced, flexible, and efficient querying capabilities that are not attainable with traditional secondary indexes. By leveraging SASI, applications can achieve superior performance and functionality, especially in scenarios requiring intricate text-based querying. However, given their experimental status, it's advisable to test thoroughly in a staging environment before deployment to ensure compatibility and performance benefits.
Related reading
- Save null Values in Cassandra using DataStax Spark Connector
- Save PHP array to MySQL?
- Save PL/pgSQL output from PostgreSQL to a CSV file
- Save Subset of MongoDB Collection to Another Collection
- Saving an Object Data persistence
- Scaling TerminusDB to multiple servers
- Scaling the System and its Database for 10k request handling. What's right Sharding or Microservice going for Distributed database
- Schedule clickhouse table optimization, ReplacingMergeTree

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.