Aerospike
Secondary Index
Node Operations
Database Management
Record Retrieval

Aerospike Read records from a particular node using secondary index

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Aerospike is a popular NoSQL database known for its scalability and performance, especially in situations requiring high throughput and low latency. Among its many features, the ability to read records based on a secondary index is particularly powerful for efficiently querying data. However, reading records from a specific node using a secondary index can be a bit tricky and requires understanding Aerospike’s architecture and query mechanisms.

Understanding Aerospike Architecture and Secondary Index

Aerospike’s architecture is distributed, consisting of several nodes that form a cluster. Data in Aerospike is partitioned across all nodes in the cluster based on a partition key derived from the primary key of each record. However, secondary indexes in Aerospike are global and not partition-specific. That is, a secondary index spans all nodes and all records, allowing queries on attributes other than the primary key.

Secondary indexes in Aerospike are typically used for queries involving non-primary key conditions. They improve the speed of searches across multiple nodes. Once a secondary index is created, it allows clients to efficiently query records based on attributes other than the primary key.

Use Case: Reading Records from a Specific Node

One of the scenarios where it might be necessary to read records from a specific node is for performance tuning or in cases of node-specific data analysis or maintenance. Here are detailed steps and considerations on how it can be accomplished:

  1. Identify Node ID: First, identify the node from which you wish to read data. Each node in an Aerospike cluster can be uniquely identified using a node ID.
  2. Record Distribution: Understand that, normally, data is evenly distributed across all nodes based on partitioning. Reading from a specific node ideally involves identifying the partitions hosted on that node and then querying records accordingly.
  3. Querying with Preferences: In Aerospike, the prefer read policy can be used in client APIs to direct the read to go specifically to one node. Use the setSendKey method to send the user key with the record so that it is available on the server side during the query execution.
  4. Secondary Index Query: When creating a secondary index query, normally, one would not specify a node; the query would run across all nodes. To limit this to a specific node, combine the secondary index search with the node preference configuration in the client API, ensuring that the query is directed to your target node.

Practical Example

Suppose you have a customer database where each record has attributes like CustomerID (Primary Key), Lastname, Age, and so on. You’ve created a secondary index on Age.

When you want to retrieve all records of customers aged 25 from a specific node, configure the client application like this:

python
1import aerospike
2from aerospike import predicates as p
3
4# Configuration for Aerospike
5config = {
6  'hosts': [('127.0.0.1', 3000)]
7}
8
9# Create a client and connect it to the cluster
10client = aerospike.client(config).connect()
11
12# Identify the node (Node ID can be fetched from the cluster)
13node_to_query = 'BB9020011AC4202'
14
15# Create a query on namespace 'test' and set 'demo'
16query = client.query('test', 'demo')
17query.where(p.equals("Age", 25))
18
19# Set policy to prefer a specific node
20policy = {'total_timeout': 1000, 'prefer': 'follow', 'acceptable_latency': 5}
21
22# Setting to prefer reads from the specific node
23query.set_read_preference(policy, node_filter=node_to_query)
24
25# Execute the query
26records = query.results()
27
28# Close the connection
29client.close()

Summary Table

FeatureDetail
Secondary IndexGlobal index for non-primary key querying
Node-specific QueryingAchieved through prefer read policy and node identification
Use CasesPerformance tuning, node-specific data handling
Client API RequirementsSetting node preference in read/query policies

Conclusion

Querying records from a specific node using a secondary index in Aerospike involves understanding both the database's distribution architecture and client API capabilities. While not typical, there are practical scenarios where such precise control over data querying can be beneficial.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design