# Geodes: Identical Copies of a Service in Many Regions, Each Able to Serve Anyone

> A geode is an identical copy of a service placed in one region. Users go to the nearest healthy copy, and the copies share their data.

**In short:** A geode is an identical copy of a service placed in one region; users go to the nearest healthy copy, and the copies share their data.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/geodes

## What you will learn

Many apps start in one cloud region, which is a group of data centers in one geographic area. That works until users live far away, or until the whole region has a bad day. The geode pattern is one answer. It runs the same service in several regions, and any copy can handle any user's request. In this lesson you will learn what a geode is, how data moves between geodes, how a worked example plays out, and what the pattern costs.

- **Region:** One geographic area where a cloud provider runs data centers, such as US East or Western Europe.
- **Latency:** The delay between sending a request and getting the reply.
- **Geode:** One full, identical deployment of a service in one region. The name comes from geographical nodes.
- **Replication:** Copying data from one machine or region to others so they all hold it.
- **Replication lag:** The short delay before a copy receives a change that was made somewhere else.
- **Strong consistency:** A rule where every read sees the latest confirmed write.
- **Eventual consistency:** A rule where copies agree after a while, but a read may show older data for a short time.
- **Conflict:** Two copies change the same item at about the same time, so the system must pick or merge the results.
- **Failover:** Sending work to another working part when the main part stops.

## What is a geode?

A geode is one complete deployment of a backend service in one place. The service is deployed to several geographic locations, and each deployment is a geode. Any geode can answer a request from any user, no matter which region that user is in. The pattern is active-active, which means every copy is live and taking traffic at the same time, not waiting as a spare.

The pattern is built around one idea: bring the compute to the data. Older designs kept data in one remote database and sent every request there. That adds distance for far-away users, and it makes one place a single weak spot. With geodes, each copy of the app sits near a local copy of the data, so requests stay short.

### What is the core design?

- **Identical copies:** Every geode is built from the same template, so the parts match. Teams deploy all of them with the same scripts, which keeps them alike.
- **Self-sufficient:** A geode has the regional resources it needs to finish a request. It does not depend on another geode's app layer, so if one geode dies, the others keep running.
- **Front door routing:** A global routing service sits in front of all geodes. It checks their health and sends each user to a suitable one. No single primary region has to be up for the system to work.

The routing layer and the data layer are shared. The geodes are loosely linked through routing and replication, not by calling each other on every request. That loose link is why one failure does not spread far.

### How does routing to the nearest healthy geode work?

1. **Request arrives:** A user opens the app. The request first reaches the global routing service, which might be a DNS-based or edge network product.
2. **Health check:** The router keeps checking whether each geode answers correctly. A geode that fails the checks is removed from the choices until it recovers.
3. **Pick the nearest:** Among the healthy geodes, the router chooses the one with the shortest network path to the user.
4. **Serve from there:** The chosen geode handles the request using its local copy of the data, so the reply comes back quickly.
5. **Shift on failure:** If that geode fails later, the next request goes to the next-nearest healthy geode. Users may be moved to another geode, but availability depends on healthy shared dependencies and enough compute and database capacity in the surviving geodes.

Here is a small model of that choice. The numbers are made up for the demo. The code picks the closest geode that is still up.

```python
# Made-up demo numbers: milliseconds from one user to each geode.
latency_ms = {"us": 120, "europe": 25, "asia": 210}
healthy = {"us": True, "europe": True, "asia": True}

def pick_geode(latency, alive):
    up = [name for name in latency if alive[name]]
    return min(up, key=lambda name: latency[name])

print(pick_geode(latency_ms, healthy))
healthy["europe"] = False
print(pick_geode(latency_ms, healthy))

```

Output:

```text
europe
us

```

## How does the data work?

Running the app code in many places is the easy part. Data is harder. Each geode reads and writes through a data store that is replicated across regions. A write made in one region is copied to the others. In Azure's example, a Cosmos DB account with multiple write regions lets each geode read and write in its own region, and those writes copy to other regions in the background. That background copy is called asynchronous replication, because the write does not wait for the copies to finish.

### Waiting for copies to catch up

Because copying takes time, another geode may briefly show an older version of an item. The app has to expect this. It gets worse when one user's next request lands on a different geode. A common fix is a session token. The client keeps a small token from its last write and sends it with later reads, so it can see its own changes even after it moves to another geode. The Cosmos DB guidance says the shared token should be used for reads only in multi-region-write accounts.

### Should you choose strong or eventual consistency?

Consistency is the rule for what readers are allowed to see. With strong consistency, every read returns the latest confirmed write. That is safe, but across long distances it is slow, because a write must wait for other regions to agree. With eventual consistency, copies agree after a while. Reads are fast and cheap, but a reader may see old data for a moment. The geode pattern usually leans toward eventual consistency, and then the app is designed to cope with stale reads.

### When two regions change the same item

Take a product with 10 items in stock. A shopper in New York reads 10, subtracts one, and writes 9. At nearly the same moment, a shopper in London also reads 10, subtracts one, and writes 9. Two sales happened, but the stock shows 9. This is a conflict. Cosmos DB's default rule is last writer wins. The write with the highest value in a chosen field, by default a server timestamp, is kept, and the others are discarded. Conflicts settled this way do not show up in the conflict feed, so nobody is alerted.

> **Tip:** Last writer wins can silently drop a real sale. For money, stock, or anything you cannot afford to lose, use a custom rule, a version check, or a merge that combines both changes.

The model below uses the same rule: keep the write with the highest timestamp. The numbers are made up for the demo.

```python
# Two regions changed the same item. Keep the write with the highest timestamp.
writes = [
    {"region": "us", "value": "quantity 9", "ts": 1001},
    {"region": "europe", "value": "quantity 8", "ts": 1002},
]
winner = max(writes, key=lambda w: w["ts"])
print(winner["region"], winner["value"])

```

Output:

```text
europe quantity 8

```

## Worked example: a messaging app in three regions

Imagine a chat app with identical backends in the US, Europe, and Asia. Each has its own app servers and a local copy of the message store. Follow one message sent by a user in Berlin.

1. **Berlin user is routed:** The routing service sees the user in Berlin. The Europe geode is healthy and closest, so the user connects there.
2. **Message saved locally:** The Europe geode writes the message to its own copy of the data. The write stays in Europe, so the sender gets a quick reply.
3. **Copies catch up:** The data store copies the message to the US and Asia copies in the background. For a short time, a reader in Tokyo may not see it yet.
4. **Europe fails:** The Europe geode stops passing health checks, so the router removes it. Suppose the US geode is the next-nearest one. The Berlin user's next request goes there, and the message is already in that copy.
5. **Europe recovers:** When Europe passes health checks again, the router can send Berlin users back. The Europe copy must first catch up on anything it missed, and the app must tolerate that catch-up period.

The message survived the failure only because it had been copied before Europe went down. If Europe had failed in the moment between the save and the copy, the message could have been lost. That is why the write path and the copy path need to be designed together.

## When to use geodes

Use this pattern when the cost of distance or downtime is high. Three cases fit it well.

- **Global users:** Users spread across several continents all expect fast replies, and a single distant region makes some of them wait.
- **Latency-sensitive apps:** Games, chat, and live tools feel every extra fraction of a second, so the copy near the user matters.
- **Regional failover:** If one region goes dark, the other geodes keep serving. Losing one geode only shifts its traffic, provided the others have spare capacity.

Be careful with rules about where data may live. If a law or contract keeps some data inside one country, a copy in every region may be the wrong design for that data.

## Costs and trade-offs

- **Money:** Every region needs compute and database capacity, so the bill grows with each geode. Autoscaling and pay-per-use compute can cut idle capacity, but they do not make the copies free.
- **Consistency work:** The app must handle replication lag, stale reads, and conflicts.
- **Legal and residency limits:** Some data may need to stay in one place, which limits which geodes can hold it. Plan this before you choose regions.
- **Same version everywhere:** All geodes should run the same release. If one region gets a new version first, users may see different behavior depending on where they land. Build every geode from the same template and roll out changes in a controlled way.

> **Tip:** Test a regional failure under realistic load before you rely on it. Check that the surviving geodes can carry the redirected traffic, and test failback, which means sending traffic back to a recovered region.

Compare this with active-passive designs. In those, one region takes writes and a standby waits. Pick the model that matches the failure you must survive.

## Check your understanding

Try these two new scenarios. Each one needs the ideas from this lesson.

**Quick check:** A video game has a global leaderboard, but every server sits in one US region. Players in Australia see slow replies, and the whole game goes down when that region fails. Which change fixes both problems, and what new problem must you handle with the scores?

A. Add more servers to the US region and keep one copy of the scores, so every player writes to the same place.
B. Run identical game servers in several regions, send each player to the nearest healthy one, and replicate the scores. Handle the case where two regions change the same score at once.
C. Keep the US as the only primary, and give Australian players a cache. Scores stay in the US, so no conflicts can happen.
D. Add a backup region that stays idle and switch to it only after the US region is fully down. Scores can be copied later.

**Answer:** B. Several identical geodes place a server near each player and remove the single-region weak spot. Replicated scores can conflict when two regions update one player's score, so you must choose a rule such as last writer wins or a custom merge.

**Quick check:** A user in Paris changes her display name on the Europe geode. Soon after, the Europe geode fails, and her next request goes to the US geode before the change has copied there. Without any extra design, what is the most likely result?

A. The US geode refuses all her requests until the Europe geode comes back.
B. She may briefly see her old name, because the change has not reached the US copy yet.
C. Her name change is deleted everywhere, because a failed geode cannot make writes.
D. The app rejects the old name and asks her to log in again, so the data is always safe.

**Answer:** B. Replication lag can hide a recent write from a different geode. A session token that the client sends with later reads is one fix. Without a fix like that, the change can look missing for a short while.

## Sources

- [Geode pattern - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/patterns/geodes)
- [Cloud design patterns catalog - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/patterns/)
- [Architecture design patterns that support reliability - Microsoft Azure Well-Architected Framework](https://learn.microsoft.com/en-us/azure/well-architected/reliability/design-patterns)
- [Architecture design patterns that support performance efficiency - Microsoft Azure Well-Architected Framework](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/design-patterns)
- [Manage conflict resolution policies in Azure Cosmos DB - Microsoft Learn](https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts)
- [AKS baseline for multiregion clusters - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/reference-architectures/containers/aks-multi-region/aks-multi-cluster)
- [Multi-Region Architecture - Sujeet Jaiswal](https://sujeet.pro/articles/multi-region-architecture)
- [Geo-Distribution and Multi-Region Design in Distributed Systems - Rahul Suryawanshi](https://rahulsuryawanshi.com/distributed-systems/scalability-performance/geo-distribution/)
- [Multi-Region Architecture: Global Deployment, Data Residency, and Designing for Regional Failures - Veduis](https://veduis.com/blog/multi-region-architecture/)
