Vitess Vindexes: Routing Queries Without Knowing Shards
Vitess Vindexes map logical keys to physical shards, letting applications query sharded MySQL without knowing shard locations. Hash and range types serve different workloads, but changing Vindexes requires costly resharding.
11 Jun 2026, 23:10 UTC

The Problem: Application Logic Shouldn't Care About Shards
When you shard a MySQL database across multiple physical instances, your application suddenly needs to know which shard holds which data. That breaks the abstraction layer between your business logic and storage. Vitess solves this with Vindexes—virtual indexes that map logical keys to physical shards without requiring application changes.
How Vindexes Decouple Logic from Storage
A Vindex sits between your SQL queries and the actual shards. When you insert a row with primary key 12345, Vitess uses the Vindex to calculate which of the 8 shards should hold that data. Your application just sees a normal MySQL table; Vitess handles the routing transparently through vtgate, the query router component.
Two common types:
- Hash-based: Distributes keys uniformly across shards. Good for write-heavy workloads where you need even distribution.
- Range-based: Keeps contiguous key ranges together. Better for analytical queries that scan ranges of values.
Setting Up a Hash Vindex: A Worked Example
Let's say you have a users table and want to shard by user ID. First, define the Vindex in your schema:
CREATE TABLE users (
id INT NOT NULL,
email VARCHAR(255),
name VARCHAR(100),
PRIMARY KEY (id)
) VTIDENTITY (id) HASH_INDEX (id, 8); -- 8 shards
Then verify routing works correctly:
- Run
vtctl LookupKeyspaceID users 12345to see which shard Vitess would route to for user ID 12345 - Insert a test user and confirm it lands in the expected shard via
vtctl ShowShard - Query that user by primary key—Vitess should route directly without scatter-gather
Important: Run these commands on your Vitess test cluster where you have vtctl access and can inspect shard distribution.
Trade-offs and Common Pitfalls
Resharding cost: Changing a Vindex definition requires moving data between shards—a full resharding operation that can take hours or days depending on your dataset size. Plan carefully before committing to a Vindex type.
Hot shards: If your key distribution isn't uniform, some shards become overloaded while others sit idle. Monitor shard key counts and consider using a hash function with good distribution properties.
Scatter queries: Queries without the Vindex key (e.g., SELECT * FROM users WHERE email = '[contact removed]') trigger cross-shard queries, significantly increasing latency. Always include your Vindex column in WHERE clauses for optimal performance.
Actionable Next Steps
To validate Vindex behavior in your environment:
- Start with a small test cluster rather than production data
- Use
GetShardorGetVindexviavtctlto observe key-to-shard mapping - Benchmark routed queries against scatter queries to measure the performance difference
The key insight: a well-chosen Vindex lets Vitess handle sharding complexity so your application can stay blissfully unaware of which physical database holds its data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.