Course contentsShow
← System Design
Lesson 249 of 1,9197. Database Scaling: ShardingFree lesson

Time-Based Sharding

Partition data by time periods (e.g., monthly shards) for time-series workloads and efficient archival.

Time-Based Sharding

What you'll learn: How to partition data by time periods to efficiently manage time-series workloads and simplify archival.

The Core Idea

Time-based sharding splits your database into separate shards based on time windows—each shard holds data from a specific time period (daily, weekly, monthly, yearly, etc.). This is particularly powerful for workloads where data has a natural temporal dimension and queries typically focus on recent time ranges.

Think of it like organizing old tax documents: you don't throw 2024's receipts into the same folder as 1998's—you create separate folders by year. When you need last month's data, you know exactly which folder to check without rifling through decades of paperwork.

How It Works

Your shard key is a timestamp or date field. You create shards like:

  • users_2024_01 for January 2024 data
  • users_2024_02 for February 2024 data
  • orders_2024_Q1 for Q1 2024 orders

Queries targeting a specific time range hit only the relevant shard(s). "Show me orders from last week" goes to one shard, not the entire dataset.

Why This Matters

For time-series data (logs, metrics, sensor readings, financial transactions), most queries focus on recent data. Time-based sharding ensures:

  • Hot data (recent) stays in fast, actively-used shards
  • Cold data (old) lives in older shards you can archive or compress
  • Predictable growth: new shards appear on schedule
  • Simple deletion: drop an entire old shard instead of deleting millions of rows

Archival becomes trivial: move logs_2023_06 to cold storage without touching active shards.

Key Takeaway:
Time-based sharding organizes data by time periods, making queries on recent data fast and enabling effortless archival of old data—ideal for logs, metrics, and any workload where time naturally segments your data.