Cassandra
SASI index
Database indexing
NoSQL
Data retrieval

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.

Practice system design

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:

  1. Local to Node: Each node manages its index for the data it stores with no coordination between different nodes.
  2. Read Performance: A query involving secondary indexes can lead to performance overhead since it might require querying multiple nodes.
  3. Write Performance: Additional impact on write performance due to the maintenance of extra data structures for indexing.
  4. 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:

  1. 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.
  2. Efficient Search:
    • Allows complex queries such as LIKE, CONTAINS, and range expressions due to enhanced indexing strategies.
  3. Column-Level Indexing:
    • Provides the ability to manage index creation on specific columns, reducing unnecessary resource overhead.
  4. 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.
  5. Write Performance:
    • Typically more efficient when compared to traditional secondary indexes, as it doesn't require the same level of cross-node querying.
  6. Indexing Methods:
    • Supports different indexing strategies like PREFIX and CONTAINS to optimize text searches.

SASI vs. Traditional Secondary Indexes

Here’s a comparison table summarizing the key differences between SASI and traditional secondary indexes:

Feature / CapabilitySecondary IndexSASI Index
Local to NodeYesYes
Data Types SupportedLimited (primarily for equality checks)Wide (text, numerical, time)
Read EfficiencyAffected by simultaneous multi-node queriesTypically requires less cross-node querying due to local storage & processing
Write ImpactHigher due to maintaining additional data structuresLess impact from overhead due to more efficient data structure management
Search CapabilitiesLimited (equality checks)Extended (wildcards, ranges, and full-text)
CustomizabilityMinimalHigh (supports custom analyzers)
Ideal Usage ScenariosSmall clusters, well-defined queries on known dataText-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:

cql
1CREATE CUSTOM INDEX sasi_index_name ON my_table (text_column)
2USING 'org.apache.cassandra.index.sasi.SASIIndex'
3WITH OPTIONS = {
4  'analyzed': 'true',
5  'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer',
6  'mode': 'CONTAINS'
7};

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.