KatInterview

Design a Cloud Storage Quota Accounting Service

System DesignModel answer
HardModel answerSystem Designdistributed-systemsconsistencyconcurrency

Last updated

Design a distributed service that tracks per-user byte consumption, enforces storage limits across concurrent multi-part uploads, and reconciles drift against blob storage.

Background

Cloud storage platforms give each user a finite storage budget — a few gigabytes on a free tier, more on paid plans, and a shared pool for team workspaces. Files are uploaded in chunks over sessions that can last minutes or hours. Multiple uploads and deletions may run in parallel for the same account.

Your task is to design a Storage Quota Accounting and Enforcement Service that reliably tracks how many bytes each account has consumed and blocks uploads that would push usage over the limit.

Functional Requirements

  • Reserve capacity: when a client starts an upload and declares a file size, hold that many bytes against the account's limit so parallel operations cannot collectively exceed it.
  • Commit and release: once an upload finishes successfully, convert the held bytes into permanent usage. If the upload is cancelled, times out, or is abandoned by the client, release the held bytes back to the available balance.
  • Reclaim on deletion: when a file is deleted or moved to trash, reflect the freed space promptly (exact policy — whether trashed files still count — is a design choice you should justify).
  • Drift correction: provide an asynchronous background process that detects and repairs mismatches between the accounting records and the actual objects present in blob storage.

Non-Functional Requirements

  • Latency: the capacity-check-and-reserve step must complete at p99 < 25 ms so upload initiation feels instant.
  • Scale: support 1 billion total accounts; 100 million active per day; peaks of 50,000 upload initiations per second.
  • Consistency: hard quota guarantees are required — small, bounded overages during traffic spikes are acceptable only if you explicitly justify the trade-off.
  • Availability: the quota verification path must target 99.99% uptime; file reads and downloads must never be blocked by quota service degradation.

What Your Answer Should Cover

  1. API surface: endpoints for reserving, committing, releasing, and querying quota; identify which calls must be idempotent and why.
  2. Reservation state machine: the full lifecycle of a multi-part upload reservation, including heartbeats, timeouts, retries, and the case where the actual file size differs from the declared size.
  3. Data model and storage: schema for usage counters, pending reservations, and tier limits; where and what to cache; why the chosen database fits the consistency and latency targets.
  4. Concurrency near the limit: how the system prevents two concurrent uploads from both "seeing" enough free space when only one can fit.
  5. Reconciliation pipeline: architecture for continuously or periodically detecting discrepancies and correcting them without disrupting live traffic.

Address trade-offs at each layer. There is no single correct technology choice, but every choice needs a justification.

Hints

Hint 1

Start by naming the core invariant the system must maintain: committed_bytes + reserved_bytes ≤ limit_bytes. Every design decision should be evaluated against whether it preserves that invariant atomically.

Hint 2

The reservation check-and-decrement must happen in a single atomic operation — never a read followed by a separate write in application code. Think about what database primitive (compare-and-set, conditional update, Lua script) gives you that at the latency target.

Hint 3

Drift between accounting records and blob storage is unavoidable. Think about two complementary layers: a streaming path that catches most discrepancies within seconds, and a daily snapshot diff that catches anything the stream missed.