Sub topic 15
Data residency and compliance constraints
This chapter assumes you know basic cloud networking and DNS, stateless services behind load balancers, and the replication models from the earlier chapters. Active-active designs copy data between regions by default. Residency and compliance rules can forbid that copying for some data. This chapter shows how to find those limits and design around them.
Residency, sovereignty and compliance
Three terms are often mixed up.
- Data residency is the requirement that data is stored in a specific country or region.
- Data sovereignty is the requirement that data is subject to the laws of the country where it is held. This includes the reach of foreign authorities over the provider that holds it.
- Compliance is the wider duty to meet a regulation or standard. Examples are GDPR, HIPAA, PCI DSS and national banking rules. Many of these set rules on where data may go and who may access it.
The practical effect is that "replicate everything to every region" can become illegal or contractually forbidden. A multi-region design that ignores this passes its failover tests and then fails an audit.
Residency rules usually cover more than the primary database. Check each of these:
- Backups and snapshots, including copies sent to a disaster recovery region.
- Logs, traces and metrics that contain user identifiers.
- Search indexes, caches and analytics warehouses built from the data.
- Message queues and event streams that carry personal data.
- Support tooling and staff access from other countries.
How residency changes the active-active model
Earlier chapters treated every region as a full peer holding a full copy of the data. Residency breaks that assumption. You now have two kinds of data.
| Data class | Example | Replication rule |
|---|---|---|
| Global | Product catalog, feature flags, public content | Replicate to all regions |
| Regulated | Customer profiles, payment details, health records | Stay inside an allowed set of regions |
| Derived and anonymized | Aggregate counts, tokenized identifiers | Replicate if the anonymization meets the legal definition |
Treat the allowed set as part of the data model. Each regulated record belongs to a residency zone, which is a group of one or more regions that may hold it. A zone can hold more than one region. For example, an "EU" zone with Frankfurt and Ireland keeps data in the EU and still gives you two regions for failover.
This ties directly to write ownership. Partitioning by home region, covered earlier, gives you a natural way to enforce residency. A user's home region is assigned from their residency zone, and all writes for that user go to a region in the zone.
Design patterns
Partition by zone, replicate within the zone
Give each zone its own set of regions and its own database cluster. Replicate between regions inside a zone, but never across zones. Inside the EU zone you keep full active-active behavior. Between zones you have separate systems that share only global data.
The trade-off is that a zone with a single region has no regional failover. If the law requires all data to stay in one country and that country has only one region from your provider, a regional outage takes that zone down. Plan for this explicitly. Options include a second provider region in the same country, a second provider, or an agreed degraded mode.
A global directory with a regional data plane
Users must be routed to their zone before the system can read their data. That requires some lookup of "which zone does this user belong to". Keep this lookup small and free of personal data. A mapping from an opaque account ID to a zone name is usually acceptable to replicate globally. An email address or name is not.
ZONES = {
"eu": ["eu-central-1", "eu-west-1"],
"us": ["us-east-1", "us-west-2"],
}
# Global directory: opaque account ID -> zone. No personal data.
DIRECTORY = {
"acct_7f3a": "eu",
"acct_91bc": "us",
}
def allowed_regions(account_id):
zone = DIRECTORY[account_id]
return ZONES[zone]
def check_replication(account_id, target_region):
"""Refuse to replicate a regulated record outside its zone."""
if target_region not in allowed_regions(account_id):
raise PermissionError(
f"{account_id} may not be replicated to {target_region}"
)
check_replication("acct_7f3a", "eu-west-1") # allowed
try:
check_replication("acct_7f3a", "us-east-1")
except PermissionError as err:
print(err) # acct_7f3a may not be replicated to us-east-1A check like this belongs in the replication path itself, not only in application code. Application checks are bypassed by a new service, a migration script or a misconfigured replication rule. Prefer controls that fail closed, such as replication rules that name destination regions explicitly, and cloud policies that deny resource creation outside approved regions.
Tokenization and field-level separation
Sometimes only a few fields are regulated. You can store those fields in the home zone and keep a token in the globally replicated record. The global record then holds an opaque token, and only the home zone can resolve it to the real value.
Skip that sketch. Use the diagram below instead.
Interactions with other design choices
Routing
Latency-based DNS routing sends a user to the nearest region. That may not be a region in the user's zone. A traveler from Germany in the US would reach a US region that is not allowed to hold their data. Two options exist:
- Route by zone first, accepting higher latency. The edge or a thin proxy forwards the request to the home zone.
- Terminate the connection nearby, then forward only the required calls to the home zone.
Both options add a cross-region hop to some requests. Measure it and include it in your latency budget.
Failover
Failover rules must respect zones. A health check that moves traffic from an unhealthy EU region to a healthy US region may break the residency rule even though it restores availability. Define failover targets per zone. If no allowed target remains, decide ahead of time whether the service goes read-only, shows an error, or waits. Do not let the default routing policy decide this for you.
Conflict resolution
Conflict resolution gets simpler within a zone, because each record has a bounded set of regions that can write it. Global data keeps the usual conflict problems. Pick last-writer-wins or a CRDT for it as in the earlier chapter.
Backups, logs and observability
Backups are the most common violation. A cross-region backup copy for disaster recovery is good engineering and may be a residency breach. Keep backups in the same zone. Do the same for logs. Scrub or hash personal identifiers before shipping logs to a central monitoring system, or run monitoring per zone and only aggregate metrics that contain no personal data.
Capacity
The "survive the loss of a region" rule from the capacity chapter now applies per zone. Each zone needs enough headroom in its remaining regions to absorb a regional loss. A global pool of spare capacity does not help a zone whose data cannot move.
Common errors
- Treating the primary database as the whole scope. Backups, logs, caches and search indexes leak data across borders.
- Letting failover cross zones. Health-based routing optimizes for availability, not legality.
- Enforcing residency only in application code. Add infrastructure policy as a second layer.
- Putting personal data in the global directory. The routing table becomes a regulated dataset replicated everywhere.
- Assuming a region name equals a legal boundary. Confirm the physical location, the provider's legal entity and any rules about remote access.
- Ignoring deletion. Rules such as the GDPR right to erasure require deletes to reach every copy, including replicas and backups, within the legal time limit.
Verifying compliance
Treat residency like any other property you test.
- Keep an inventory of every data store and its zone.
- Add automated checks that list the regions of all storage buckets, databases and queues, and fail when a regulated store appears outside its zone.
- During failure tests, confirm that traffic and data stay inside the zone.
- Keep audit records of who accessed regulated data and from where.
Legal interpretation is not an engineering decision. Involve legal and compliance staff early, and record their decisions as explicit rules that engineers can implement and test.
Exercise
A service has an EU zone with eu-central-1 and eu-west-1. Latency-based DNS sends a German user to us-east-1 while they are traveling, and the account is in the EU zone. Name two ways to handle this, and say what each costs.
Show answer
Route by zone first, so the edge forwards the request to an EU region, or terminate the connection in the US and forward only data calls to the EU zone. Both add a cross-region network hop, so the user sees higher latency. The data stays in the EU zone in either case, and no regulated record is stored or replicated in us-east-1.
Exercise
Health checks show eu-central-1 is down. The only other healthy region is us-east-1, but the EU data may not leave the EU. What should the failover design do?
Show answer
Fail over only to eu-west-1, which is in the same zone. If eu-west-1 is also unavailable, the design must follow a predefined fallback such as read-only mode or a clear error. It must not send EU regulated traffic or data to us-east-1, even though that would restore availability.