Backend Development 16 min read

Optimizing an Audit System Processing Chain for Traffic Spikes Using Chain of Responsibility and Sliding‑Window Detection

To prevent audit pipelines from choking during traffic surges, the article proposes augmenting the classic Chain of Responsibility with a sliding‑window anomaly detector, a statistically‑driven dynamic detector, a rule‑based backflow mechanism, and AI‑powered traffic forecasting, enabling adaptive routing and proactive scaling.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Optimizing an Audit System Processing Chain for Traffic Spikes Using Chain of Responsibility and Sliding‑Window Detection

In modern information systems, audit pipelines pre‑process data from various business lines before manual review. Sudden spikes in traffic can exhaust resources and cause delays or crashes.

This article introduces a series of optimizations to the audit system’s processing chain, employing the Chain of Responsibility pattern, sliding‑window based traffic anomaly detection, dynamic rule‑driven backflow, and future AI‑driven traffic prediction.

Audit System Overview

The audit system integrates multiple processors: ContactFilter, Highlight, MD5Check, KeywordCheck, ... SaveDatabase. Processors are linked in a fixed order, forming a responsibility chain.

Existing Chain and Responsibility Pattern

The chain follows the classic responsibility‑chain design, allowing each handler to process a request or pass it to the next handler.

public abstract class Handler {
    protected Handler next;
    public void setNext(Handler next) { this.next = next; }
    public abstract void handle(Request request);
}

public class ContactFilterHandler extends Handler {
    @Override
    public void handle(Request request) {
        // process contact filter
        if (next != null) { next.handle(request); }
    }
}
// other handlers omitted for brevity

Challenge: Traffic Spikes

When traffic surges, the chain may become a bottleneck, leading to resource exhaustion.

First Optimization – Separate Anomaly Detector

A sliding‑window detector monitors the number of requests per 10‑minute window. If the total exceeds a threshold, the request is marked abnormal and routed to a simplified chain.

public class SlidingWindowDetector {
    private final int windowSize; // minutes
    private final int slideStep; // minutes
    private final int threshold;
    private final Queue
dataPoints = new LinkedList<>();
    private int currentTotal = 0;

    public SlidingWindowDetector(int windowSize, int slideStep, int threshold) {
        this.windowSize = windowSize;
        this.slideStep = slideStep;
        this.threshold = threshold;
    }

    public void addDataPoint(int dataPoint) {
        dataPoints.add(dataPoint);
        currentTotal += dataPoint;
        if (dataPoints.size() > windowSize) {
            currentTotal -= dataPoints.poll();
        }
    }

    public boolean isTrafficAbnormal() {
        return currentTotal > threshold;
    }

    public void slideWindow() {
        for (int i = 0; i < slideStep && !dataPoints.isEmpty(); i++) {
            currentTotal -= dataPoints.poll();
        }
    }

    public static void main(String[] args) {
        // simulation omitted
    }
}

Second Optimization – Intelligent Sliding Window

Instead of a fixed threshold, the detector computes the mean and standard deviation of the window and flags data points that deviate more than three standard deviations.

public class DynamicSlidingWindowDetector {
    private final int windowSize;
    private final Queue
dataPoints = new LinkedList<>();
    private int currentTotal = 0;
    private int currentCount = 0;

    public DynamicSlidingWindowDetector(int windowSize) { this.windowSize = windowSize; }

    public void addDataPoint(int dp) {
        dataPoints.add(dp);
        currentTotal += dp;
        currentCount++;
        if (currentCount > windowSize) {
            int removed = dataPoints.poll();
            currentTotal -= removed;
            currentCount--;
        }
    }

    public boolean isTrafficAbnormal() {
        if (currentCount < windowSize) return false;
        double mean = currentTotal / (double) currentCount;
        double variance = 0;
        for (int v : dataPoints) variance += Math.pow(v - mean, 2);
        variance /= currentCount;
        double stdDev = Math.sqrt(variance);
        int latest = dataPoints.peek();
        return Math.abs(latest - mean) > 3 * stdDev;
    }

    public static void main(String[] args) {
        // simulation omitted
    }
}

Third Optimization – Backflow Mechanism

When an abnormal request is later identified as normal, a backflow detector loads rules from a configuration center and routes the request back to the full processing chain.

public class BackflowDetector {
    private Map
> rulesMap = new HashMap<>();

    public void loadRules() {
        // Load backflow rules from configuration center
        rulesMap = ConfigurationCenter.loadRules();
    }

    public boolean shouldBackflow(Request request) {
        List
rules = rulesMap.get(request.getBusinessLine());
        if (rules != null) {
            for (Rule r : rules) {
                if (r.evaluate(request)) return true;
            }
        }
        return false;
    }
}

Future Directions

Dynamic configuration centers will allow real‑time reordering of handlers based on load. AI models (e.g., RandomForestRegressor) will predict traffic trends and enable proactive scaling.

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import numpy as np
# Assume historical traffic data is prepared in `data`
X = data[:, :-1]
y = data[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestRegressor()
model.fit(X_train, y_train)
future_data = np.array([...])  # features for future time points
predicted_traffic = model.predict(future_data)

Overall, the layered optimizations enable the audit system to handle traffic bursts gracefully, preserve normal business flow, and lay the groundwork for intelligent, self‑adjusting pipelines.

backendchain of responsibilitydesign patternsJavasystem optimizationSliding Windowtraffic detection
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.