← Back to all recaps

SnowFlake Unified Quota Service System Design - High Frequency System Design Interview (Similar to Google/Apple/Microsoft Cloud Storage)

1 min read

Problem Description

Your task: Design a quota service that supports multiple independent services (S1, S2, …), all sharing the same quota limit, ensuring consistency, concurrency safety, and accuracy.

Problem Analysis

This is a typical Unified Storage Quota System Design question, similar to the backend model of Google Drive, iCloud, or OneDrive. Multiple services (e.g., Photos, Docs, Mail) share a single quota bucket. The system needs to achieve:

  • ✅ Cross-service total quota consistency
  • ✅ Concurrent writes without double counting or deadlocks
  • ✅ Independent accounting for each service
  • ✅ Client-facing Query and Write APIs
  • ✅ High Concurrency, High Scalability

This question tests your understanding of Distributed Counters, Transactions, Locking Mechanisms, Quota Check Flows, and System Scalability.

Core Algorithm

1. Quota Consumption (with Optimistic Locking)

def consume_quota(user_id: int, service: str, size: int) -> bool:
    """
    Consume quota using optimistic locking for concurrency safety
    """
    max_retries = 3
    
    for attempt in range(max_retries):
        # 1. Read current quota
        quota = db.query(
            "SELECT total_quota, used_quota, version "
            "FROM user_quota WHERE user_id = %s",
            user_id
        )
        
        # 2. Check for overflow
        if quota.used_quota + size > quota.total_quota:
            return False  # Insufficient quota
        
        # 3. Attempt update (Optimistic Lock)
        affected = db.execute(
            "UPDATE user_quota "
            "SET used_quota = used_quota + %s, "
            "    version = version + 1, "
            "    updated_at = NOW() "
            "WHERE user_id = %s AND version = %s",
            size, user_id, quota.version
        )
        
        if affected > 0:
            # 4. Update service-level stats
            db.execute(
                "INSERT INTO service_usage (user_id, service_name, used_quota) "
                "VALUES (%s, %s, %s) "
                "ON DUPLICATE KEY UPDATE used_quota = used_quota + %s",
                user_id, service, size, size
            )
            
            # 5. Log operation
            log_quota_change(user_id, service, 'ADD', size, 
                           quota.used_quota, quota.used_quota + size)
            
            # 6. Update cache
            cache.delete(f"quota:{user_id}")
            
            return True
        
        # Version conflict, retry with backoff
        time.sleep(0.01 * (2 ** attempt))
    
    return False  # Retry failed

Optimization Strategy

1. Caching Strategy

  • Read Cache: Cache quota query results for 60 seconds.
  • Write-Through: Delete cache on quota update.

2. Database Optimization

  • Sharding: Shard by user_id.
  • Read/Write Splitting: Queries to replicas, writes to primary.

3. Concurrency Control

  • Optimistic Locking: Use version field to avoid over-selling.
  • Retry Mechanism: Exponential backoff on conflict.

Summary

Snowflake Quota System Design points:

  1. Concurrency Control: Optimistic vs Pessimistic Locking
  2. Consistency: Transactions, Versioning
  3. Performance: Caching, Sharding, Indexing
  4. Scalability: Horizontal Scaling, Async Processing
  5. Monitoring: Usage Metrics, Anomaly Detection

oavoservice specializes in interview coaching for Snowflake / Google / Amazon, offering OA support and real-time VO assistance. Contact us if you need help.


Need real interview questions? Contact WeChat Coding0201 immediately to get real questions.


联系方式

Email: [email protected] Telegram: @OAVOProxy