Skip to the content.
Understanding Data Sharding and Indexing | AI Systems Design From Scratch

Connect with Amin Boulouma Official

🏠 Documentation Hub 📝 Engineering Blog 💻 GitHub Repository

Scalable Indexing: Sharding and Data Distribution

Amin Boulouma, Software Engineer

In large-scale systems, storing all data in a single location creates a bottleneck for both storage capacity and query performance. Sharding (or horizontal partitioning) is the architectural solution, where a large dataset is broken into smaller, more manageable segments called Shards.

The Architecture of a Scalable Index

A sharded index manages two primary concerns: Deterministic Routing and Distributed Aggregation.

1. Deterministic Routing

When a new document arrives, the system must decide which shard it belongs to. We use a Routing Key (usually the document ID) and a modulo operation to ensure the placement is consistent:

def _get_shard_route(self, document_id: int) -> Shard:
    # Ensures the same document ID always hits the same shard
    return self.shards[document_id % len(self.shards)]

This ensures that when you need to retrieve that specific document later, you know exactly which shard to query.

2. Distributed Aggregation

Searching or aggregating across shards is a “scatter-gather” operation. The ScalableIndex collects data from all individual partitions (_all_documents) and merges them into a single response.

Pattern Implementation: The ScalableIndex

The ScalableIndex serves as the orchestrator. It enforces a Schema (the mapping dictionary) so that only valid fields are stored, ensuring data integrity within the shards.

Performance Note: $O(N)$ vs Indexing

In this implementation, the search and aggregate_counts methods perform a linear scan ($O(N)$) across all documents. While functional for small sets, production-grade engines (like Elasticsearch) optimize this by creating secondary structures (Inverted Indexes) that allow for $O(1)$ or $O(\log N)$ lookups.

Why Sharding Scales

Best Practices

By decomposing your data into shards, you transform a monolithic storage problem into a distributed management system, allowing your application to scale with your data growth.

Connect with Amin Boulouma Official