Interview: users search cars by color, but the table is sharded by car id

A filter on a non-key column across shards: local index (ask every shard), global index (one lookup, costlier writes), stale reads, and the slowest-shard…

More on Ticket interviews

Keep learning

Overview

ids 0–99 ids 100–199 ids 200–299 ids 300–399 ui-monospace, SFMono-Regular, Menlo, monospace Index A Index B terms a–m terms n–z Each shard looks in its own small index and answers. The app waits for the slowest shard, then merges the answers. New car 370 has an id in 300–399, so it belongs to shard 3. Shard 3 saves the row and updates its own color and make index. Done. 1 shard. The row and its index entries live on the same shard, so one local write keeps them in step. New car 370 belongs to shard 3, so the row is written there. All three updates commit together (a distributed transaction). Safe, but slow, and one failure holds up the others. The app says "done" now. The two index updates arrive a moment later. Fresh index, but one listing write now needs 3 shards to agree. Fast write, but for a moment a search for silver cars would miss car 370. The index is a little behind. A library with four rooms. Books are filed by call number, so red-cover books are spread over three of the rooms. A card catalog drawer lists all red books in one place. Card catalog red: 12 130 340 one place to look Books are filed by call number (the shard key). Red covers are spread over the rooms. No shared index Each shard keeps its own small one local index Index type What happens Add a car Search color ) : mode === 'global' ? ( Index update Same transaction A moment later search request index tells where the rows are write to data index update that arrives later Servers involved Grows with shards Stale-answer risk What to notice: a local index makes writes cheap and searches wide. A global index makes searches narrow and writes wide. One shard: no one else to wait for. Illustrative: each shard is slow for 1 search in 100. Local: chance a search is slow Global: asks about 2 shards Data you can store Local searches per second Number of shards What to notice: more shards store more data, but a local-index search is not faster. It is slower more often, and the whole cluster answers no more searches per second. Red cars sit in shards 0, 1 and 3 but not in shard 2, so the id does not say where they are. no red car Local index 1 shard Global, 2 indexed columns up to 3 shards Global, 5 indexed columns up to 6 shards One write touches 1 shard with a local index, up to 3 with a global index on two columns, and up to 6 with five indexed columns. Two index shards each send a long list of ids to one place that keeps only the ids in both lists. Index B says: red 1,000,000 ids Index A says: Ford 800,000 ids Keep ids in both 20,000 are left Illustrative sizes. Almost all of the traffic is lists that get thrown away. The database knows where listing 12 lives. It has no idea where the red cars live. global index write cost stale reads A library with four rooms filtering by color or make has no shard key, so it gets slower with every shard. Pick an approach and name its cost. Shards now / planned 4 / 40 (illustrative) Open one listing by id: 1 shard Filter by color or make no shard key in the query Reads : writes about 100 : 1 (illustrative) 40 shards. The filter page is the busiest page. Reads outnumber writes about 100 to 1. A new listing may show up a few seconds late. What do you ship? A global index updated after the write. The page says new listings can take a few seconds to appear. Searches read 1 index shard, not 40, and writes stay quick. The short delay is a deliberate, product-approved cost. Search speed Write cost Keep local indexes and ask all 40 shards Nothing to build, always fresh. But every search makes 40 requests and waits for the slowest. A global index updated in the same transaction as every listing write Always fresh, but every write needs several shards to agree. You pay that to protect seconds nobody asked for. Why can the database not just look in one place for the red cars? explain simply The id says which shard a car is in. The color says nothing. So the choice is who keeps the red list: each shard (local) or one shared index (global). names local and global plain words What changes when we grow from 4 shards to 40? what breaks if With local indexes, every search now makes 40 requests. The slowest shard sets the speed: 1% slow each → ~1 in 3 searches slow. More shards store more data, but answer no more searches per second. A global index search reads one index shard, whatever the shard count. slowest shard wins capacity does not grow A listing has 2 indexed columns. How many shards does one new listing touch? Local: 1 shard. Global: up to 3 (data + one per indexed value). With 5 indexed columns, global means up to 6. Changing a color touches both the old and new entries: one more. Committing all of them together is a distributed transaction: slower, more to fail. counts shards one index shard per value links to distributed transactions What else could you do instead of a global index? what would you do instead Shard by the main filter, or copy into a search engine. If every search is inside one dealer, shard by dealer: one shard per search. Avoid hand-made lookup tables: races and half-failed writes make them drift. key matches the query a copy can lag A seller posts a car, then searches for it. It is not there. Why? The index is updated later, so it has not heard about the car yet. Same family as replica lag: a read slightly behind a write. Fix in the product: “live in a few seconds”, or show own listings from the data shard. index lag compares to replica lag The filter is "red AND Ford". How does a global index answer that? staff curveball Short lists are fine. A million-id list over the network is the slow part. Options: search engine, global index only for selective columns, or one combined entry. Global indexes suit few conditions and short lists. multi-term AND long lists cross the network chooses by selectivity How do you search a sharded table by a column that is not the shard key? The problem The shard key finds a row. A color does not. Option 1: local Writes: 1 shard. Searches: every shard, slowest wins. Option 2: global Searches: 1 index shard. Writes: data + index shards. The catch My pick Read-heavy at 40 shards: global, updated later, flagged in the product. “Just add an index” Say which shape, local or global, and its cost. Local searches still visit every shard. Forgetting the write cost A global index adds a shard per indexed value. Forgetting the index can lag Decide what the product says about it. Named local and global indexes Slowest shard sets the speed Counted shards per global-index write Global index updated later can be stale Offered an alternative: shard key or search engine Shard by the filter e.g. dealer. One shard per search. Search engine copy Built for filters. Can lag. Hand-made lookup table Drifts from the real data. data shard 3 index shard POST car 370 (silver) row saved ✓ search color=silver silver → 290 no 370 yet silver → 290 · 370 update arrives

Users want to search cars by color, but the table is split by car id

Color filter gets slower with every shard added Color isn’t the shard key, so every shard is asked Global index, updated a moment later, lag shown in UI 1 · The story in plain words Each room checks its shelves Local index. Cheap to keep current, but you ask every room. One card catalog for red Global index. One place to look, but every new book updates it. Books are filed by call number (the shard key). Cover color is not.

The color and make filter is slow, and it gets slower with every shard we add

Marketplace team Listings are sharded by id. Opening one listing is fast. The filter page has no id, so it cannot pick a shard.

Move the switches and watch who gets asked

Try both index types. Search first, then add a car. Count the servers. Now grow the cluster. The slider shows why asking every shard gets worse.

Four ways to answer a filter

Cheap writes Narrow searches Always fresh Any filter works Local index Global, same transaction Global, updated later Shard key in the query ✓ strength · ~ partly · ✕ the cost you pay pick when Few shards, rare filters, or heavy writes. Reads dominate and new rows must show at once. Reads dominate and a few seconds’ delay is fine. One access path dominates, e.g. per dealer.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.