← Building blocks

Shard by region

Scaling & loadstaff

Traffic is naturally local (a city, a country) and cross-region latency hurts.

Say it like this

Rides never cross a city, so I shard every stateful component by region and keep each request inside one region, which gives local latency and makes a region outage a partial outage instead of a global one.

Basic solution

Partition data and services by geography so a request is served entirely within its region.

region shardsgeo DNScell-based architecture

Requests stay inside a region; global data replicates in

The edge routes by location or entity id into one region, which owns its index, matcher and trip store. A small global store for accounts replicates into every region and is written through its home region.

How it works

  1. Choose the region as the unit of isolation: a metro area or a country. Each region gets its own copy of every stateful component (location index, match queue, trip database, caches), sized for that region alone.
  2. Route at the edge. Geo DNS or the gateway looks at where the request came from, or at the entity it touches (a trip id carries a region prefix), and sends the request to that region's servers. From then on it stays there.
  3. Inside the region, services talk only to that region's stores. A match in Sydney reads the Sydney geo index and writes the Sydney trip database; nothing on the hot path crosses to another region.
  4. Keep the small amount of truly global data (user accounts, payment methods, pricing config, feature flags) in one replicated store. Writes go to a single home region through one write path; every other region holds a read replica a second or so behind and reads from it locally.
  5. Handle movement explicitly. When a trip is about to cross a region boundary, a handover step copies its state to the destination region before it arrives, so the destination can continue it.
  6. Deploy and fail by region. Roll a release out to one region first and watch it. When a region is unhealthy, flip the edge routing so its traffic lands on a neighbour, which serves it in degraded mode rather than everything going down.

Why region, not hash

  • A hash spreads load evenly but puts every request across the network, since the data for a request could be anywhere. A region keeps the data next to the traffic and bounds the blast radius of a failure to one geography.

Worked example

Regioning the ride-matching platform

  • 300 cities; the largest has 200K online drivers and 50K active trips, the smallest a few hundred drivers.
  • Location index per region: the largest city needs 33K updates per second and a 20 MB index, which is one Redis node; the smaller cities share nodes, about 10 cities per node. That is about 60 Redis nodes in total instead of one global cluster that every update would hit.
  • Matcher per region: a single matcher process per city removes contention (two riders, one driver) without a distributed lock, because only one process ever assigns drivers in that city. 300 lightweight matchers instead of one global one taking a lock per match.
  • Latency: a rider in Sydney hits the Sydney region, 10 ms away, rather than a global primary in Virginia at 200 ms; a match completes in under a second instead of several.
  • Blast radius: the Sydney region failing affects Sydney's 1 percent of trips. A global index failing affects 100 percent.
  • Global data: 50M user profiles replicated to every region, 10 GB per replica; writes (profile edits) are a few hundred per second and go to the home region, with the local replica catching up within a second.

Variants and when each wins

Region sharding with a global layer

  • Hot-path state lives per region, and a small global store holds accounts and config. It wins for products tied to a place, such as rides, because requests get local latency and a region outage is a partial outage.

Cells (cell-based architecture)

  • Capacity is divided into fixed-size cells and users are assigned to cells regardless of geography. It wins for even blast radius and simple capacity maths, at the cost of cross-cell latency for products where users interact with nearby users.

Home region per user

  • Every user has a home region holding all their data, and their requests are routed there wherever they are. It wins on simple ownership and consistency; it loses for travellers, who pay cross-ocean latency on every call.

Global active-active

  • Every region can serve any request and data replicates everywhere, with conflict resolution for concurrent writes. It wins on availability for global products; it is the hardest to keep consistent and is not needed for rides.

Recommendation: region shards for anything tied to a place, cells for a global product with no natural geography, and a home-region layer for the per-user data either way.

Failure modes

Entities that cross the boundary

  • A trip from one city to a neighbouring one is lost at the border. It happens because the trip's region was fixed when it was created, and the destination region has never heard of it. Add a handover that copies the trip to the destination region at the boundary, or define regions large enough (a metro area rather than a district) that trips rarely cross.

Global store on the hot path

  • Every match waits on a cross-ocean round trip. It happens because the user profile or pricing rules are read from the home region on each request. Replicate the global data into every region and read the local replica, accepting that a profile edit takes a second or two to appear everywhere.

One region becomes a hot shard

  • The biggest city saturates its nodes while hundreds of small cities idle. It happens because regions are defined by geography and are nowhere near equal in size. Sub-partition the large regions into geographic cells spread across nodes, and pack several small regions onto one node.

Failover with no capacity

  • Failing a region over to its neighbour takes the neighbour down as well. It happens because the neighbour was sized for its own load with no headroom reserved. Size each region to absorb its largest neighbour at degraded service, and practise the failover so the gap shows up in a drill rather than an incident.

Config drift between regions

  • A feature works in one city and not another. It happens because regions were deployed from different versions or hand-edited configs. Use one deployment pipeline that rolls regions in a fixed order from a single artifact, and keep per-region configuration as data in the global store rather than in files on servers.

Interviewer follow-ups

Answer out loud before you open each one.

How does a request find its region?

From the entity when there is one: a trip or driver id carries a region prefix, so the gateway routes on it. Otherwise from the client’s location or geo DNS for the first request, after which the response tells the client which region to use. The client caches the region and includes it on every call.

What stays global and how do you keep it consistent?

Accounts, payment methods, pricing rules, feature flags: small, read-mostly data. Writes go to a single home region through one path; every region holds a read replica a second or so behind. Anything that must be read-after-write, such as a just-added payment method, reads from the home region once.

What is the difference between a region and a cell?

A region is defined by geography and holds everything for that place, so sizes vary wildly. A cell is a fixed-capacity slice that users are assigned to regardless of where they are, so every cell is the same size and the blast radius is uniform. Local products want regions; global ones want cells, and large regions are often split into cells.

How do you fail a region over?

Flip the edge routing so the region’s traffic lands on a neighbour, which serves it in degraded mode: matching works from the neighbour’s copy of the global data, but the failed region’s in-flight trips and local index are unavailable until it recovers. That is only possible if the neighbour has reserved headroom and the handover was rehearsed.

Connections

In study paths
Splitting the data
Built on
Geohash and hierarchical cells (H3, S2) · CRDT
Pairs with
Partition the data · Find things near a point · Ingest millions of position updates · Route users to the nearest healthy site · Spread across zones and regions · Isolate failures
Pulls against
Add nodes without remapping everything
Used in
Ride Matching (Uber / Lyft)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon Route 53Edge / geo DNSCloud DNSAzure DNS, Traffic ManagerCloudflare DNS, NS1
Amazon AuroraGlobal store (home), Local replicasAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB