Design a Photo Upload and Viewing Component

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-11-04

Problem Statement

Designing a Photo Upload and Viewing Component that allows users to upload, store, and view images (like profile pictures, gallery uploads, or post photos) in a web or mobile application.

The system should support efficient upload handling, image transformations (compression, resizing, thumbnails), secure storage, and fast retrieval for display.

🎯 Objective


Build a scalable, fault-tolerant, and performant photo management system that supports:


β€’High-volume concurrent uploads
β€’Fast retrieval and viewing
β€’Secure and durable image storage
β€’Optimized delivery via CDN and caching


βœ… Functional Requirements


1. Photo Upload


β€’Users can upload one or more images through web/mobile apps.
β€’Support various file formats (JPEG, PNG, WEBP, HEIC).
β€’Enforce file size and dimension limits.
β€’Show upload progress to users.


2. Image Processing


β€’Once uploaded, images should be:
β€’Validated (type, size, malicious content).
β€’Compressed to optimize storage.
β€’Resized into multiple resolutions (e.g., thumbnail, medium, full).
β€’Watermarked (optional, for copyrighted content).
β€’Processing should happen asynchronously to maintain responsiveness.


3. Storage


β€’Store the original and processed images in cloud object storage (e.g., AWS S3, Azure Blob, GCS).
β€’Maintain metadata (owner, upload time, resolution variants, privacy settings) in a database.
β€’Support private/public access modes.


4. Photo Retrieval & Viewing


β€’Retrieve and display photos in different sizes based on device (mobile, web, high-res screen).
β€’Cache frequently accessed images using CDN or edge caching.
β€’Provide fast-loading photo galleries with lazy loading/pagination.


5. Access Control


β€’Only the photo owner (or authorized users) can view or modify private photos.
β€’Public photos can be accessed via signed or time-limited URLs.


6. Deletion and Lifecycle


β€’Users can delete uploaded images.
β€’Expired or unused images can be automatically purged after a defined retention period.


7. Analytics (Optional)


β€’Track view count, likes, or engagement metrics per photo.


βš™οΈ Non-Functional Requirements


| Category | Description |

| --------------------- | -------------------------------------------------------------------- |

| **Scalability** | Support millions of uploads and billions of views. |

| **Availability** | 99.9% uptime for image retrieval. |

| **Durability** | Store photos reliably with redundancy (e.g., 11 nines on S3). |

| **Performance** | Upload and retrieval latency < 300ms via CDN. |

| **Security** | Encrypted uploads, signed URLs, and access control. |

| **Cost Optimization** | Use tiered storage (standard, infrequent access, archival). |

| **Extensibility** | Allow integration with tagging, album creation, or sharing features. |


🧠 Key Components to Design

1. Upload Service

β€’Handles client uploads and validates files.
β€’Generates upload tokens (presigned URLs) for direct uploads to object storage.
β€’Stores metadata in database once upload completes.

2. Image Processing Service

β€’Consumes uploaded image events from a message queue.
β€’Performs compression, resizing, and watermarking.
β€’Uploads processed images to storage and updates metadata.
β€’Can be built using serverless compute (e.g., AWS Lambda, Azure Functions).

3. Storage Service

β€’Stores raw and processed images in object storage (S3, Blob, etc.).
β€’Uses folder structure like:
1 2/user_id/ 3 original/ 4 thumbnail/ 5 medium/ 6 full/
β€’Applies lifecycle rules for data retention.

4. Metadata Service

β€’Stores image metadata (owner, upload time, resolutions, tags, permissions).
β€’Backed by a relational or document database.

5. CDN & Caching Layer

β€’Distribute images globally for fast access.
β€’Cache popular or recently viewed images.

6. View Service / API

β€’Exposes APIs for:
β€’Fetching user galleries
β€’Getting signed URLs for images
β€’Retrieving specific image variants
β€’Implements rate limiting and caching.

7. Notification Service (Optional)

β€’Notifies user when image processing completes.

🧱 High-Level Architecture

1 2 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 3 β”‚ Web / Mobile App β”‚ 4 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 5 β”‚ 6 β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 7 β”‚ API Gateway β”‚ 8 β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 9 β”‚ 10 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 11 β”‚ β”‚ β”‚ 12 β”‚ Upload Service View Service 13 β”‚ β”‚ β”‚ 14 β”‚ Image Processing Service Metadata Service 15 β”‚ β”‚ β”‚ 16 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 17 β”‚ 18 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 19 β”‚ Message Queue (MQ) β”‚ 20 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 21 β”‚ 22 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 23 β”‚ Object Storage + β”‚ 24 β”‚ CDN Distribution β”‚ 25 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’Ύ Data Model Example

Photo Table

| Field | Type | Description |

| ------------- | --------- | ---------------------------------- |

| photo_id | UUID | Unique photo identifier |

| user_id | UUID | Owner reference |

| url_original | String | Link to original photo |

| url_thumbnail | String | Thumbnail variant |

| url_medium | String | Medium-size variant |

| visibility | Enum | PUBLIC / PRIVATE |

| status | Enum | UPLOADING / PROCESSING / AVAILABLE |

| created_at | Timestamp | Upload time |

| updated_at | Timestamp | Last updated |


⚑ Scalability & Performance Considerations


β€’Use presigned URLs for direct S3 uploads (reduces server load).
β€’Process images asynchronously via queue + worker pattern.
β€’Use Redis cache for popular photo metadata.
β€’Serve all image retrievals through CDN for low latency.
β€’Enable lazy loading and pagination for galleries.


πŸ” Security Considerations


β€’Use JWT/OAuth for user authentication.
β€’Apply signed URLs for private photo access.
β€’Use malware scanning for uploaded files.
β€’Encrypt images at rest (S3 SSE) and in transit (HTTPS).

⚠️ Failure Scenarios


| Failure | Mitigation |

| ------------------------ | ---------------------------------------------- |

| Upload interrupted | Resume upload using multipart upload |

| Image processing failure | Retry via queue with exponential backoff |

| CDN cache miss | Fetch from object storage and refresh cache |

| Unauthorized access | Validate JWT + enforce ACL before serving URLs |


βœ… Expected Outcome

A highly scalable, secure, and responsive Photo Upload and Viewing Component that:

β€’Handles massive image uploads efficiently
β€’Delivers images quickly via CDN
β€’Supports multiple sizes and formats
β€’Ensures privacy, durability, and extensibility


Solution

Comments