← 返回博客列表
Snowflake

SnowFlake Unified Quota Service System Design

2025-11-18

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:

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

2. Database Optimization

3. Concurrency Control

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.