AI Saree

AI Saree

Leader posted 3 min read

The Opportunity

In September 2024, Google's Gemini Nano Banana AI sparked a social media phenomenon. Within one week, the "AI Saree" trend exploded across Instagram, with over 100,000 creators transforming selfies into 90s Bollywood portraits. While the trend was hot, the existing tools had serious limitations: slow generation times, poor user experience during processing, and no way to handle viral traffic spikes.

We saw an opportunity to build something better.

The Problem We Solved

Most AI image generators follow a synchronous request pattern. Users submit a photo, the server processes it, and everyone waits. When generation takes 30 to 60 seconds, this creates terrible user experience. When thousands of users hit your service simultaneously, it creates server meltdowns.

The existing Nano Banana tool crashed repeatedly during peak viral moments. Users faced timeout errors, lost uploads, and frozen interfaces. We knew there had to be a better way.

Our Technical Solution

We built AI Saree as an asynchronous, event-driven system designed for viral scale from day one.

Architecture Overview

Our stack leverages Cloudflare's edge infrastructure with Next.js 15 for maximum performance:

User Upload → Cloudflare Pages → Replicate AI Prediction → KV Status Store → R2 Image Storage
     ↓                                      ↓                      ↓              ↓
Frontend Polling ← Webhook Updates ← Processing Status ← Completed Image

Core Components

Frontend: Next.js 15 with React 19, TypeScript, and Tailwind CSS v4. We use SWR for intelligent polling with exponential backoff (1s → 2s → 5s → 10s) to minimize unnecessary requests while keeping users informed.

Backend: Cloudflare Pages with OpenNext adapter. API routes handle submission, status checking, and webhook callbacks. We validate inputs server-side and return request IDs immediately.

AI Processing: Replicate's Flux model for high-quality generation. Instead of blocking on API calls, we create predictions and let Replicate handle queuing and processing asynchronously.

Storage Strategy:

  • Cloudflare KV for temporary prediction status (1-hour TTL)
  • Cloudflare R2 for persistent image storage
  • Cloudflare D1 (SQLite) for user data and credit transactions

Rate Limiting: Custom Cloudflare Worker with Durable Objects prevents abuse while allowing burst traffic from legitimate viral moments.

Performance Optimizations

Non-blocking UI: Users get instant feedback with progress indicators. The interface remains fully responsive during generation. They can navigate away and return later.

Smart Caching: Generated images cache at the edge. Popular styles pre-warm the cache. Repeat requests serve instantly.

Database Design: Drizzle ORM with carefully indexed tables. User credits, works, and transactions tracked efficiently without bottlenecks.

Error Recovery: Automatic retry on transient failures. Webhook fallback when polling times out. Graceful degradation when services partially fail.

Results and Metrics

Since launching, we've achieved:

  • Generation Speed: 8 to 12 seconds average (vs 30 to 60 seconds for competitors)
  • Uptime: 99.9% availability during viral traffic spikes
  • Scale: Handled 50,000+ generations in first week without degradation
  • User Retention: 40% of users return within 48 hours
  • Conversion: 12% free to paid conversion rate

Technical performance:

  • Lighthouse scores: 95+ across all metrics
  • Time to First Byte: <200ms globally via Cloudflare edge
  • API response time: p99 under 500ms
  • Zero data loss incidents despite viral growth

Challenges and Solutions

Challenge 1: Webhook Reliability

Replicate webhooks occasionally fail or arrive out of order. Solution: Implement idempotent handlers with request ID validation. Fall back to polling when webhooks don't arrive within expected timeframe.

Challenge 2: Credit System Race Conditions

Concurrent requests could double-spend credits. Solution: Implement optimistic locking in D1 with transaction isolation. Credits deduct atomically before generation starts.

Challenge 3: Image Storage Costs

R2 storage filled quickly with viral growth. Solution: Implement intelligent cleanup for anonymous users (7-day retention). Compress images without visible quality loss. Deduplicate identical generations via content hashing.

Challenge 4: Prompt Injection

Users attempted to break the AI with malicious prompts. Solution: Server-side prompt sanitization and validation. Predefined style templates that users customize rather than freeform input.

Technical Learnings

Async First: Never make users wait for AI. Return immediately with a tracking ID and handle everything else asynchronously.

Edge Computing Works: Cloudflare's global network made our service fast everywhere. Indian users get sub-second response times despite servers in the US.

Simplicity Scales: We avoided complex orchestration. Simple queues, simple storage, simple retry logic. It just works.

Monitor Everything: We track generation times, error rates, and user flows. Debugging viral scale issues requires comprehensive observability.

Open Source Contributions

While our core service remains proprietary, we've open-sourced several components:

  • Cloudflare Worker rate limiter with Durable Objects
  • Next.js webhook handler for Replicate
  • SWR polling hook with exponential backoff
  • R2 image optimization pipeline

What's Next

We're expanding beyond sarees to other cultural fashion styles. Our architecture generalizes well to any async AI generation task.

Upcoming features:

  • Video generation for Instagram Reels
  • Batch processing for multiple styles
  • API for developers to build on our platform
  • WhatsApp bot for emerging markets

Try It Yourself

Experience the speed at AI Saree. No login required for your first generations.


Built with Next.js 15 and Cloudflare Workers. Serving 2,000+ creators and growing.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Great job on creating such a fast and fun AI Saree experience. What made you choose Cloudflare over other platforms for handling viral traffic?

More Posts

Building Credit Systems and User Management for AI Applications

horushe - Sep 21

AI Polaroid Generator

horushe - Sep 23

Intelligent Prediction State Management: Building Scalable Storage Abstractions for AI Applications

horushe - Sep 10

Fastjrsy

horushe - Aug 29

Cloudflare accidentally DDoSed themselves because of a bad React useEffect

Sibasish Mohanty - Sep 15
chevron_left