<?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: Ahmed Mahmoud Abbas</title>
    <description>The latest articles on DEV Community by Ahmed Mahmoud Abbas (@ahmed145).</description>
    <link>https://dev.to/ahmed145</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4271590%2Fc4f2197f-a326-49e2-8da8-aaf6e7713340.jpg</url>
      <title>DEV Community: Ahmed Mahmoud Abbas</title>
      <link>https://dev.to/ahmed145</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmed145"/>
    <language>en</language>
    <item>
      <title>Why my URL shortener did 36 requests a second, not 225</title>
      <dc:creator>Ahmed Mahmoud Abbas</dc:creator>
      <pubDate>Sun, 29 Nov 2026 03:37:31 +0000</pubDate>
      <link>https://dev.to/ahmed145/why-my-url-shortener-did-36-requests-a-second-not-225-1b53</link>
      <guid>https://dev.to/ahmed145/why-my-url-shortener-did-36-requests-a-second-not-225-1b53</guid>
      <description>&lt;p&gt;Load tests, 9 Nov 2025 and 29 Nov 2026&lt;/p&gt;

&lt;p&gt;I load tested my capstone URL shortener in November 2025 and wrote 225 requests a second into my docs. A year later the same test on my laptop gave 36. After four changes to the way the app writes to SQLite, the same laptop did 1128 at 10 users. The write was the ceiling, not the machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two runs, one year apart
&lt;/h2&gt;

&lt;p&gt;In November 2025 I ran Apache Bench against POST /api/shorten, 1000 requests, 10 at a time, and my week 4 doc recorded 225.22 requests a second, 0 failed, 44.4 ms mean and 95% of requests under 62 ms. That doc does not say which machine it was.&lt;/p&gt;

&lt;p&gt;In November 2026 I cloned the same commit again, 771a139, which is the parent of 38b2752, and ran the same command on my laptop: AMD Ryzen 5 7235HS, 4 cores and 8 threads, 11 GiB of RAM, Pop!_OS 24.04. It did 36.01 requests a second, 277.7 ms mean, 95% under 548 ms.&lt;/p&gt;

&lt;p&gt;I did not find out why the two numbers are so far apart. It could be the machine, the Docker setup, or a year of small changes in the project. What I trust is the pair I ran on one laptop on one day, and that is the rest of this post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'{"url":"https://example.com/test"}'&lt;/span&gt; \
  &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; post_data.json

ab -n 1000 -c 10 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What the baseline was doing
&lt;/h2&gt;

&lt;p&gt;The fresh baseline did 36.01, 115.21 and 85.93 requests a second at 10, 50 and 100 users. Those do not climb in a straight line, and I did not chase that either. What they do show is how long one request waited: 277.7 ms at 10 users, 434.0 ms at 50, 1163.7 ms at 100.&lt;/p&gt;

&lt;p&gt;POST /api/shorten does very little in JavaScript. It validates the URL, generates a six character code, checks the code is not taken, and inserts a row. The insert is the part that touches the disk.&lt;/p&gt;

&lt;p&gt;SQLite in its default mode keeps a rollback journal and commits with synchronous FULL. Every commit is written and flushed to disk before it returns, and only one write can happen at a time. So each request queued behind the one before it. Divide the 277.7 ms mean by the 10 users and you get 27.7 ms of work for one request served on its own, which is the shape of a disk commit, not a calculation.&lt;/p&gt;

&lt;p&gt;The redirect side had the same problem in a smaller place. Every GET /:shortCode ran an UPDATE to add one click, so a read request was also a disk write.&lt;/p&gt;

&lt;p&gt;One more thing was wrong and Apache Bench could not show it. The latency histogram was labelled with the raw request path. Any request to a path that did not exist created a new set of labels in Prometheus, so a few thousand 404s made a few thousand series for one histogram.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four changes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. WAL mode, synchronous NORMAL, busy timeout
&lt;/h3&gt;

&lt;p&gt;Three PRAGMAs at startup. WAL lets readers read while a write is in progress instead of waiting on the journal. NORMAL stops flushing on every commit and syncs at checkpoints instead. The busy timeout makes a writer wait up to five seconds for the lock instead of failing straight away.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;journal_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;synchronous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;NORMAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;busy_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. An in-memory cache for redirect codes
&lt;/h3&gt;

&lt;p&gt;A Map from short code to the original URL. It is filled when a URL is shortened, read first on a redirect, and cleared when a URL is deleted. It holds 10,000 entries and drops the oldest when it is full. A hit redirects without touching SQLite.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Click counts buffered in memory
&lt;/h3&gt;

&lt;p&gt;Redirects add to a counter in memory instead of running an UPDATE. Every 5 seconds the buffer is written in one transaction, and the buffer is flushed on shutdown too. If a commit fails, the counts go back into the buffer.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Prometheus labels that do not multiply
&lt;/h3&gt;

&lt;p&gt;The latency histogram now uses the route pattern. A path that looks like a short code is labelled /:shortCode, so a 404 for a random path lands on one series instead of making a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this costs
&lt;/h2&gt;

&lt;p&gt;None of it is free, and the costs belong next to the change.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With synchronous NORMAL, a power cut can lose the last few seconds of commits. The database does not get corrupted, but the data can be behind. For a URL shortener on a laptop that is a fine trade. For anything financial it is not.&lt;/li&gt;
&lt;li&gt;The click buffer can lose up to 5 seconds of clicks if the process is killed hard. SIGTERM and SIGINT flush the buffer first, a crash does not.&lt;/li&gt;
&lt;li&gt;The cache lives inside one process. A second backend replica would keep its own cache, and a delete on one would not clear the other. This project runs one backend container, so I did not build that part.&lt;/li&gt;
&lt;li&gt;A cached redirect still counts its click, but the count is written up to 5 seconds later, so the click numbers in Grafana run a little behind.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The same test after the changes
&lt;/h2&gt;

&lt;p&gt;Same laptop, same Docker setup, same commands, with the four changes in place.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;10 users&lt;/th&gt;
&lt;th&gt;50 users&lt;/th&gt;
&lt;th&gt;100 users&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Before, requests a second&lt;/td&gt;
&lt;td&gt;36.01&lt;/td&gt;
&lt;td&gt;115.21&lt;/td&gt;
&lt;td&gt;85.93&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After, requests a second&lt;/td&gt;
&lt;td&gt;1128.40&lt;/td&gt;
&lt;td&gt;1605.70&lt;/td&gt;
&lt;td&gt;1752.33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Before, mean latency&lt;/td&gt;
&lt;td&gt;277.7 ms&lt;/td&gt;
&lt;td&gt;434.0 ms&lt;/td&gt;
&lt;td&gt;1163.7 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After, mean latency&lt;/td&gt;
&lt;td&gt;8.9 ms&lt;/td&gt;
&lt;td&gt;31.1 ms&lt;/td&gt;
&lt;td&gt;57.1 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Before, 95th percentile&lt;/td&gt;
&lt;td&gt;548 ms&lt;/td&gt;
&lt;td&gt;2057 ms&lt;/td&gt;
&lt;td&gt;4805 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After, 95th percentile&lt;/td&gt;
&lt;td&gt;13 ms&lt;/td&gt;
&lt;td&gt;41 ms&lt;/td&gt;
&lt;td&gt;68 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Apache Bench against POST /api/shorten on my laptop, 29 Nov 2026.&lt;/p&gt;

&lt;p&gt;At 10 users the same machine went from 36.01 to 1128.40 requests a second, and one request waited 8.9 ms instead of 277.7 ms. At 100 users it went from 85.93 to 1752.33, with 95% of requests under 68 ms instead of 4805 ms.&lt;/p&gt;

&lt;p&gt;ab also reported failed requests, and they are worth explaining rather than hiding. ab marks a request as failed when the response length is different from the first response it got. The shorten response contains the short code and the row id. The short code is random bytes turned into base64 with every character that is not a letter or a digit stripped, so its length is not always the same, and the id gains a digit as the table grows. That is what those counts are: 990, 9 and 6009 of the 1000, 5000 and 10000 requests before, and 991, 7 and 6006 after. There were no connect errors, receive errors, non 2xx responses or timeouts in either run.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to run it yourself
&lt;/h2&gt;

&lt;p&gt;You need Docker and Apache Bench, which is in apache2-utils on Debian and Ubuntu. 771a139 is the commit before the changes and 38b2752 is the one with them.&lt;/p&gt;

&lt;p&gt;Before the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/ahmed-145/containerized-url-shortener-monnitoring.git
&lt;span class="nb"&gt;cd&lt;/span&gt; containerized-url-shortener-monnitoring

git checkout 771a139
docker compose up -d --build backend
curl -s http://localhost:3000/health

&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'{"url":"https://example.com/test"}'&lt;/span&gt; \
  &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; post_data.json
ab -n 1000 -c 10 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
ab -n 5000 -c 50 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
ab -n 10000 -c 100 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the same tests on the commit with the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose down -v
git checkout 38b2752
docker compose up -d --build backend
curl -s http://localhost:3000/health

ab -n 1000 -c 10 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
ab -n 5000 -c 50 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
ab -n 10000 -c 100 -p post_data.json \
  -T application/json \
  http://localhost:3000/api/shorten
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;docker compose down -v throws the database volume away, so each run starts with an empty table. If you skip it, the second run has 16,000 more rows in the table and it is not the same test any more.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;The first lesson was about my own docs. I had 225 requests a second written down as a result, and the same code on my laptop did 36. A number in a README is not a measurement. It is a result from one machine on one day, and it needs running again.&lt;/p&gt;

&lt;p&gt;The second is the fix. The app was not slow because SQLite is small. It was slow because every request waited for the disk to answer. Taking that wait out of the request path moved the same laptop from 36 requests a second to 1128 at 10 users, and what I paid for it is the last few seconds of writes and a little accuracy in the click counts.&lt;/p&gt;

&lt;p&gt;The roadmap for this project had a move to PostgreSQL in a later phase. The measurement said the database was not the problem yet.&lt;/p&gt;

&lt;p&gt;Both runs are Apache Bench against POST /api/shorten. The 9 Nov 2025 numbers are from my week 4 doc, the 29 Nov 2026 numbers are from my laptop.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>performance</category>
      <category>node</category>
      <category>docker</category>
    </item>
    <item>
      <title>Why my Grafana alert never fired</title>
      <dc:creator>Ahmed Mahmoud Abbas</dc:creator>
      <pubDate>Sun, 29 Nov 2026 02:19:57 +0000</pubDate>
      <link>https://dev.to/ahmed145/why-my-grafana-alert-never-fired-1ipp</link>
      <guid>https://dev.to/ahmed145/why-my-grafana-alert-never-fired-1ipp</guid>
      <description>&lt;p&gt;I built a URL shortener for my capstone project and added Prometheus to collect metrics. I took over the Grafana part of the project, the dashboards and the alerts. Then the alerts stopped working. The cause was smaller than I expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  What went wrong
&lt;/h2&gt;

&lt;p&gt;When I opened Grafana on my localhost, it showed the metrics as unhealthy and no data was coming back from Prometheus. The alert rules failed with "data source not found".&lt;/p&gt;

&lt;h2&gt;
  
  
  How I found it
&lt;/h2&gt;

&lt;p&gt;I figured the problem was the ID, because that ID is the link between Grafana and the metrics. Grafana had created the Prometheus data source with an auto-generated ID, so the alert rules couldn't find it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;I set a fixed ID in grafana/provisioning/datasources/prometheus.yml, so it doesn't change every time I restart the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I checked it
&lt;/h2&gt;

&lt;p&gt;I waited a few minutes, then checked again, and the metrics arrived. I also checked the metrics in Prometheus and compared them, because Grafana is just the UI and the real data comes from Prometheus.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two more alert problems
&lt;/h2&gt;

&lt;p&gt;The ID was not the only alert problem I hit in that project.&lt;/p&gt;

&lt;h3&gt;
  
  
  The alert that never fired
&lt;/h3&gt;

&lt;p&gt;My latency alert was set to fire when P95 latency went over 100 ms. During a load test, Prometheus showed 520 ms, well over the threshold. The alert stayed Normal, green, and no notification came.&lt;/p&gt;

&lt;p&gt;The rule had a for: 2m condition, so the problem has to last two minutes before the alert fires. My load test started at 00:00 and I stopped it at 02:00. At 01:00 the condition was met and the alert went to Pending. At 02:00 the load stopped and P95 dropped to 45 ms. At 02:15 the next check found the condition gone, so the alert went back to Normal.&lt;/p&gt;

&lt;p&gt;The alert was working as designed. The test was too short. I changed the load test to run for 4 minutes, twice the alert duration.&lt;/p&gt;

&lt;h3&gt;
  
  
  The label that did not match
&lt;/h3&gt;

&lt;p&gt;An alert sat in Pending for over 10 minutes and never went to Firing, even though the condition was clearly met in Prometheus. The query was returning no data. One cause was a label mismatch: the alert rule asked for route="/api/shorten", and the metric was labelled route="/shorten". Different name, so the query found nothing.&lt;/p&gt;

&lt;p&gt;I checked the real labels on the backend with curl &lt;a href="http://localhost:3000/metrics" rel="noopener noreferrer"&gt;http://localhost:3000/metrics&lt;/a&gt; | grep http_request_duration_seconds, and changed the rule to the label the metric actually had. I also added OR on() vector(0) to the query, so an empty result returns 0 instead of leaving the alert pending.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;When a problem takes me too long, the cause is often something small and easy that I missed, because I was busy thinking about complicated, hard explanations.&lt;/p&gt;

</description>
      <category>grafana</category>
      <category>prometheus</category>
      <category>devops</category>
      <category>monitoring</category>
    </item>
  </channel>
</rss>
