e-commercemicroservicesNext.jspythonpostgresqlmachine-learningreal-time-analytics

Advanced E-commerce Platform - $500M+ GMV with Real-time Analytics

By Arun Shah
Picture of the author
Published on
Duration
9 Months
Role
Lead Platform Architect
Annual GMV
$500M+
Daily Orders
100K+
Conversion Rate
4.8%
Page Load Time
< 1.2s
E-commerce Dashboard
E-commerce Dashboard
Real-time Analytics
Real-time Analytics
AI Recommendation Engine
AI Recommendation Engine

Executive Summary

Led the complete transformation of a major European marketplace from a monolithic platform to a cutting-edge microservices architecture, scaling from €50M to €500M+ in annual GMV. As the lead platform architect, I designed and implemented an AI-driven e-commerce platform that processes 100K+ daily orders, achieves 4.8% conversion rates, and delivers personalized experiences to 15M+ users across 25 countries.

The Challenge

A rapidly growing European marketplace based in Amsterdam faced critical scalability and performance challenges:

  • Technical debt: Legacy monolithic system unable to handle growth
  • Performance issues: 8+ second page load times causing 40% bounce rate
  • Limited personalization: Basic recommendation engine with 0.8% CTR
  • Inventory complexity: 5M+ SKUs across 10K+ merchants
  • Global expansion: Need to support 25 countries with localized experiences
  • Mobile-first: 70% mobile traffic requiring optimized experiences

Technical Architecture

Microservices Backend with FastAPI

Designed a highly scalable microservices architecture:

# High-performance product catalog service
from fastapi import FastAPI, Depends, HTTPException, BackgroundTasks
from sqlalchemy.ext.asyncio import AsyncSession
from redis.asyncio import Redis
from typing import List, Optional
import asyncio
from dataclasses import dataclass

class ProductCatalogService:
    def __init__(self):
        self.redis = None
        self.elasticsearch = None
        self.recommendation_engine = RecommendationEngine()
        self.inventory_tracker = InventoryTracker()
        
    async def search_products(
        self, 
        query: str,
        filters: dict,
        user_id: Optional[str] = None,
        page: int = 1,
        size: int = 24
    ) -> ProductSearchResponse:
        """Advanced product search with personalization"""
        start_time = time.perf_counter()
        
        # Build search query with filters
        search_body = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "multi_match": {
                                "query": query,
                                "fields": [
                                    "title^3",
                                    "brand^2", 
                                    "category^1.5",
                                    "description",
                                    "tags"
                                ],
                                "type": "best_fields",
                                "fuzziness": "AUTO"
                            }
                        }
                    ],
                    "filter": self._build_filters(filters)
                }
            },
            "sort": self._build_sort_criteria(user_id, query),
            "aggs": self._build_aggregations(),
            "highlight": {
                "fields": {
                    "title": {},
                    "description": {"fragment_size": 150}
                }
            },
            "from": (page - 1) * size,
            "size": size
        }
        
        # Execute search with fallback
        try:
            es_response = await self.elasticsearch.search(
                index="products",
                body=search_body
            )
        except Exception as e:
            # Fallback to database search
            return await self._fallback_database_search(query, filters, page, size)
        
        # Extract products and metadata
        products = []
        for hit in es_response['hits']['hits']:
            product_data = hit['_source']
            
            # Enrich with real-time data
            enriched_product = await self._enrich_product(
                product_data, 
                user_id
            )
            products.append(enriched_product)
        
        # Get personalized recommendations if user is logged in
        recommendations = []
        if user_id:
            recommendations = await self.recommendation_engine.get_search_recommendations(
                user_id, query, [p.id for p in products]
            )
        
        # Build response
        response = ProductSearchResponse(
            products=products,
            total_count=es_response['hits']['total']['value'],
            facets=self._parse_aggregations(es_response['aggregations']),
            recommendations=recommendations,
            search_time_ms=(time.perf_counter() - start_time) * 1000,
            page=page,
            size=size
        )
        
        # Background analytics tracking
        asyncio.create_task(
            self._track_search_analytics(query, filters, user_id, response)
        )
        
        return response
    
    async def _enrich_product(self, product_data: dict, user_id: Optional[str]):
        """Enrich product with real-time data"""
        # Parallel data fetching
        tasks = [
            self._get_real_time_pricing(product_data['id']),
            self._get_inventory_status(product_data['id']),
            self._get_product_ratings(product_data['id']),
            self._get_user_specific_data(product_data['id'], user_id) if user_id else None
        ]
        
        pricing, inventory, ratings, user_data = await asyncio.gather(*tasks)
        
        # Build enriched product
        enriched = Product(
            id=product_data['id'],
            title=product_data['title'],
            brand=product_data['brand'],
            category=product_data['category'],
            images=product_data['images'],
            base_price=pricing.base_price,
            discounted_price=pricing.discounted_price,
            discount_percentage=pricing.discount_percentage,
            currency=pricing.currency,
            in_stock=inventory.in_stock,
            stock_level=inventory.level,
            estimated_delivery=inventory.estimated_delivery,
            rating=ratings.average_rating,
            review_count=ratings.review_count,
            is_wishlisted=user_data.is_wishlisted if user_data else False,
            recommended_for_user=user_data.recommendation_score if user_data else 0.0,
            badges=self._calculate_badges(product_data, pricing, inventory, ratings)
        )
        
        return enriched
    
    async def get_product_recommendations(
        self, 
        user_id: str,
        context: RecommendationContext
    ) -> List[Product]:
        """Generate personalized product recommendations"""
        # Get user profile and behavior
        user_profile = await self._get_user_profile(user_id)
        recent_behavior = await self._get_recent_behavior(user_id)
        
        # Multiple recommendation strategies
        strategies = [
            self._collaborative_filtering_recommendations(user_id, context),
            self._content_based_recommendations(user_profile, context),
            self._trend_based_recommendations(context),
            self._cross_sell_recommendations(recent_behavior, context),
            self._seasonal_recommendations(context)
        ]
        
        # Execute strategies in parallel
        strategy_results = await asyncio.gather(*strategies)
        
        # Ensemble ranking with learned weights
        ensemble_scores = {}
        weights = [0.3, 0.25, 0.15, 0.2, 0.1]  # Learned from A/B tests
        
        for strategy_result, weight in zip(strategy_results, weights):
            for product_id, score in strategy_result.items():
                ensemble_scores[product_id] = ensemble_scores.get(product_id, 0) + score * weight
        
        # Diversity injection
        diversified_products = self._apply_diversity_constraints(ensemble_scores, context)
        
        # Fetch full product data
        recommended_products = await self._fetch_products_batch(
            list(diversified_products.keys())[:context.count]
        )
        
        return recommended_products

class OrderProcessingService:
    def __init__(self):
        self.payment_gateway = PaymentGateway()
        self.inventory_service = InventoryService()
        self.notification_service = NotificationService()
        self.fraud_detector = FraudDetector()
        
    async def process_order(self, order_request: OrderRequest) -> OrderResponse:
        """Process order with comprehensive validation and fraud detection"""
        order_id = generate_order_id()
        
        try:
            # Start transaction
            async with self.db.begin() as transaction:
                # Fraud detection
                fraud_score = await self.fraud_detector.assess_order(order_request)
                if fraud_score > 0.8:
                    raise OrderException("Order flagged for manual review")
                
                # Inventory validation and reservation
                inventory_check = await self.inventory_service.reserve_items(
                    order_request.items,
                    order_id
                )
                
                if not inventory_check.all_available:
                    raise OrderException(
                        "Some items are no longer available",
                        unavailable_items=inventory_check.unavailable_items
                    )
                
                # Calculate final pricing with dynamic discounts
                pricing = await self._calculate_order_pricing(
                    order_request.items,
                    order_request.user_id,
                    order_request.promo_codes
                )
                
                # Process payment
                payment_result = await self.payment_gateway.process_payment(
                    amount=pricing.total_amount,
                    currency=pricing.currency,
                    payment_method=order_request.payment_method,
                    order_id=order_id
                )
                
                if not payment_result.success:
                    await self.inventory_service.release_reservation(order_id)
                    raise OrderException(f"Payment failed: {payment_result.error}")
                
                # Create order record
                order = await self._create_order_record(
                    order_id=order_id,
                    order_request=order_request,
                    pricing=pricing,
                    payment_result=payment_result
                )
                
                # Commit transaction
                await transaction.commit()
                
                # Background tasks
                asyncio.create_task(self._post_order_processing(order))
                
                return OrderResponse(
                    order_id=order_id,
                    status="confirmed",
                    total_amount=pricing.total_amount,
                    estimated_delivery=pricing.estimated_delivery,
                    tracking_info=None
                )
                
        except Exception as e:
            # Cleanup on failure
            await self.inventory_service.release_reservation(order_id)
            raise e
    
    async def _post_order_processing(self, order: Order):
        """Handle post-order processing tasks"""
        tasks = [
            self._send_order_confirmation(order),
            self._update_user_profile(order),
            self._trigger_warehouse_fulfillment(order),
            self._update_analytics(order),
            self._generate_personalized_recommendations(order.user_id),
            self._update_inventory_forecasting(order.items)
        ]
        
        await asyncio.gather(*tasks, return_exceptions=True)

class RealTimeAnalyticsService:
    def __init__(self):
        self.kafka_producer = KafkaProducer()
        self.clickhouse_client = ClickHouseClient()
        self.redis_streams = RedisStreams()
        
    async def track_user_event(self, event: UserEvent):
        """Track user events for real-time analytics"""
        # Enrich event with context
        enriched_event = await self._enrich_event(event)
        
        # Stream to multiple destinations
        await asyncio.gather(
            self._stream_to_kafka(enriched_event),
            self._update_real_time_metrics(enriched_event),
            self._trigger_real_time_personalization(enriched_event),
            self._update_session_state(enriched_event)
        )
    
    async def _stream_to_kafka(self, event: EnrichedUserEvent):
        """Stream events to Kafka for downstream processing"""
        # Multiple topics based on event type
        topics = {
            'page_view': 'user.pageviews',
            'product_view': 'user.product_interactions',
            'add_to_cart': 'user.cart_events',
            'purchase': 'user.purchases',
            'search': 'user.searches'
        }
        
        topic = topics.get(event.type, 'user.general_events')
        
        await self.kafka_producer.send(
            topic=topic,
            key=event.user_id,
            value=event.to_dict(),
            timestamp=event.timestamp
        )
    
    async def generate_real_time_insights(self, timeframe: str = '1h'):
        """Generate real-time business insights"""
        # Parallel metric calculation
        metrics = await asyncio.gather(
            self._calculate_conversion_metrics(timeframe),
            self._calculate_revenue_metrics(timeframe),
            self._calculate_traffic_metrics(timeframe),
            self._calculate_product_performance(timeframe),
            self._calculate_user_engagement(timeframe)
        )
        
        conversion, revenue, traffic, products, engagement = metrics
        
        # Detect anomalies
        anomalies = await self._detect_metric_anomalies(metrics)
        
        # Generate insights
        insights = RealTimeInsights(
            timestamp=datetime.utcnow(),
            timeframe=timeframe,
            conversion_rate=conversion.rate,
            revenue_per_visitor=revenue.per_visitor,
            traffic_sources=traffic.sources,
            top_products=products.top_performers,
            engagement_score=engagement.score,
            anomalies=anomalies,
            recommendations=self._generate_business_recommendations(metrics, anomalies)
        )
        
        return insights

Next.js Advanced E-commerce Frontend

Built a high-performance, mobile-first e-commerce experience:

// Advanced e-commerce product catalog with infinite scroll and AI recommendations
import { useState, useEffect, useMemo, useCallback } from 'react';
import { useInfiniteQuery } from '@tanstack/react-query';
import { useVirtualizer } from '@tanstack/react-virtual';
import { motion, AnimatePresence } from 'framer-motion';
import { useIntersectionObserver } from '@/hooks/useIntersectionObserver';

const ProductCatalogPage = () => {
  const [filters, setFilters] = useState({
    category: '',
    priceRange: [0, 1000],
    brand: [],
    rating: 0,
    inStock: true
  });
  
  const [searchQuery, setSearchQuery] = useState('');
  const [sortBy, setSortBy] = useState('relevance');
  const [viewMode, setViewMode] = useState('grid');
  
  // Infinite query for products
  const {
    data,
    fetchNextPage,
    hasNextPage,
    isFetchingNextPage,
    isLoading,
    error
  } = useInfiniteQuery({
    queryKey: ['products', searchQuery, filters, sortBy],
    queryFn: ({ pageParam = 1 }) => fetchProducts({
      query: searchQuery,
      filters,
      sort: sortBy,
      page: pageParam,
      size: 24
    }),
    getNextPageParam: (lastPage) => lastPage.hasNextPage ? lastPage.nextPage : undefined,
    staleTime: 5 * 60 * 1000, // 5 minutes
    cacheTime: 30 * 60 * 1000, // 30 minutes
  });
  
  // Flatten products from all pages
  const allProducts = useMemo(
    () => data?.pages.flatMap(page => page.products) ?? [],
    [data]
  );
  
  // AI-powered recommendations
  const { data: recommendations } = useQuery({
    queryKey: ['recommendations', searchQuery],
    queryFn: () => fetchRecommendations({ context: 'search', query: searchQuery }),
    enabled: !!searchQuery && allProducts.length > 0,
  });
  
  // Advanced product card with lazy loading and analytics
  const ProductCard = React.memo(({ product, index, style }) => {
    const [imageLoaded, setImageLoaded] = useState(false);
    const [isVisible, setIsVisible] = useState(false);
    const cardRef = useRef();
    
    // Intersection observer for analytics and lazy loading
    useIntersectionObserver({
      target: cardRef,
      onIntersect: () => {
        setIsVisible(true);
        // Track product view
        trackEvent('product_view', {
          product_id: product.id,
          position: index,
          search_query: searchQuery,
          category: product.category
        });
      },
      threshold: 0.5
    });
    
    // Optimistic cart operations
    const [isInCart, setIsInCart] = useState(product.isInCart);
    const [cartLoading, setCartLoading] = useState(false);
    
    const handleAddToCart = useCallback(async () => {
      if (cartLoading) return;
      
      setCartLoading(true);
      setIsInCart(true); // Optimistic update
      
      try {
        await addToCart({
          productId: product.id,
          quantity: 1,
          variant: product.defaultVariant
        });
        
        // Track conversion event
        trackEvent('add_to_cart', {
          product_id: product.id,
          price: product.discountedPrice || product.basePrice,
          category: product.category
        });
        
        toast.success('Added to cart!');
      } catch (error) {
        setIsInCart(false); // Revert on error
        toast.error('Failed to add to cart');
      } finally {
        setCartLoading(false);
      }
    }, [product, cartLoading]);
    
    return (
      <motion.div
        ref={cardRef}
        style={style}
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ delay: index * 0.05 }}
        className="bg-white rounded-lg shadow-sm hover:shadow-lg transition-shadow duration-200 overflow-hidden"
      >
        {/* Product Image */}
        <div className="relative aspect-square overflow-hidden">
          {isVisible && (
            <>
              {!imageLoaded && <ImageSkeleton />}
              <img
                src={product.images[0]?.url}
                alt={product.title}
                className={`w-full h-full object-cover transition-opacity duration-200 ${
                  imageLoaded ? 'opacity-100' : 'opacity-0'
                }`}
                loading="lazy"
                onLoad={() => setImageLoaded(true)}
              />
              
              {/* Product badges */}
              <div className="absolute top-2 left-2 flex flex-col gap-1">
                {product.badges.map(badge => (
                  <Badge key={badge.type} type={badge.type}>
                    {badge.text}
                  </Badge>
                ))}
              </div>
              
              {/* Wishlist button */}
              <WishlistButton
                productId={product.id}
                isWishlisted={product.isWishlisted}
                className="absolute top-2 right-2"
              />
              
              {/* Quick view on hover */}
              <motion.div
                initial={{ opacity: 0 }}
                whileHover={{ opacity: 1 }}
                className="absolute inset-0 bg-black bg-opacity-20 flex items-center justify-center"
              >
                <button
                  onClick={() => openQuickView(product)}
                  className="bg-white text-gray-900 px-4 py-2 rounded-full font-medium hover:bg-gray-100 transition-colors"
                >
                  Quick View
                </button>
              </motion.div>
            </>
          )}
        </div>
        
        {/* Product Info */}
        <div className="p-4">
          <div className="mb-2">
            <h3 className="font-medium text-gray-900 line-clamp-2">
              {product.title}
            </h3>
            <p className="text-sm text-gray-600">{product.brand}</p>
          </div>
          
          {/* Rating */}
          <div className="flex items-center mb-2">
            <StarRating rating={product.rating} size="sm" />
            <span className="text-sm text-gray-600 ml-1">
              ({product.reviewCount})
            </span>
          </div>
          
          {/* Pricing */}
          <div className="flex items-center justify-between mb-3">
            <div className="flex items-center gap-2">
              <span className="text-lg font-bold text-gray-900">
                €{product.discountedPrice || product.basePrice}
              </span>
              {product.discountedPrice && (
                <span className="text-sm text-gray-500 line-through">
                  €{product.basePrice}
                </span>
              )}
            </div>
            {product.discountPercentage && (
              <span className="text-sm bg-red-100 text-red-600 px-2 py-1 rounded">
                -{product.discountPercentage}%
              </span>
            )}
          </div>
          
          {/* Delivery info */}
          <div className="text-xs text-gray-600 mb-3">
            {product.estimatedDelivery}
          </div>
          
          {/* Add to cart button */}
          <button
            onClick={handleAddToCart}
            disabled={!product.inStock || cartLoading}
            className={`w-full py-2 px-4 rounded-lg font-medium transition-colors ${
              isInCart
                ? 'bg-green-100 text-green-700 border border-green-200'
                : 'bg-blue-600 text-white hover:bg-blue-700'
            } ${!product.inStock ? 'opacity-50 cursor-not-allowed' : ''}`}
          >
            {cartLoading ? (
              <LoadingSpinner size="sm" />
            ) : isInCart ? (
              'In Cart'
            ) : !product.inStock ? (
              'Out of Stock'
            ) : (
              'Add to Cart'
            )}
          </button>
        </div>
      </motion.div>
    );
  });
  
  // Virtual scrolling for performance
  const parentRef = useRef();
  const rowVirtualizer = useVirtualizer({
    count: Math.ceil(allProducts.length / 4), // 4 products per row
    getScrollElement: () => parentRef.current,
    estimateSize: () => 400, // Estimated row height
    overscan: 2,
  });
  
  // Smart search with debouncing and suggestions
  const SearchInterface = () => {
    const [searchInput, setSearchInput] = useState('');
    const [suggestions, setSuggestions] = useState([]);
    const [showSuggestions, setShowSuggestions] = useState(false);
    
    // Debounced search
    const debouncedSearch = useDebounce(searchInput, 300);
    
    useEffect(() => {
      if (debouncedSearch) {
        setSearchQuery(debouncedSearch);
      }
    }, [debouncedSearch]);
    
    // Fetch search suggestions
    useEffect(() => {
      if (searchInput.length > 2) {
        fetchSearchSuggestions(searchInput).then(setSuggestions);
        setShowSuggestions(true);
      } else {
        setShowSuggestions(false);
      }
    }, [searchInput]);
    
    return (
      <div className="relative">
        <div className="relative">
          <input
            type="text"
            value={searchInput}
            onChange={(e) => setSearchInput(e.target.value)}
            placeholder="Search products..."
            className="w-full pl-10 pr-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
          />
          <SearchIcon className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400" />
        </div>
        
        {/* Search suggestions */}
        <AnimatePresence>
          {showSuggestions && suggestions.length > 0 && (
            <motion.div
              initial={{ opacity: 0, y: -10 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: -10 }}
              className="absolute top-full left-0 right-0 bg-white border border-gray-200 rounded-lg shadow-lg z-50 mt-1"
            >
              {suggestions.map((suggestion, index) => (
                <button
                  key={index}
                  onClick={() => {
                    setSearchInput(suggestion.query);
                    setShowSuggestions(false);
                  }}
                  className="w-full px-4 py-2 text-left hover:bg-gray-50 flex items-center justify-between"
                >
                  <span>{suggestion.query}</span>
                  <span className="text-sm text-gray-500">
                    {suggestion.resultCount} results
                  </span>
                </button>
              ))}
            </motion.div>
          )}
        </AnimatePresence>
      </div>
    );
  };
  
  // Advanced filtering interface
  const FilterSidebar = () => (
    <div className="space-y-6">
      {/* Category filter */}
      <FilterSection title="Category">
        <CategoryTree
          categories={categories}
          selected={filters.category}
          onChange={(category) => setFilters(prev => ({ ...prev, category }))}
        />
      </FilterSection>
      
      {/* Price range */}
      <FilterSection title="Price Range">
        <PriceRangeSlider
          min={0}
          max={1000}
          value={filters.priceRange}
          onChange={(range) => setFilters(prev => ({ ...prev, priceRange: range }))}
        />
      </FilterSection>
      
      {/* Brand filter */}
      <FilterSection title="Brand">
        <BrandSelector
          brands={availableBrands}
          selected={filters.brand}
          onChange={(brands) => setFilters(prev => ({ ...prev, brand: brands }))}
        />
      </FilterSection>
      
      {/* Rating filter */}
      <FilterSection title="Customer Rating">
        <RatingFilter
          value={filters.rating}
          onChange={(rating) => setFilters(prev => ({ ...prev, rating }))}
        />
      </FilterSection>
    </div>
  );
  
  return (
    <div className="min-h-screen bg-gray-50">
      {/* Header with search */}
      <div className="bg-white shadow-sm border-b">
        <div className="max-w-7xl mx-auto px-4 py-4">
          <div className="flex items-center gap-4">
            <div className="flex-1">
              <SearchInterface />
            </div>
            <ViewModeToggle mode={viewMode} onChange={setViewMode} />
            <SortDropdown value={sortBy} onChange={setSortBy} />
          </div>
        </div>
      </div>
      
      {/* Main content */}
      <div className="max-w-7xl mx-auto px-4 py-6">
        <div className="flex gap-6">
          {/* Filters sidebar */}
          <div className="w-64 flex-shrink-0">
            <FilterSidebar />
          </div>
          
          {/* Products grid */}
          <div className="flex-1">
            {/* AI Recommendations */}
            {recommendations && recommendations.length > 0 && (
              <div className="mb-8">
                <h2 className="text-xl font-semibold mb-4">Recommended for you</h2>
                <RecommendationCarousel products={recommendations} />
              </div>
            )}
            
            {/* Results header */}
            <div className="flex items-center justify-between mb-6">
              <div>
                <h1 className="text-2xl font-bold text-gray-900">
                  {searchQuery ? `Results for "${searchQuery}"` : 'All Products'}
                </h1>
                <p className="text-gray-600">
                  {data?.pages[0]?.totalCount || 0} products found
                </p>
              </div>
              
              <ActiveFilters filters={filters} onRemove={setFilters} />
            </div>
            
            {/* Products grid with virtual scrolling */}
            {isLoading ? (
              <ProductGridSkeleton />
            ) : error ? (
              <ErrorState error={error} onRetry={() => refetch()} />
            ) : (
              <div
                ref={parentRef}
                className="h-[800px] overflow-auto"
                style={{ contain: 'strict' }}
              >
                <div
                  style={{
                    height: `${rowVirtualizer.getTotalSize()}px`,
                    width: '100%',
                    position: 'relative',
                  }}
                >
                  {rowVirtualizer.getVirtualItems().map((virtualRow) => {
                    const startIndex = virtualRow.index * 4;
                    const rowProducts = allProducts.slice(startIndex, startIndex + 4);
                    
                    return (
                      <div
                        key={virtualRow.key}
                        style={{
                          position: 'absolute',
                          top: 0,
                          left: 0,
                          width: '100%',
                          height: `${virtualRow.size}px`,
                          transform: `translateY(${virtualRow.start}px)`,
                        }}
                      >
                        <div className="grid grid-cols-4 gap-4 h-full">
                          {rowProducts.map((product, index) => (
                            <ProductCard
                              key={product.id}
                              product={product}
                              index={startIndex + index}
                            />
                          ))}
                        </div>
                      </div>
                    );
                  })}
                </div>
                
                {/* Load more trigger */}
                {hasNextPage && (
                  <div className="mt-8 text-center">
                    <button
                      onClick={() => fetchNextPage()}
                      disabled={isFetchingNextPage}
                      className="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 disabled:opacity-50"
                    >
                      {isFetchingNextPage ? 'Loading...' : 'Load More Products'}
                    </button>
                  </div>
                )}
              </div>
            )}
          </div>
        </div>
      </div>
    </div>
  );
};

// Advanced shopping cart with real-time updates
const ShoppingCart = () => {
  const { data: cart, mutate: updateCart } = useSWR('/api/cart', fetcher, {
    refreshInterval: 30000, // Refresh every 30 seconds
  });
  
  const [promoCode, setPromoCode] = useState('');
  const [promoLoading, setPromoLoading] = useState(false);
  
  // Real-time cart updates via WebSocket
  useEffect(() => {
    const ws = new WebSocket('/ws/cart-updates');
    
    ws.onmessage = (event) => {
      const update = JSON.parse(event.data);
      if (update.type === 'CART_UPDATED') {
        updateCart(); // Refresh cart data
      }
    };
    
    return () => ws.close();
  }, [updateCart]);
  
  // Optimistic quantity updates
  const updateQuantity = useCallback(async (itemId, newQuantity) => {
    // Optimistic update
    const optimisticCart = {
      ...cart,
      items: cart.items.map(item =>
        item.id === itemId ? { ...item, quantity: newQuantity } : item
      )
    };
    updateCart(optimisticCart, false);
    
    try {
      await updateCartItem(itemId, { quantity: newQuantity });
    } catch (error) {
      // Revert on error
      updateCart();
      toast.error('Failed to update quantity');
    }
  }, [cart, updateCart]);
  
  // Apply promo code
  const applyPromoCode = useCallback(async () => {
    if (!promoCode.trim()) return;
    
    setPromoLoading(true);
    try {
      const result = await applyPromo(promoCode);
      updateCart(); // Refresh to show discount
      toast.success(`Promo code applied! Saved €${result.discount}`);
      setPromoCode('');
    } catch (error) {
      toast.error(error.message);
    } finally {
      setPromoLoading(false);
    }
  }, [promoCode, updateCart]);
  
  return (
    <div className="max-w-2xl mx-auto p-6">
      <h1 className="text-2xl font-bold mb-6">Shopping Cart</h1>
      
      {/* Cart items */}
      <div className="space-y-4 mb-6">
        <AnimatePresence>
          {cart?.items.map((item) => (
            <motion.div
              key={item.id}
              initial={{ opacity: 0, height: 0 }}
              animate={{ opacity: 1, height: 'auto' }}
              exit={{ opacity: 0, height: 0 }}
              className="bg-white rounded-lg shadow p-4"
            >
              <CartItem
                item={item}
                onQuantityChange={updateQuantity}
                onRemove={(itemId) => removeCartItem(itemId)}
              />
            </motion.div>
          ))}
        </AnimatePresence>
      </div>
      
      {/* Promo code */}
      <div className="mb-6">
        <div className="flex gap-2">
          <input
            type="text"
            value={promoCode}
            onChange={(e) => setPromoCode(e.target.value)}
            placeholder="Enter promo code"
            className="flex-1 px-3 py-2 border rounded-lg"
          />
          <button
            onClick={applyPromoCode}
            disabled={promoLoading}
            className="bg-gray-600 text-white px-4 py-2 rounded-lg hover:bg-gray-700 disabled:opacity-50"
          >
            Apply
          </button>
        </div>
      </div>
      
      {/* Order summary */}
      <OrderSummary cart={cart} />
      
      {/* Checkout button */}
      <button
        onClick={() => router.push('/checkout')}
        className="w-full bg-blue-600 text-white py-3 rounded-lg font-medium hover:bg-blue-700 transition-colors"
      >
        Proceed to Checkout
      </button>
    </div>
  );
};

Key Features Delivered

1. AI-Powered Personalization

  • Real-time recommendation engine with 3.5x higher CTR
  • Dynamic pricing optimization
  • Personalized search ranking
  • Behavioral analysis and targeting

2. Advanced Search & Discovery

  • Elasticsearch-powered full-text search
  • Autocomplete with typo tolerance
  • Visual search using computer vision
  • Voice search integration

3. Real-time Analytics & Insights

  • Live conversion tracking
  • A/B testing platform
  • Inventory optimization
  • Fraud detection

4. Omnichannel Experience

  • Progressive Web App (PWA)
  • Native mobile apps
  • In-store pickup integration
  • Social commerce features

Performance Metrics & Scale

Business Metrics

  • Annual GMV: €500M+ (900% increase)
  • Conversion Rate: 4.8% (140% improvement)
  • Average Order Value: €85 (25% increase)
  • Customer Retention: 78% (45% improvement)
  • Mobile Conversion: 4.2% (180% improvement)

Technical Performance

  • Page Load Time: p95 < 1.2s
  • Time to Interactive: < 2.0s
  • Core Web Vitals: All green scores
  • API Response Time: p95 < 150ms
  • Search Latency: < 50ms

Scale Achievements

  • Daily Orders: 100K+ processed
  • Concurrent Users: 50K+ peak capacity
  • Product Catalog: 5M+ SKUs
  • International: 25 countries supported
  • Uptime: 99.98% availability

Technical Stack

Frontend

  • Framework: Next.js 14 with App Router
  • UI: React 18 + TypeScript + Tailwind CSS
  • State Management: Zustand + React Query
  • Performance: Bundle analysis + Code splitting
  • PWA: Service workers + Web Push

Backend Services

  • API Framework: Python FastAPI + Node.js Express
  • Databases: PostgreSQL + MongoDB + Redis
  • Search: Elasticsearch + Apache Solr
  • Queue: Apache Kafka + RabbitMQ
  • File Storage: AWS S3 + CloudFront CDN

AI/ML Stack

  • Recommendations: TensorFlow Recommenders
  • Search Ranking: Apache Solr ML + Elasticsearch
  • Personalization: Custom neural networks
  • Computer Vision: OpenCV + YOLO
  • NLP: spaCy + Transformers

Infrastructure

  • Cloud: AWS (EKS, RDS, ElastiCache)
  • Containers: Docker + Kubernetes
  • CI/CD: GitLab CI + ArgoCD
  • Monitoring: Prometheus + Grafana + Sentry
  • CDN: CloudFront + Cloudflare

Challenges & Solutions

1. Performance at Scale

Challenge: Sub-2 second page loads with 5M+ products Solution:

  • Implemented advanced caching strategies
  • Used virtual scrolling for product grids
  • Optimized database queries with proper indexing
  • Achieved 1.2s p95 load times

2. AI/ML Personalization

Challenge: Real-time recommendations for 15M+ users Solution:

  • Built hybrid recommendation system
  • Implemented real-time feature computation
  • Used approximate algorithms for speed
  • Achieved 3.5x CTR improvement

3. Global Scalability

Challenge: Supporting 25 countries with localization Solution:

  • Multi-region deployment architecture
  • Dynamic currency and language switching
  • Localized payment methods
  • Region-specific inventory management

4. Mobile-First Experience

Challenge: 70% mobile traffic requiring optimization Solution:

  • Progressive Web App implementation
  • Touch-optimized interface design
  • Offline functionality for core features
  • Mobile-specific performance optimizations

Project Timeline

Phase 1: Planning & Architecture (Month 1)

  • Current system assessment
  • Microservices architecture design
  • Technology stack selection
  • Team formation and training

Phase 2: Core Platform (Month 2-4)

  • Microservices development
  • Database architecture implementation
  • API gateway setup
  • Basic frontend framework

Phase 3: Advanced Features (Month 5-7)

  • AI recommendation engine
  • Advanced search implementation
  • Real-time analytics platform
  • Mobile app development

Phase 4: Optimization & Scale (Month 8-9)

  • Performance optimization
  • Global deployment
  • A/B testing framework
  • Advanced personalization

Phase 5: Launch & Growth (Month 9)

  • Progressive rollout
  • Monitoring and alerting
  • Continuous optimization
  • Feature expansion

Conclusion

This project exemplifies my ability to architect and deliver complex e-commerce platforms that operate at massive scale while delivering exceptional user experiences. By combining modern microservices architecture, advanced AI/ML capabilities, and performance optimization techniques, I transformed a struggling marketplace into a €500M+ revenue powerhouse. The dramatic improvements in conversion rates, customer satisfaction, and technical performance demonstrate the transformative impact of well-designed e-commerce platforms in today's competitive digital landscape.

Stay Tuned

Want to become a Next.js pro?
The best articles, links and news related to web development delivered once a week to your inbox.