Heuristic:A616567126 GPT WEB JAVA SD Queue Backpressure
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Queuing |
| Last Updated | 2026-10-13 18:00 GMT |
Overview
Enforce a Redis List queue with a hard size limit of 5 for Stable Diffusion image generation to apply backpressure and prevent resource exhaustion.
Description
The SD image generation system uses `QueueUtil` to manage a Redis List-based queue (`user_queue`). Before adding a new request, the queue length is checked against `CommonConst.QUEUE_SIZE` (hardcoded to 5). If the queue is full, a business exception is thrown (`"队列已满请等待"` / "Queue is full, please wait"). A scheduled task (`MyTask.run()`) polls the queue every 5 seconds, processing only the first item (FIFO). After successful generation, the item is removed from the queue. On any exception, the item is also removed and the user's remaining times are refunded. This backpressure mechanism prevents the SD WebUI from being overwhelmed with concurrent requests and ensures fair FIFO ordering.
Usage
Use this heuristic when queuing image generation requests to an external SD WebUI service. Apply a hard queue limit to reject new requests when the system is at capacity, and use scheduled polling (not event-driven) to control request rate. Always refund user credits and remove the queue item on failure to prevent stuck queues.
The Insight (Rule of Thumb)
- Action: Check `getCurrentQueueLength() > QUEUE_SIZE` before pushing to the Redis queue; throw exception if exceeded.
- Value: `QUEUE_SIZE = 5` (hardcoded in `CommonConst`).
- Trade-off: Hard limit of 5 may reject requests under high load; users see "queue full" message instead of waiting indefinitely. Increase this value only if SD WebUI can handle higher throughput.
- Compatibility: Uses Redis List `rightPush` for enqueue and `remove` for dequeue; FIFO ordering guaranteed by Redis List semantics.
Reasoning
Stable Diffusion image generation is GPU-bound and typically takes 5-30 seconds per image. The SD WebUI has limited concurrency capacity. Without backpressure, a burst of requests would accumulate in the queue, leading to unbounded memory growth and very long user wait times. A hard limit of 5 ensures the SD WebUI processes at most 5 queued requests at any time. The 5-second polling interval in `MyTask` provides a reasonable balance between responsiveness and CPU overhead. On failure, credit refund + queue removal prevents both user dissatisfaction (lost credits) and queue stalls (stuck items).