Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Design archiving for an orders database where 90 percent of queries touch the last 3 months, but customer service occasionally needs orders from 10 years ago.

Keep hot orders, roughly the last 12 to 24 months, in the operational database partitioned by month, and move older partitions into an archive: a separate cheaper database or self-contained order documents in object storage. Customer service reaches old orders through an archive lookup API that accepts a slower response, the archived records carry a schema version because the live schema keeps changing, restores and lookups are tested on a schedule, and the archive follows the retention policy so orders are deleted when their time is up.

Why this question is asked

When 90 percent of queries touch the last three months, every older row is mostly weight: it makes indexes larger, backups slower, migrations longer and the database more expensive, while helping almost nobody. But we cannot simply delete it, because customer service still needs a ten year old order now and then. Interviewers ask this to see whether we can split data by temperature and still keep old data correct and reachable. In this answer, we will recap what hot and cold data mean here, design the tiers, build the lookup path for customer service, deal with schema changes over ten years, and finish with testing and retention.

1A Quick Recap of Hot and Cold Data

Hot data is data that is read and written often and needs fast responses, here the orders from the last few months. Cold data is rarely read and almost never written, here orders from years ago that are only looked up when a customer calls. The idea of tiering is to store each kind where it is cheapest to meet its needs: hot data on fast database storage with full indexes, cold data on cheaper storage with slower but acceptable access.

Orders are a good fit because an order stops changing after a while. Once it is delivered and the return window and any refunds have closed, perhaps after six to twelve months, it is effectively immutable. That moment is our boundary: data that can still change stays hot.

2Designing the Tiers

In the operational PostgreSQL database we partition orders and order lines by month on created_at. Partition pruning keeps the common queries fast because they only touch recent partitions. We keep, say, 24 months online, which covers the 90 percent case with a wide margin and catches late refunds and disputes. Each month a job moves the partition that has crossed 24 months into the archive tier and then detaches and drops it from the live database.

For the archive tier there are two common choices. One is a separate archive database on cheaper storage with the same tables and fewer indexes, which is easy to query with SQL. The other is to write each order, with its lines, payments and shipping, as one self-contained document, for example a JSON or Parquet record in object storage, indexed by order id and customer id. Object storage costs a fraction of database storage per gigabyte, and at ten years of orders that difference is large.

Step throughOrder storage by age
  1. Hot: PostgreSQL, last 24 monthsmonthly partitions, full indexes, milliseconds
  2. Move job, monthlyexport partition, verify counts, detach and drop
  3. Archive: documents in object storageone record per order, schema version, cheap
  4. Lookup indexorder id and customer id -> archive location
  5. Deletion at end of retentionlifecycle rule removes orders past 10 years

5 layers, from the top down. Step through them one at a time.

Most queries never leave the top layer.

3The Archive Lookup API for Customer Service

Customer service should not need to know where an order lives. Their tool calls one order service, and the service tries the live database first and falls back to the archive. The archive lookup needs its own small index, because object storage cannot search by customer: a table that maps order id and customer id to the archive file and position. That index is tiny compared with the orders themselves and can stay in PostgreSQL.

Java
public OrderView findOrder(long orderId) {
return liveOrders.findById(orderId) // hot path: ~5 ms
.map(OrderView::fromLive)
.orElseGet(() -> archive.find(orderId) // archive path: ~1-2 s
.map(OrderView::fromArchive)
.orElseThrow(() -> new OrderNotFound(orderId)));
}
// archive.find(orderId):
// 1. look up location in archive_index (order_id -> object key) // indexed, fast
// 2. read that object from storage and decode by schema_version // the slow part

Notice that the archive path is allowed to be slower. Agreeing on that with customer service, for example two seconds for a single order and longer for listing a customer's whole history, is part of the design, because it is what lets us use cheap storage. We also make the archive read-only through this API: if an old order truly needs a correction, it is an exceptional, audited operation.

4Versioning the Archive Schema

Over ten years the live schema will change many times. Columns are renamed, a single address becomes billing and shipping addresses, currencies are added. If the archive simply mirrors today's tables, a migration would have to rewrite years of archived data every time. Instead, each archived order is written as it was, with a schema_version field that says which shape it has.

The reader keeps a small decoder per version that turns any archived shape into the current OrderView. When the live schema changes, we add a new version and a decoder; old records are never touched. We also store values that may change later, such as the product name and price at the time of the order, inside the archived document, because the product catalog in ten years may no longer have that product.

5Testing Lookups and Restores

An archive that is never read slowly rots: a decoder breaks, a permission changes, a bucket policy blocks access. So we test it on a schedule. A daily job picks a few random archived orders from different years, reads them through the same API customer service uses, and checks that they decode and that their totals match the counts and sums recorded at archive time. A quarterly exercise restores a whole month from the archive into a scratch database, to prove that the bulk path also works.

6Aligning With the Retention Policy

Keeping orders forever is a risk, not a safety net. The retention policy, agreed with legal, might say orders are kept for ten years for tax and warranty reasons. The archive enforces it: object storage lifecycle rules or a scheduled job delete orders older than ten years, the lookup index entries are removed with them, and legal holds on specific customers or disputes pause deletion for just those records. Personal data inside archived orders also has to respect erasure requests, which is easier if it is encrypted per customer.

7Follow-up questions

Why not just keep everything in PostgreSQL with good indexes?

It works for a while, but every index, backup, restore, vacuum and major version upgrade grows with total data, not with the hot data we actually use. At ten years of orders the database may be ten times larger than it needs to be, which makes recovery after an incident much slower. Moving cold data out keeps the operational database small and quick to restore.

How would customer service list all orders of one customer across both tiers?

The order service queries the live database by customer id and the archive index by customer id, merges the two lists by date and returns summaries. Full details of an archived order are fetched only when the agent opens it, so listing stays fast even for customers with many old orders.

What if analysts want to query old orders in bulk?

Bulk analysis should not go through the lookup API. The warehouse or a query engine that reads the archive files directly, such as Trino or Athena over Parquet, serves that need. The archive lookup API is built for one order at a time.

8Conclusion

In this answer, we saw how to split orders by temperature. Recent orders stay in a monthly partitioned PostgreSQL database, and a monthly job moves partitions that are past the active window into cheap archive storage as self-contained, versioned documents. Customer service reaches them through one lookup API that falls back to the archive with an agreed slower latency, scheduled tests prove that lookups and restores still work, and lifecycle rules delete orders once the retention period ends.

What to say in the interviewAbout 80 seconds aloud

Open with

I would keep hot orders, for example the last 24 months, in the operational PostgreSQL database, partitioned by month so the 90 percent of queries on recent orders only touch a few partitions.

Then, if they want more

Orders become effectively immutable once returns and refunds close, so each month a job takes the partition that has aged out, writes each order with its lines and payments as one self-contained document in object storage, verifies the counts, and then detaches and drops the partition.

A small index in PostgreSQL maps order id and customer id to the archive location. Customer service uses one order lookup API that tries the live database and falls back to the archive, and we agree that the archive can take a second or two.

Each archived document carries a schema version and the values from the time of the order, such as product names and prices, so later schema changes never require rewriting the archive; the reader has a decoder per version.

A daily job reads random archived orders through the same API and a quarterly test restores a whole month, so we know the archive works. Finally, lifecycle rules delete orders after the ten year retention period, except those under legal hold.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design archiving for an orders database where 90 percent of queries touch the last 3...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/50bb9c8958/