# Monolithic Persistence: One Database for Everything Slows Everything

> Why keeping all of an app's data in one store can slow the whole system, how to spot the problem, and how to split data by how it is used.

**In short:** Monolithic persistence means keeping all of an app's data in one store, which can slow the whole system, so the fix is to split data by how it is used.

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

## What You Will Learn

This lesson explains Monolithic Persistence. A monolith is one big unit. Persistence means keeping data so it lasts after a request ends. Monolithic Persistence is the habit of putting all of an application's data into one data store. It is a performance antipattern, which means a common design mistake that slows systems down, often only under heavy load. You will learn why the habit hurts, how to spot it, and how to fix it by splitting data by how it is used.

- **Data store:** Any place a system keeps data, such as a database, a file store, or a cache.
- **Database:** A data store built to save, search, and change records safely.
- **Workload:** One kind of work a system asks of its data, such as writing orders, reading sessions, or saving logs.
- **Access pattern:** The way code reads and writes a set of data, for example many small lookups or a long stream of new lines added at the end.
- **Contention:** A fight for the same resource, so that requests have to wait their turn.
- **Bottleneck:** The one slowest part that limits how fast the whole system can go.
- **Session:** The short-lived record of a signed-in user's activity between page visits.
- **Polyglot persistence:** Using several kinds of data stores in one system, with each store chosen for one kind of data.

## What Is the Shared Store Problem?

Many early applications used one data store for everything. Teams did this because one store kept the design simple and matched the skills the team already had. That approach can work for a small app. Modern systems are different. They keep many kinds of data at once: business records, sessions, files such as photos, cached copies, queued messages, application logs, and telemetry, which are the numbers and events that show how a system is running.

Putting all of that into one store can slow the system for two main reasons. First, storing and fetching large amounts of unrelated data in one place causes contention, and contention leads to slow responses and failed connections. Second, no single store suits every kind of data. A store that is good at safe record updates may be a poor fit for a huge stream of log lines.

- **Shared resources:** The Azure page names processor time, memory, I/O, and database connections as shared resources. One heavy job can starve the others, even if the others are small and important.
- **No room to tune:** Each kind of work likes different settings. A store set up for quick, small updates may handle long scans badly. When everything lives in one store, you cannot tune it for every workload at once.
- **Scaling is all or nothing:** The Azure page recommends scaling up first, then partitioning the load across servers if limits remain.

A simple picture helps. Imagine one oven that a bakery uses for bread, cakes, and a huge batch of cookies at the same time. The bread waits, even though bread is not the problem. The oven is the shared limit. A database works the same way.

> **Tip:** When you read a design, list every kind of data it stores. If one store holds three or more unrelated kinds, ask which one could slow the others down.

### Why Does One Store Hurt?

The core issue is that one heavy workload slows the others. Transactions (groups of changes that must all succeed or all fail), application logs, user sessions, search, media files, and analytics jobs all compete for the same store. When the logs spike, checkout can slow down, even though nobody changed the checkout code.

## How Do You Spot It?

This antipattern rarely announces itself. It looks like a system that gets slower as more people use it. The signs below point toward a shared store that has become a bottleneck.

### What Symptoms Should You Watch For?

- **Latency rises under load:** Latency is the time one request takes to finish. When response times climb as more users arrive, and the code has not changed, the shared store may be the limit.
- **Lock contention:** A lock is a short hold that stops two writes from changing the same data at once. Many writes from unrelated features waiting on the same locks is a sign of contention.
- **One store sized for the worst case:** The team buys a large database to survive its busiest hour, then pays for that size all day, even when traffic is calm.
- **Wrong fit:** A schema, which is the layout that says what each table holds, may suit one data type well. Other data then gets forced into awkward shapes that are slow to read or write.

> **Tip:** A slow system is not always a shared-store problem. Check the symptoms against real timing data before you change the design.

### How Do You Confirm the Cause?

1. **Record timing:** Measure how long each operation takes. Note each point where the app reads from or writes to a data store.
2. **Watch real use:** Monitor production for a few days, or run a load test, which is a planned test that sends many requests to see how the system copes. Use a realistic mix of users doing normal tasks.
3. **Find slow periods:** Compare the slow times with database activity. A daily batch job, for example, may line up exactly with the slowdowns.
4. **List busy stores:** During those slow periods, find which data stores and tables took the most work.
5. **Name the contended resource:** Decide which shared resource is under pressure, such as processor time, connections, disk input and output, or locks, and which unrelated data is using it.

## Worked Example: An Online Shop

Imagine a small online shop. It keeps five kinds of data in one SQL database, which is a relational database that stores data in linked tables: orders, user sessions, product images, search queries, and click logs. During a flash sale, a short burst of very high demand, the site writes a flood of session updates and log lines. Checkout slows down, even though the checkout code did not change. Here is how the team fixes it.

1. **Sort the data by use:** Orders need exact, safe updates, so they stay in the SQL database. Sessions are small, short-lived, and fetched by a key, so they move to a key-value store, which finds each value by its key name.
2. **Move the big files:** Product images are large files that are read often but rarely change. They move to object storage, which keeps whole files under a name. The database keeps only the name.
3. **Move the logs:** Click logs arrive as a steady stream and are read later for reports. They move to a separate log store, so their constant writes no longer compete with checkout.
4. **Keep search apart:** Search needs a search index, which is a structure built to find words quickly. The index lives beside the shop, not inside the order tables.
5. **Check the result:** In the next flash sale, checkout should stay fast, because orders no longer share a store with the surge of session and log writes. The team confirms this with the same timing steps used to find the problem.

## How to Fix It: Split Data by Use

The fix is called polyglot persistence. It means using several kinds of data stores in one system, each chosen for the data it holds. The rule is simple. Separate data by how it is used, then pick a store that fits that use. Azure's guidance makes the same point with logs: log records arrive in order, while business records are often read at random and are usually linked, so the two should not share one store.

Splitting data is not free, and it can create a new trap. A common mistake is a distributed monolith, where the app is split into services but they still share one database. The shared database then becomes the bottleneck again.

### Match Each Data Type to a Store

- **Business records:** A relational database suits linked records that need safe, multi-row changes, such as orders and payments.
- **Sessions and temporary data:** A key-value store or an in-memory cache returns short-lived data quickly when given its key.
- **Files and media:** Object storage holds photos and documents well, so the database does not have to carry big files.
- **Reports and documents:** A document database can keep one formatted report or document together as a single item. Azure's guide suggests this for formatted reports.
- **Logs and telemetry:** A log store handles long, ordered streams of records, since log records are inherently sequential.

### Tradeoffs to Weigh

- **More parts to run:** Each store needs backups, upgrades, monitoring, and staff who know it. Operations work grows with every new store.
- **Consistency across stores:** Consistency means readers see correct, matching data. When one action updates two stores, keeping them in step is hard, and for a short time they may disagree.
- **More code paths:** The application must know which store to call for which data. That adds code, and each path is a place for bugs.
- **Scale first:** Before splitting, Azure's guidance suggests scaling up the database you already have. If limits remain, splitting the load across servers comes next, and that may require redesigning the app. Later lessons cover that step.

> **Tip:** Split the workload that causes the slowdown first, as the shop did with sessions and logs. Every new store is one more thing to run, so add one only when the measurements justify it.

## A Small Code Sample

These two samples are toy models, not real databases. The first is a routing table that sends each kind of data to its own store. The second shows how one shared list mixes unrelated work.

```python
stores = {
    "orders": "sql",
    "sessions": "key-value",
    "images": "object",
    "logs": "log-store",
}
for kind in ["orders", "sessions", "images", "logs"]:
    print(kind, "->", stores[kind])

```

Output:

```text
orders -> sql
sessions -> key-value
images -> object
logs -> log-store

```

```python
shared = []
for i in range(3):
    shared.append("order")
for i in range(5):
    shared.append("log")
print("operations on shared store:", len(shared))
print("log operations:", shared.count("log"))

```

Output:

```text
operations on shared store: 8
log operations: 5

```

In the second sample, three order operations share one list with five log operations. A flood of one kind of work fills the same place that the other kind needs. Real stores are far more complex, but the lesson is the same.

## Check Your Understanding

**Quick check:** A food-delivery app keeps orders, live driver location pings (small GPS updates sent every few seconds), and app logs all in one database. At dinner rush, placing an order becomes slow, even though the number of orders is normal. What is the likely cause, and which data should move out first?

A. The orders table has too many columns, so move the orders out first.
B. Location pings and logs compete with orders for the same store.
C. Customers log in too often, so move the logs to a key-value store and leave everything else alone.
D. The database needs a faster screen for staff, so copy the logs into the orders table.

**Answer:** B. The shared store is the bottleneck. Location pings arrive constantly and use the same resources as orders. Moving them out first frees capacity for orders, which need safe, linked records that stay in the database.

**Quick check:** A note-taking app keeps notes, photo attachments, and sign-in sessions in one database. Photo uploads are large. While people upload photos, saving a note feels slow. Which change best matches the fix for this antipattern?

A. Add more columns to the notes table so it can hold photo data directly.
B. Turn off sign-in sessions so the database has less work to do.
C. Store each photo in object storage and keep only its file name in the database.
D. Copy the whole database into a second database with the same tables.

**Answer:** C. Photos are large files that do not suit a record store. Object storage keeps whole files, and the database keeps a small pointer. That leaves the database free to save notes quickly.

## Key Takeaways

- **Separate by use:** Group data by how it is read and written, then choose a store for each group. Do not force every kind of data into one place.
- **Measure first:** Use timing data and busy-store records to prove the shared store is the bottleneck before you split anything.
- **Weigh the cost:** Each extra store adds operations work, cross-store consistency problems, and code paths. Split only where the gain is clear.

Monolithic Persistence is not a crime. A single store is often the right start. The mistake is letting one store carry every kind of work for too long, until its limits show up as slow checkouts and failed connections.

## Sources

- [Monolithic Persistence antipattern, Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/monolithic-persistence)
- [Performance testing and antipatterns, Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/)
- [Microservices architecture style, Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/guide/architecture-styles/microservices)
- [Polyglot persistence, Wikipedia](https://en.wikipedia.org/wiki/Polyglot_persistence)
- [Difference Between Monolithic Database and Polyglot Persistence, GeeksforGeeks](https://www.geeksforgeeks.org/dbms/difference-between-monolithic-database-and-polyglot-persistence/)
- [Monolithic DB vs. Polyglot Persistence: Microservices Data Strategy, developers.dev](https://www.developers.dev/tech-talk/monolithic-database-vs-polyglot-persistence-an-architectural-decision-framework-for-microservices.html)
