Skip to content

Latest commit

 

History

History
257 lines (181 loc) · 7.35 KB

File metadata and controls

257 lines (181 loc) · 7.35 KB

🚀 Race Condition Fixes - Deployment Guide

This guide will help you deploy the race condition fixes to your Tronline Bazar application safely.

📋 Pre-Deployment Checklist

  • Backup your current database
  • Test the fixes in a staging environment first
  • Ensure you have admin access to your Supabase project
  • Notify users of potential brief maintenance (optional)

🗃️ Database Migration

Step 1: Run Migration Scripts

Execute these SQL scripts in order in your Supabase SQL Editor:

Part 1: Functions and Columns

-- Copy and paste the content from migration-part1.sql
-- This adds missing columns and creates atomic functions

Part 2: Indexes and Constraints

-- Copy and paste the content from migration-part2.sql
-- This adds performance indexes and data constraints

Part 3: Security Policies

-- Copy and paste the content from migration-part3.sql  
-- This updates RLS policies for proper security

Step 2: Verify Migration Success

Run these queries to verify the migration worked:

-- Check if new columns exist
SELECT column_name FROM information_schema.columns 
WHERE table_name = 'product_codes' AND column_name = 'used_at';

-- Check if functions exist
SELECT routine_name FROM information_schema.routines 
WHERE routine_name = 'claim_product_codes_atomic';

-- Test sample promo codes
SELECT * FROM promo_codes WHERE code IN ('WELCOME10', 'SAVE50');

💻 Application Deployment

Step 1: Deploy Code Changes

The following files have been updated and need to be deployed:

Critical Files:

  • src/app/(shop)/checkout/CheckoutClient.tsx - Fixed promo race conditions
  • src/app/api/orders/create/route.ts - Enhanced order creation with rollbacks
  • src/app/api/promo/validate/route.ts - New atomic promo validation API

Enhanced Files:

  • src/lib/providers/AuthProvider.tsx - Fixed auth state race conditions
  • src/contexts/CartContext.tsx - Improved cart state management
  • All auth forms - Added double-submission prevention

New Files:

  • src/app/(shop)/order-success/page.tsx - Order completion page

Step 2: Deploy to Production

# Build and deploy your Next.js application
npm run build
npm run start

# Or deploy to your hosting platform (Vercel, etc.)

🧪 Testing Checklist

Critical Race Condition Tests

  1. Promo Code Test:

    • Apply the same promo code from multiple browser tabs simultaneously
    • Verify only one application succeeds
    • Test both percentage and fixed discount codes
  2. Product Code Assignment Test:

    • Create multiple orders for the same digital product simultaneously
    • Verify each order gets unique product codes
    • Check that no codes are duplicated or lost
  3. Wallet Payment Test:

    • Make multiple wallet payments simultaneously
    • Verify wallet balance is correctly decremented
    • Test insufficient balance scenarios
  4. Form Submission Test:

    • Try submitting login/register forms multiple times rapidly
    • Verify only one submission is processed
    • Check loading states work correctly

Performance Tests

  1. Concurrent Users:

    • Test with 10+ simultaneous checkout processes
    • Monitor database performance during peak usage
    • Verify response times remain acceptable
  2. Database Locks:

    • Check that SKIP LOCKED prevents deadlocks
    • Monitor for any hanging transactions
    • Verify atomic operations complete successfully

📊 Monitoring & Alerts

Database Monitoring

Monitor these metrics post-deployment:

  1. Transaction Deadlocks: Should be eliminated
  2. Lock Wait Times: Should be minimal with SKIP LOCKED
  3. Function Execution Times: Monitor performance of new atomic functions
  4. Constraint Violations: Should catch any data integrity issues

Application Monitoring

  1. API Response Times: /api/promo/validate and /api/orders/create
  2. Error Rates: Should decrease for race condition related errors
  3. Order Success Rate: Should improve with better error handling
  4. User Experience: Monitor checkout completion rates

🚨 Rollback Plan

If issues occur, follow this rollback procedure:

Database Rollback

-- 1. Drop new functions
DROP FUNCTION IF EXISTS claim_product_codes_atomic(uuid, integer, uuid);
DROP FUNCTION IF EXISTS increment_promo_usage(uuid);

-- 2. Remove new columns (OPTIONAL - data will be preserved)
-- ALTER TABLE product_codes DROP COLUMN IF EXISTS used_at;

-- 3. Restore original policies if needed
-- (Refer to your backup)

Application Rollback

# Revert to previous deployment
git revert <commit-hash>
npm run build && npm run start

🔧 Configuration Updates

Environment Variables

Ensure these are set in your production environment:

# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key

# Email Configuration (for order confirmations)
RESEND_API_KEY=your_resend_key

Supabase Settings

Verify these settings in your Supabase dashboard:

  1. Database: Connection pooling enabled
  2. Auth: Email confirmations configured
  3. Storage: Payment screenshots bucket configured
  4. RLS: All policies enabled and working

📈 Success Metrics

After deployment, you should see:

Zero race condition errors in your logs ✅ Improved order success rate (target: 99%+) ✅ Faster response times for checkout operations ✅ Better user experience with proper loading states ✅ Data integrity maintained with no duplicate orders/codes

🎯 Post-Deployment Actions

Immediate (Within 24 hours)

  • Monitor error logs for any new issues
  • Check database performance metrics
  • Verify sample transactions work correctly
  • Test all payment methods (wallet, bank transfer, etc.)

Short-term (Within 1 week)

  • Analyze checkout conversion rates
  • Review user feedback for any UX issues
  • Monitor database growth and performance
  • Optimize any slow queries if identified

Long-term (Within 1 month)

  • Review system performance under peak load
  • Consider additional optimizations based on usage patterns
  • Plan for scaling if needed
  • Document any lessons learned

🆘 Support & Troubleshooting

Common Issues

  1. Function Permission Errors:

    GRANT EXECUTE ON FUNCTION function_name TO authenticated;
  2. RLS Policy Conflicts:

    • Check existing policies don't conflict with new ones
    • Use DROP POLICY IF EXISTS before creating new ones
  3. Index Creation Timeouts:

    • Run index creation during low-traffic periods
    • Consider creating indexes separately if needed

Getting Help

If you encounter issues:

  1. Check the error logs in Supabase dashboard
  2. Review the database query performance
  3. Verify all migration steps were completed
  4. Test in isolation to identify the specific issue

🎉 Congratulations!

Your Tronline Bazar application is now race-condition free and ready for high-traffic scenarios!

The fixes implemented include:

  • ⚡ Atomic database operations
  • 🔒 Proper concurrency handling
  • 🛡️ Data integrity constraints
  • 📈 Performance optimizations
  • 🔧 Better error handling

Your digital marketplace can now handle concurrent users safely and reliably.