🗄️ Database

Data Archival

Strategies for archiving old data to maintain performance.

When to Archive

Data TypeArchive AfterReason
Screenshots90 daysStorage cost
Time logs2 yearsPerformance
Activity logs1 yearStorage & performance
Audit logs3 yearsCompliance
Deleted records90 daysGDPR compliance

Archive Strategy

Diagram; its source is available below
Diagram source
graph LR
    Active[(Active DB)] -->|Archive Job| Archive[(Archive DB)]
    Archive -->|Cold Storage| S3[(S3/Object Storage)]
    Active -->|Query| API
    Archive -->|Historical Reports| API

Implementation

Automated Archival Job

@Cron(CronExpression.EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT)
async archiveOldScreenshots() {
  const cutoff = subDays(new Date(), 90);

  // Move to archive storage
  const old = await this.screenshotRepo.find({
    where: { createdAt: LessThan(cutoff) },
  });

  for (const screenshot of old) {
    await this.archiveStorage.store(screenshot);
    await this.screenshotRepo.softDelete(screenshot.id);
  }
}

Table Partitioning

-- Partition time_log by month
CREATE TABLE time_log (
    id UUID PRIMARY KEY,
    started_at TIMESTAMPTZ NOT NULL,
    employee_id UUID NOT NULL
) PARTITION BY RANGE (started_at);

CREATE TABLE time_log_2025_03 PARTITION OF time_log
    FOR VALUES FROM ('2025-03-01') TO ('2025-04-01');

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey