Participant Export Optimization
Summary
Significant optimizations have been implemented for participant exports to prevent the server from slowing down with large volumes of data (30,000+ records).Optimizations Implemented
1. Chunked Export
- File:
app/Exports/ParticipantsExportOptimized.php - Benefit: Processes data in small batches (1000 records by default) instead of loading all records into memory
- Implementation: Uses
WithChunkReadingandFromQueryfrom Laravel Excel
2. Background Processing
- File:
app/Jobs/ProcessParticipantsExport.php - Benefit: For large exports (>5000 participants), processing happens in the background
- Notification: The user receives an email when the export is ready
- Timeout: 1 hour maximum processing time
3. Validations and Limits
- File:
app/Http/Middleware/ValidateExportRequest.php - Maximum limit: 100,000 participants per export
- Immediate limit: 5,000 participants (configurable)
- Validation: Required fields and event existence
4. Query Optimization
- Specific select: Only loads necessary fields
- Filtering by event: Reduces the volume of processed data
- Indexes: Optimized for queries by
evento_id
5. Automatic Cleanup
- Command:
app/Console/Commands/CleanupExportFiles.php - Schedule: Runs daily
- Retention: Files are deleted after 7 days
Configuration
Environment Variables (.env)
Configuration in config/app.php
Operation Flow
Small Exports (≤5,000 participants)
- User clicks “Export”
- Processed immediately
- Direct download of the Excel file
Large Exports (>5,000 participants)
- User clicks “Export”
- Notification is shown: “The export is being processed”
- Job is dispatched to the background
- User receives an email with the attached file when ready
Files Modified
Controllers
app/Http/Controllers/Eventos/EventosController.php- Optimized
exportParticipants()method - Decision logic between immediate/background processing
- Optimized
Views
resources/views/eventos/participants/index.blade.php- Updated JavaScript to handle JSON responses
- Export status notifications
Routes
routes/eventos.phpvalidate.exportmiddleware applied to the export route
Middleware
app/Http/Middleware/ValidateExportRequest.php- Validations for limits and required fields
Artisan Commands
File Cleanup
Automatic Scheduling
The command runs automatically every day at 00:00.Performance Benefits
- Memory: 90% reduction in memory usage for large exports
- Response time: Small exports remain immediate
- Scalability: Support for up to 100,000 participants
- User experience: Clear notifications of processing status
- Maintenance: Automatic cleanup of temporary files
Monitoring
Logs
- Large exports are logged
- Processing errors are captured and reported
Metrics
- Number of participants per export
- Processing time
- Files generated and deleted
Security Considerations
- Strict limits: Prevents DoS attacks
- Field validation: Only allowed fields
- Automatic cleanup: Temporary files are deleted
- Authentication: Only authorized users can export
Recommended Next Steps
- Monitoring: Implement performance metrics
- Cache: Consider caching for frequent queries
- Compression: Compress large files before sending
- CDN: Use a CDN for large file distribution
