AutoAffiliate
AI video generation SaaS with hybrid licensing & concurrency locks.
The System Bottleneck & Problem Statement
Digital marketing affiliates needed automated short-form video generation to promote affiliate products. However, SaaS platforms either charged expensive monthly subscriptions that users abandoned, or suffered account-sharing abuse where 10+ users pooled money to exploit a single account.
Architected Solution & Execution Mechanics
Architected a hybrid business model: users purchase a lifetime software license for the core workflow, and consume pay-as-you-go credit tokens for cloud AI video rendering. Protected server infrastructure by enforcing a strict 2-device concurrency session limit.
Affiliate Dashboard
Zero-lag interface with credit balance telemetry and live video previews.
Core Architecture Implementation
Production-grade architecture implementation powering AutoAffiliate:
import { db } from '@/lib/db';
// Strict Dual-Device Concurrency Session Guard
export async function enforceMaxTwoDevices(userId: string, currentDeviceId: string): Promise<boolean> {
const { data: activeSessions, error } = await db
.from('user_active_sessions')
.select('device_id, last_heartbeat')
.eq('user_id', userId)
.gt('last_heartbeat', new Date(Date.now() - 5 * 60 * 1000).toISOString())
.order('last_heartbeat', { ascending: true });
if (error || !activeSessions) return false;
const deviceExists = activeSessions.some(s => s.device_id === currentDeviceId);
if (deviceExists) {
// Update heartbeat of current device
await db
.from('user_active_sessions')
.update({ last_heartbeat: new Date().toISOString() })
.eq('device_id', currentDeviceId);
return true;
}
// If already at 2 devices, evict oldest session
if (activeSessions.length >= 2) {
const oldestDevice = activeSessions[0].device_id;
await db
.from('user_active_sessions')
.delete()
.eq('device_id', oldestDevice);
}
// Register new device session
await db
.from('user_active_sessions')
.insert({
user_id: userId,
device_id: currentDeviceId,
last_heartbeat: new Date().toISOString()
});
return true;
}Key Engineering Responsibilities
Engineered credit ledger database schema with ACID atomic transactions to prevent double-spending.
Built session concurrency middleware detecting and limiting simultaneous logins to 2 active devices.
Created a unified billing & top-up workflow with automated credit disbursement upon payment.
Designed high-density dark UI with intuitive credit meters and video generation status monitors.
Non-Trivial Trade-Offs & Decisions
Preventing Double-Spending of Video Credits
System Friction: When users opened multiple tabs and clicked "Render" simultaneously, asynchronous requests read the old balance before writing the debit, allowing free video generations.
Architectural Decision: Migrated balance checks to PostgreSQL stored procedures using `SELECT FOR UPDATE` row-level locks, making balance verification and debit execution completely atomic.
Measured Outcome: Zero credit leaks across thousands of simulated parallel requests.