<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Agboola</title>
    <description>The latest articles on DEV Community by Ali Agboola (@sage-ali).</description>
    <link>https://dev.to/sage-ali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1096562%2Fb6eedf35-919a-4c31-9cc3-ff52199a7954.png</url>
      <title>DEV Community: Ali Agboola</title>
      <link>https://dev.to/sage-ali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sage-ali"/>
    <language>en</language>
    <item>
      <title>From Cache Keys to Concurrency</title>
      <dc:creator>Ali Agboola</dc:creator>
      <pubDate>Sat, 13 Jun 2026 11:39:21 +0000</pubDate>
      <link>https://dev.to/sage-ali/from-cache-keys-to-concurrency-176l</link>
      <guid>https://dev.to/sage-ali/from-cache-keys-to-concurrency-176l</guid>
      <description>&lt;p&gt;The final stage of HNG is a writing task: pick two pieces of work from the internship, one solo and one team, and write about whichever ones stuck. Mine are Stage 4, an optimization pass on an API I'd already built, and Stage 6, the queue infrastructure behind a team product called Flowbrand. Neither is here because it went well. Each one cost me an assumption I didn't know I was carrying, which is a better reason to write than a clean scoreboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 4: the difference between working and holding up
&lt;/h2&gt;

&lt;p&gt;By Stage 4 I had a NestJS API sitting on db seeded with demographic profiles. Filtering, sorting, pagination, a natural-language search endpoint. All of it worked. Stage 4 then asked the question every stage before it had politely avoided: what happens when people actually use this?&lt;/p&gt;

&lt;p&gt;Three deliverables: faster queries, consistent cache keys, and a CSV ingestion endpoint that could take files of up to 500,000 rows without flattening the server.&lt;/p&gt;

&lt;p&gt;The slowness came first. A query like &lt;code&gt;gender=male&amp;amp;country_id=NG&amp;amp;age_group=adult&lt;/code&gt; against a million-row Postgres table over a remote connection took about 1.6 seconds. All three columns had single-column indexes, so I assumed the database was covered. It wasn't. Postgres can't combine three separate indexes into a single lookup; it runs a bitmap scan on each one and intersects the results in memory, and at a million rows the intersection is where the time goes.&lt;/p&gt;

&lt;p&gt;The caching problem was quieter. Redis was in place, but &lt;code&gt;gender=male&amp;amp;country_id=NG&lt;/code&gt; and &lt;code&gt;country_id=NG&amp;amp;gender=male&lt;/code&gt; serialized to different cache keys. Two requests asking the same question, two trips to the database.&lt;/p&gt;

&lt;p&gt;The CSV problem was plain arithmetic. Inserting 500,000 rows one at a time is 500,000 network round trips; at 5ms each, that's about forty minutes. Buffer the whole file in memory instead and a 50MB upload becomes a 50MB heap spike, multiplied by every concurrent upload.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I built
&lt;/h3&gt;

&lt;p&gt;Composite indexes went in first because they required no application changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@@index([gender, country_id])
@@index([gender, age_group, country_id])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Order matters here. &lt;code&gt;gender&lt;/code&gt; leads because it prunes the most rows at the first step, and a composite index lets Postgres walk one pre-grouped structure instead of doing three scans and stitching them together afterwards.&lt;/p&gt;

&lt;p&gt;Then the cache keys, before touching any Redis logic. A cache that sometimes misses is a nuisance; a cache that returns results for the wrong query is a hazard, and inconsistent keys are exactly how you get there. So every filter object is normalized before anything else happens: strings lowercased, keys sorted alphabetically, numerics coerced to consistent precision, undefined fields stripped, then serialized.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildCacheKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProfileQueryFilters&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;normalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;normalizeFilters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sorted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`profiles:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cache itself is plain cache-aside. Check Redis, return on a hit, query and store on a miss with a five-minute TTL, invalidate on writes. Five minutes because analysts repeat queries within a session, and slightly stale analytics is a trade nobody notices.&lt;/p&gt;

&lt;p&gt;For the CSV endpoint, the file never sits in the Node heap. Multer's &lt;code&gt;diskStorage&lt;/code&gt; writes the upload to &lt;code&gt;/tmp&lt;/code&gt;, the service opens a &lt;code&gt;readline&lt;/code&gt; interface over a read stream, and valid rows collect in a 1,000-row buffer that flushes through &lt;code&gt;createMany&lt;/code&gt; with &lt;code&gt;skipDuplicates: true&lt;/code&gt;. Each chunk commits on its own, and there is deliberately no transaction around the import. If the process dies at row 400,000, those rows stay committed, and because duplicates are skipped, re-running the same file is safe. Rolling back 400,000 good inserts because row 400,001 crashed helps nobody.&lt;/p&gt;

&lt;h3&gt;
  
  
  What broke
&lt;/h3&gt;

&lt;p&gt;The bug that taught me the most was in my own normalization. I had put it inside &lt;code&gt;buildCacheKey&lt;/code&gt;, which meant the cache saw normalized filters while the database received the raw ones. Same request, two different shapes. It mostly worked anyway, because Postgres happened to be case-insensitive on that column, which is the worst kind of bug: one that passes by accident. The fix was boring and correct. Normalization became the first thing &lt;code&gt;findAllProfiles()&lt;/code&gt; does, before the cache check and before the query, so both sides see the same object.&lt;/p&gt;

&lt;p&gt;The other adjustment was chunk size. I started flushing every 100 rows, which on a 500k file means 5,000 round trips. Too many. Moving to 1,000-row chunks cut it to 500 and kept memory per chunk reasonable.&lt;/p&gt;

&lt;p&gt;The before and after:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After (cache miss)&lt;/th&gt;
&lt;th&gt;After (cache hit)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;gender=male&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;820ms&lt;/td&gt;
&lt;td&gt;310ms&lt;/td&gt;
&lt;td&gt;12ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;gender=male&amp;amp;country_id=NG&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1,240ms&lt;/td&gt;
&lt;td&gt;390ms&lt;/td&gt;
&lt;td&gt;14ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;gender=male&amp;amp;country_id=NG&amp;amp;age_group=adult&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1,680ms&lt;/td&gt;
&lt;td&gt;420ms&lt;/td&gt;
&lt;td&gt;11ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What stayed with me, in order of how often I've repeated it to myself since: &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; beats assumptions. I was sure my single-column indexes were earning their keep until the plan showed three bitmap scans and a hash join over 70,000 rows. Cache key normalization is a correctness requirement, not a performance tweak, and before this stage I had never once thought about it. And partial-failure behaviour in a bulk operation is a decision to make deliberately at design time, not something to discover during an incident.&lt;/p&gt;

&lt;p&gt;I picked this stage because it was the first one that asked "does it hold up?" rather than "does it work?". Every earlier stage had a happy path. This one had a number: 1.6 seconds before, 11 milliseconds after. Watching that number move was the first time the work felt like engineering instead of assembly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stage 6: Flowbrand, queues, and the merge I'd take back
&lt;/h2&gt;

&lt;p&gt;Stage 6 was the team stage. We built the backend for Flowbrand, a platform that takes an uploaded document and generates a branded marketing funnel from it. My slice was the queue infrastructure, the document upload pipeline, and the worker that actually produces the funnels. And then, late in the stage, a security audit that sent me somewhere I hadn't expected to go.&lt;/p&gt;

&lt;p&gt;The product is asynchronous whether you like it or not. A user uploads a PDF. An LLM call extracts structured content from it. A second LLM call turns that content into a funnel schema. The assembled result lands in Postgres. Thirty seconds on a good day, with a failure possible at every step, and none of it belongs inside an HTTP request. Without a queue, Flowbrand is a timeout with a logo.&lt;/p&gt;

&lt;p&gt;The boilerplate we inherited had no queue at all, and features were already being built against synchronous service calls the real flow would never survive. So before anything else, the team needed something to plug into.&lt;/p&gt;

&lt;h3&gt;
  
  
  Five PRs and an audit
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;PR #12&lt;/strong&gt; laid the foundation: Bull wired into NestJS with Redis behind it, a &lt;code&gt;QueueService&lt;/code&gt; any module could inject to enqueue work, a base processor pattern, and a &lt;code&gt;QueueModule&lt;/code&gt; for the rest of the team to import. The decision I'd defend hardest from that PR is typing jobs as a discriminated union — one payload shape per job type — so a processor always knows exactly what it's holding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PR #30&lt;/strong&gt; came from watching the foundation misbehave under real conditions. Redis briefly unreachable during startup? Module threw. Job died with an unhandled exception? Vanished, silently. I added connection retries with graceful degradation, structured error logging at the processor level, and an event listener that records failures to the database where someone can actually see them. The question underneath all of it: who owns this failure, and what are they supposed to do with it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PR #35&lt;/strong&gt; was the funnel worker: two processors, 889 lines, fifteen files. &lt;code&gt;ExtractionProcessor&lt;/code&gt; reads the document from S3, calls the LLM, stores the structured content. &lt;code&gt;FunnelGenerationProcessor&lt;/code&gt; takes that content, calls the LLM again with a funnel-specific prompt, and writes the assembled result inside a single &lt;code&gt;QueryRunner&lt;/code&gt; transaction. Keep the 889 in mind. It comes back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PR #62&lt;/strong&gt; was the upload pipeline — and where the real edge-case work happened. Flowbrand accepts PDFs, Word files, PowerPoints. Each one needs to be validated, streamed to MinIO, queued for text extraction, and tracked via a polling endpoint so the frontend knows what's happening. The &lt;code&gt;uploaded_documents&lt;/code&gt; table, a status enum (&lt;code&gt;UPLOADING&lt;/code&gt;, &lt;code&gt;PARSING&lt;/code&gt;, &lt;code&gt;COMPLETED&lt;/code&gt;, &lt;code&gt;FAILED&lt;/code&gt;), and a &lt;code&gt;failure_reason&lt;/code&gt; column all went in first. Then the storage service, then the extraction processor.&lt;/p&gt;

&lt;p&gt;The happy path worked within a day. Two things then broke it.&lt;/p&gt;

&lt;p&gt;The first was the upload itself. The initial version used NestJS's default &lt;code&gt;FileInterceptor&lt;/code&gt;, which buffers the whole file to disk before you touch it. Fine at 2MB. A 25MB PowerPoint stalled the server. The fix was switching to &lt;code&gt;memoryStorage: false&lt;/code&gt; in Multer config and piping &lt;code&gt;createReadStream(file.path)&lt;/code&gt; directly into the MinIO upload call — the file never fully lives in memory.&lt;/p&gt;

&lt;p&gt;The second was a retry idempotency problem I hadn't thought through. Bull retries failed jobs automatically. If &lt;code&gt;ExtractionProcessor&lt;/code&gt; crashed after writing 40% progress to the database but before marking the job complete, the retry would pick up the same job and try to reset progress to 0 — which made no sense, and in some cases hit a unique constraint violation. The fix was an orphan guard at the top of the processor: read the current DB record first. If &lt;code&gt;status&lt;/code&gt; is &lt;code&gt;COMPLETED&lt;/code&gt;, skip. If &lt;code&gt;status&lt;/code&gt; is &lt;code&gt;FAILED&lt;/code&gt;, re-run from the top. If &lt;code&gt;percent_complete &amp;gt; 0&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; is &lt;code&gt;PARSING&lt;/code&gt;, we're resuming — don't reset. A Bull job is not a function call. It can run more than once, on a different process, with the same inputs. If your processor has side effects — and writing to MinIO and updating a DB row definitely are — you need to be able to answer: &lt;em&gt;what happens if this runs twice?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PR #102&lt;/strong&gt; wrapped every controller response in a consistent &lt;code&gt;{ status: "success", data: ... }&lt;/code&gt; envelope. Not glamorous. The frontend was blocked on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PR #166&lt;/strong&gt; came from a code audit, and it was the most uncomfortable work I did in Stage 6. Three concurrency bugs in the authentication service, all security-relevant, all in code that had passing tests.&lt;/p&gt;

&lt;p&gt;The first: two simultaneous first-time logins from the same user both read &lt;code&gt;auth_metadata = null&lt;/code&gt;, both tried to create the row, and one crashed on a unique constraint. Fix: catch Postgres error code &lt;code&gt;23505&lt;/code&gt; and re-read the row the concurrent request already created. The second requester gets a valid result without knowing a race happened.&lt;/p&gt;

&lt;p&gt;The second was worse. The account lockout counter used a JavaScript read-modify-write pattern: read &lt;code&gt;failed_attempts&lt;/code&gt;, add 1, write it back. Under concurrent wrong-password attempts, two requests could read the same value, both add 1, both write the same number. The counter could only go up by 1 even if five requests hit simultaneously. A brute-force attack could exhaust attempts without ever triggering the lock. The fix was deleting the read-modify-write entirely and replacing it with a single &lt;code&gt;repository.increment()&lt;/code&gt; call — one SQL statement, &lt;code&gt;UPDATE auth_metadata SET failed_attempts = failed_attempts + 1 WHERE id = $1&lt;/code&gt;. The database handles the concurrency; there is no window between read and write for another request to interfere.&lt;/p&gt;

&lt;p&gt;The third: the distributed lock on OTP verification had a 10-second TTL, chosen arbitrarily. A slow DB response under load could let the lock expire before the handler finished, allowing two concurrent OTP submissions to both succeed for the same token. Fix: raise the TTL to 30 seconds.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;increment()&lt;/code&gt; fix introduced a subtle problem I missed on first pass. The method had been returning the updated metadata object, which the caller used to decide whether to trigger lockout. &lt;code&gt;increment()&lt;/code&gt; doesn't give you the row back. My first version re-fetched it — two DB round trips per failed login. I looked at what the caller actually needed: just the new count. So the method now returns the old value plus one, calculated locally, and only hits the database again when it needs to write &lt;code&gt;locked_until&lt;/code&gt;. One write per failed attempt, no superfluous read.&lt;/p&gt;

&lt;p&gt;These three bugs existed in code with passing tests. The tests ran one request at a time. That's the lesson from PR #166: concurrency bugs are invisible in unit tests unless you specifically design the test to expose them. And the fix for bug 2 is less about adding code than removing it — moving the operation to where it belongs, which is the database, not JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I keep
&lt;/h3&gt;

&lt;p&gt;The queue left me with a clean model for failure ownership, because the two processors handle it opposite ways and both are right. &lt;code&gt;ExtractionProcessor&lt;/code&gt; swallows exceptions and records them to the database: an extraction failure is specific to that document, and retrying the same broken content against the same call doesn't change anything. &lt;code&gt;FunnelGenerationProcessor&lt;/code&gt; rethrows: its failures are usually transient, rate limits and timeouts, and Bull's retry machinery exists for exactly that case.&lt;/p&gt;

&lt;p&gt;And then PR #35. 889 lines across fifteen files is at least three concerns in one diff, and I knew that when I opened it. The deadline was real. I merged. My lowest score in Stage 6 was Collaboration + Docs — 6 out of 10 — and it was earned. Reviewers can only review what they can read. The question I now ask before opening anything: &lt;em&gt;what's the smallest piece of this that someone could review on its own?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I picked Stage 6 because it's where the internship stopped being coursework. In the solo stages, reviewability is theoretical; the reviewer is a grading script. In Stage 6, a real teammate is blocked until your work makes sense to them. PR #35 put the tradeoff in front of me plainly. I chose to ship the whole feature. The score put a number on what that cost. I don't plan to pay it twice.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Ali . HNG XIV Backend Track.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Portfolio: &lt;a href="https://sage-ali.vercel.app" rel="noopener noreferrer"&gt;sage-ali.vercel.app&lt;/a&gt; · GitHub: &lt;a href="https://github.com/sage-ali" rel="noopener noreferrer"&gt;github.com/sage-ali&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>learning</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
