Volume 3 · Chapter 10 — Entity Framework Core and Data Access Engineering

Enterprise Data Access Architecture

Goutham Mayaluri 8 min read .NETC#

10.1 Introduction

Enterprise data access architecture defines how applications interact with databases, reporting platforms, and distributed services to achieve scalability, reliability, and maintainability. Large systems need more than CRUD: clear data-ownership boundaries, optimized read and write paths, resilience, and the right storage technology per workload. This chapter ties the volume together with the architectural patterns that Volumes 6, 8, and 9 develop in depth.

10.2 Clean Layering and CQRS

Data access should be isolated from business rules: the application layer defines what it needs through interfaces, and infrastructure (EF Core) provides the implementation, following Clean Architecture’s Dependency Rule. For high-volume systems, CQRS separates the write model (transactional, normalized, consistent) from the read model (denormalized, query-optimized), letting each be scaled and tuned independently - often with the write side on a primary and reads on replicas or a purpose-built read store.

flowchart TB
  API["API"] --> Cmd["Commands"]
  API --> Qry["Queries"]
  Cmd --> Write[("Write model / primary (consistent)")]
  Write -- "events" --> Proj["Projections"]
  Proj --> Read[("Read model / replicas (fast)")]
  Qry --> Read

10.3 Microservice Data Ownership

In microservices, each service owns its data and no other service reads its tables directly - this is what makes independent deployment and scaling real. Services integrate through APIs and events, not shared databases. Sharing a database recreates hidden coupling and is the shortcut that turns microservices back into a distributed monolith.

10.4 Event-Driven Sync, Event Sourcing, and Polyglot Persistence

Services propagate changes through events (reliably, via the outbox pattern) rather than reaching into each other’s data, accepting eventual consistency. Event sourcing stores changes as an immutable event log (perfect audit history, temporal queries) where the audit trail justifies its complexity. And polyglot persistence uses the right store per workload - relational for transactions, NoSQL for scale, search engines for discovery, warehouses for analytics.

10.5 Distributed Consistency

Because distributed ACID transactions don’t scale across services, systems use sagas (local transactions plus compensation), the outbox pattern (atomic state-plus-event), idempotency, and event replay to achieve correctness under eventual consistency - the subject of Volumes 6 and 9.

Senior Interview Question

Should microservices share a database? Why or why not?

Generally no - each microservice should own its data and no other service should access its database directly. The reason is that data ownership is what makes microservices actually independent, which is the whole point of the architecture. If services share a database, they become coupled through the schema: one service’s schema change can break others, they must coordinate migrations and releases, and they contend for the same database resources - so they can no longer be developed, deployed, or scaled independently, which recreates the coupling of a monolith while paying the operational cost of distribution (the “distributed monolith,” the worst of both worlds). By contrast, when each service owns its own database, it can evolve its schema freely, choose the storage technology that fits its workload, scale its data tier on its own, and deploy without coordinating with others - genuine independence. Services then integrate through well-defined contracts - synchronous APIs when they need data from another service, and asynchronous events when they react to another service’s changes - rather than reaching into each other’s tables. If a service needs a local copy of another’s data for performance or its own queries, it subscribes to that service’s events and maintains its own denormalized copy (accepting eventual consistency), rather than querying the owner’s database. The trade-offs of not sharing are real - you give up cross-service ACID transactions (hence sagas and eventual consistency) and some data is duplicated - but those are the necessary consequences of the independence you’re buying, and they’re manageable with the right patterns (outbox, idempotency, sagas). So the principled answer is that microservices should not share a database because database-per-service is the foundation of their independence; sharing one couples them and defeats the architecture, so services own their data and communicate via APIs and events, accepting eventual consistency as the price of that independence. The litmus test: if two services can’t be deployed independently because they share a database, they aren’t really separate services.

Principal Engineer Question

How do you design a data architecture for a platform serving millions of users where a single database has become the bottleneck?

When a single shared database becomes the bottleneck at scale, the answer isn’t one change but a set of architectural moves that separate concerns, match storage to workload, and reduce contention - applied incrementally and driven by where the actual load is. First, separate read and write paths, because read and write workloads usually have very different profiles and volumes: I offload reads to read replicas and, where read shapes diverge sharply from the write model or read scale is extreme, introduce CQRS with a denormalized, query-optimized read model fed from the transactional store via events, so heavy reads and reporting don’t contend with writes. Second, cache aggressively: put frequently-read, slowly-changing data in a distributed cache so most reads never hit the database at all, which often removes a large fraction of the load immediately. Third, decompose by ownership: if it’s a monolith over one database, I identify bounded contexts and move toward service-owned databases so each capability’s data scales independently and no single database is a shared chokepoint - services integrate via APIs and events rather than shared tables. Fourth, use the right storage per workload (polyglot persistence): keep the transactional core on a strongly-consistent relational store, but use a horizontally-scalable store (e.g. Cosmos DB) for globally-distributed, high-scale key-based access, a search engine for search, and a data warehouse/lake for analytics - rather than forcing everything through one relational database. Fifth, scale the data tier itself: proper indexing and query optimization first (often the cheapest win), then read replicas, then partitioning/sharding of the largest tables by an even, access-aligned key when write throughput genuinely exceeds a single node - taking each step only as measured need requires, since sharding adds significant complexity. Sixth, move heavy and non-critical work off the synchronous path into asynchronous, event-driven, batched background processing, and use the outbox pattern and sagas for reliable cross-service consistency under eventual consistency. Throughout, I keep the money/critical paths strongly consistent while letting read models and cross-service data be eventually consistent for scale, and I measure continuously - profiling queries, watching database load and replica lag - so each change targets the real bottleneck. So the design is: split reads from writes (replicas + CQRS), cache hot data, give services their own databases, use polyglot persistence for the right workloads, scale the data tier up a ladder (index → replicate → shard) on evidence, and push heavy/eventual work to async processing - transforming a single-database bottleneck into a set of independently scalable, workload- matched data stores, which is how you support millions of users without one database limiting the whole platform.

10.6 Banking Enterprise Scenario

A banking platform runs customer, payment, transaction, and reporting workloads on one database that becomes a bottleneck as usage grows. The architecture evolves to service-owned databases per bounded context, read replicas and a denormalized read model for balances and statements, event-driven synchronization via the outbox pattern, caching of hot reference data, and a data warehouse for analytics - keeping the ledger strongly consistent while scaling reads and reporting independently, with sagas coordinating cross-service workflows.

Volume 3 Summary and Interview Rapid-Fire

  • EF Core architecture: query pipeline, change tracker, providers
  • DbContext lifecycle and tracking
  • LINQ translation, IQueryable vs IEnumerable, and N+1
  • Relationships, owned entities, and aggregates
  • Migrations and expand-and-contract
  • Transactions and optimistic concurrency
  • Repository/Unit of Work (applied judiciously)
  • Performance optimization
  • Caching strategies
  • Enterprise data access architecture
Rapid-Fire Interview Question

How does EF Core turn LINQ into SQL?

It captures the LINQ as an expression tree, translates it to provider SQL, executes it in the database, and materializes the results.

Rapid-Fire Interview Question

When AsNoTracking?

For read-only queries - it skips change-tracking overhead, lowering memory and speeding up reads.

Rapid-Fire Interview Question

N+1 problem and fix?

One query for a list plus one per item for related data - fix with Include (eager) or a single projected query.

Rapid-Fire Interview Question

IQueryable vs IEnumerable?

IQueryable translates to SQL (database-side); switching to IEnumerable early loads everything into memory and filters in C#.

Rapid-Fire Interview Question

DbContext lifetime?

Scoped - one per request; singleton is unsafe (not thread-safe, stale/shared tracked state).

Rapid-Fire Interview Question

Expand-and-contract in one line?

Add the new schema additively → deploy → remove the old once no version needs it - backward-compatible at every step for zero downtime.

Rapid-Fire Interview Question

Optimistic concurrency detection?

A rowversion token in the UPDATE’s WHERE clause - zero rows affected means a conflict (DbUpdateConcurrencyException).

Rapid-Fire Interview Question

Does EF Core already give you repository/unit of work?

Yes - DbSet ≈ repository, DbContext ≈ Unit of Work; add your own only when it isolates the domain or provides meaningful aggregate queries.

Rapid-Fire Interview Question

Why is cache invalidation hard?

Cached data goes stale the moment the source changes, and keeping copies consistent across a distributed, concurrent system without constant checking is genuinely difficult.

Rapid-Fire Interview Question

Should microservices share a database?

No - database-per-service preserves independence; sharing one couples them into a distributed monolith.