mohd-faraz

_blogs

// blogs / 20260801.md

Dev Log: August 01 Wrap-up

2026-08-01
#backend#security#optimization#debugging

Overview

Today was mostly about hardening our public-facing AI workflow. I spent a good chunk of time making our spam detection smarter and cleaning up how we identify users behind proxies. It wasn't anything flashy, but it feels good to close out these small technical debts that have been lingering.

What I Worked On

Hardening IP Tracking

One of the most annoying parts of dealing with public endpoints is getting a reliable client IP. If you just grab the remote address, you often end up with the IP of our own load balancer, which is useless for rate limiting.

I overhauled the logic to properly parse X-Forwarded-For and X-Real-IP headers. I also made sure to handle those pesky IPv6 loopback addresses (::1) that always seem to pop up during local development. Now, the system takes the first IP in the chain, which should give us the actual user behind the request. It’s one of those things that sounds simple until you're staring at three different headers and trying to decide which one to trust.

Throttling Junk Requests

We’ve been working on a way to track "irrelevant" messages—basically, if a user keeps sending nonsense to the AI, we need to shut it down after a few tries. I implemented a local cache using a ConcurrentHashMap to keep track of these counts in memory.

I also noticed we were hitting the configuration service every single time we needed to check the threshold limit. That’s unnecessary overhead for a value that rarely changes. I added a simple caching layer for that threshold value, so we only fetch it once and reuse it. It's a small win, but it keeps the logs cleaner and the response times snappier.

I did find a silly logic error where I was blocking users exactly at the limit rather than after they exceeded it. Swapping a >= for a > fixed that. Here’s a look at the simplified caching logic I went with:

private int getRequestThreshold() {
    if (CACHED_LIMIT != null) {
        return CACHED_LIMIT;
    }
    try {
        String val = fetchConfig("request_threshold_key");
        if (val != null) {
            CACHED_LIMIT = Integer.parseInt(val.trim());
            return CACHED_LIMIT;
        }
    } catch (Exception e) {
        // Fallback to a hardcoded default if the config service is down
    }
    return DEFAULT_LIMIT;
}

Frontend Payload Tweaks

On the frontend side, I had to jump into our 'Get Started' module to ensure we're passing the right metadata. The workflow was failing because it didn't have the account_id and access_token it expected in the payload. A quick update to the request object to pull the token from local storage sorted that out.

Wrapping Up

Everything feels a bit more robust now. The IP tracking is more reliable, and we aren't hammering our config service for data that doesn't change. Tomorrow I’ll probably spend some time monitoring the logs to make sure the new threshold logic isn't being too aggressive with real users. Catch you then!