<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[The Manasseh Adekola]]></title><description><![CDATA[The Manasseh Adekola]]></description><link>https://mannietech.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6a8fafe486730c5c86384c46/7be15eaa-d2d5-4592-81d0-e5feb6f3790e.png</url><title>The Manasseh Adekola</title><link>https://mannietech.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Dec 2026 21:10:57 GMT</lastBuildDate><atom:link href="https://mannietech.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[One login read 2,987 documents it had no right to see]]></title><description><![CDATA[Every endpoint in your API checks that the request comes from a logged-in user. That feels safe. Then an ordinary employee notices their document lives at…]]></description><link>https://mannietech.hashnode.dev/one-login-read-2987-documents</link><guid isPermaLink="true">https://mannietech.hashnode.dev/one-login-read-2987-documents</guid><category><![CDATA[Security]]></category><category><![CDATA[Python]]></category><category><![CDATA[systemdesign]]></category><category><![CDATA[system design interview]]></category><category><![CDATA[webdevelopment]]></category><dc:creator><![CDATA[MannieTech]]></dc:creator><pubDate>Thu, 03 Dec 2026 23:48:19 GMT</pubDate><content:encoded><![CDATA[<p>Every endpoint in your API checks that the request comes from a logged-in user. That feels safe. Then an ordinary employee notices their document lives at <code>/docs/1jk</code>, changes the URL to <code>/docs/1jl</code>, and is reading a stranger's file.</p>
<p>I simulated exactly that: an in-memory document API shared by 3 companies, with 300 users and 3,000 documents. The attacker is one normal employee with a valid login. Nothing gets hacked. They just ask.</p>
<h2>The number</h2>
<pre><code class="language-text">[1/4] Only the login is checked. IDs are sequential, so the attacker counts upward:
      read 2,987 documents belonging to other people, in 3 of 3 companies
</code></pre>
<p>One login read <strong>2,987 documents</strong> that belonged to other people, in every company on the system.</p>
<p>The API did check something. It checked <em>who</em> the user was. It never checked whether <em>that</em> user was allowed to see <em>that</em> document.</p>
<h2>Two questions that sound like one</h2>
<ul>
<li><strong>Authentication</strong> asks "who are you?". A login, a session or a token answers it.</li>
<li><strong>Authorization</strong> asks "may <em>you</em> do <em>this</em> to <em>this object</em>?". Only your own rules can answer it.</li>
</ul>
<p>An API that checks only the first has an <strong>IDOR</strong> (insecure direct object reference): the client sends an object's ID, and the server uses it without asking whether this user may touch that object. OWASP's list of API security risks calls it <strong>broken object level authorization</strong>, and in its 2023 edition it's number one.</p>
<h2>"Just use UUIDs"</h2>
<p>The usual first reaction is to make the IDs impossible to guess. So I switched the same broken API to random 7-character IDs:</p>
<pre><code class="language-text">[2/4] Only the login is checked. IDs are random, 7 characters:
      10,000 guessed IDs: read 0 documents
      25 IDs seen in shared links, emails and logs: read 25 of 25
</code></pre>
<p>Random IDs do stop counting: 10,000 guesses found nothing. But IDs don't stay secret. They end up in shared links, emails, screenshots, browser history and logs. All 25 that leaked still worked.</p>
<p>Random IDs <strong>hide</strong> documents. They don't <strong>protect</strong> them. OWASP's Authorization Cheat Sheet says the same: making IDs hard to guess "is generally not sufficient by itself".</p>
<h2>The fix</h2>
<img src="https://raw.githubusercontent.com/mannietech15/system-design-mastery/main/assets/25-authorization.svg" alt="The same request with a login check only, which leaks the document, and with a policy check on every request, which answers 404" style="display:block;margin:0 auto" />

<p>The whole policy lives in one function. Each <code>return True</code> is one rule, and reaching the end means no:</p>
<pre><code class="language-python">def is_allowed(user: User, action: str, doc: Document) -&gt; bool:
    """The policy. Every True below is one rule; falling off the end means "no"."""
    if action not in ACTIONS:
        # An unknown action is a bug in the caller. Raise, so it can't quietly become a yes.
        raise ValueError(f"unknown action {action!r}; expected one of {ACTIONS}")
    if user.org != doc.org:
        return False  # tenant isolation comes first: no rule below may cross orgs
    if user.id == doc.owner:
        return True  # owners may do everything to their own documents
    if action == "read" and user.id in doc.readers:
        return True
    if action in ("read", "delete") and user.is_admin:
        return True  # admins can read and remove anything in their org, but not edit it
    return False  # deny by default
</code></pre>
<p>Three choices in there matter more than they look:</p>
<ol>
<li><strong>Deny by default.</strong> A forgotten rule refuses someone. The opposite design, allow unless a rule says no, lets everyone in when a rule is forgotten.</li>
<li><strong>The company check comes first.</strong> In a system shared by many customers, no later rule, not even "admin", can cross from one customer to another.</li>
<li><strong>An unknown action raises an error</strong> instead of quietly becoming a yes or a no.</li>
</ol>
<p>Then every endpoint reaches documents through one function that applies the policy:</p>
<pre><code class="language-python">def _load(self, user: User, action: str, doc_id: str) -&gt; Document:
    # The ONE way endpoints reach a document. Putting the check here, instead of in each
    # endpoint, means a new endpoint can't forget it: it would have to bypass _load.
    doc = self._docs.get(doc_id)
    if doc is None:
        raise NotFound(f"no document {doc_id!r}")
    if self.check and not is_allowed(user, action, doc):
        if self.hide_existence:
            # Same exception and same message as a missing ID, so a scan learns nothing.
            raise NotFound(f"no document {doc_id!r}")
        raise Forbidden(f"{user.id} may not {action} {doc_id!r}")
    return doc
</code></pre>
<p>Read, edit, delete, share and list all go through it. A new endpoint can't forget the check without visibly going around it. That matters, because OWASP's cheat sheet puts it bluntly: "an attacker only needs to find one way in."</p>
<p>Same attacker, same data, now trying every real ID:</p>
<pre><code class="language-text">[3/4] Every request is checked against the policy, deny by default.
      The attacker tries all 3,000 real IDs: read 0 documents that aren't theirs.
      Refused 2,987; still readable: their 10 own and 3 shared with them
</code></pre>
<p>Zero. And the 13 documents they're entitled to still work. Good authorization refuses the wrong people, not everyone.</p>
<h2>403 or 404?</h2>
<p>When a user may not see a document, the API can say <strong>403 Forbidden</strong>, or pretend it doesn't exist with <strong>404 Not Found</strong>. It sounds like a detail. I sent 4,000 probes, half real IDs and half made up:</p>
<pre><code class="language-text">[4/4] What does a refusal SAY? 4,000 probes: 2,000 real IDs, 2,000 made up
      'not yours' answered as        403s      404s   real IDs it revealed
      403 Forbidden                 1,987     2,000                  1,987
      404 Not Found                     0     3,987                      0
</code></pre>
<p>A 403 confirms the ID is real. Answering 403 told the attacker which 1,987 of their probes were real documents, a target list for later. Answering 404 for both made a forbidden document look exactly like a missing one.</p>
<p>GitHub does this for private repositories: its API docs say it answers 404 instead of 403 "to avoid confirming the existence of private repositories."</p>
<h2>What interviewers listen for</h2>
<p>If a system design interview touches multi-tenant data, these are the red flags:</p>
<ul>
<li>"The user is logged in, so it's fine."</li>
<li>Taking the tenant ID, role or an <code>isAdmin</code> flag from the request instead of the server-side session.</li>
<li>"Our IDs are UUIDs, so nobody can get in."</li>
<li>Hiding a button in the UI and calling it authorization. Anyone can send the request directly.</li>
</ul>
<h2>Run it yourself</h2>
<p>Every number in this post came from a real run. The chapter is about 125 lines of plain Python, with nothing to install:</p>
<pre><code class="language-bash">git clone https://github.com/mannietech15/system-design-mastery
cd system-design-mastery/25-authorization
python demo.py
</code></pre>
<p>It's chapter 25 of <a href="https://github.com/mannietech15/system-design-mastery">system-design-mastery</a>: 25 system design concepts you can run, from rate limiters to Raft, now with a 4-week study plan and all 200 interview questions as Anki flashcards. This chapter also has two challenges that fail until you build them: spotting an IDOR scan in progress, and shares that expire.</p>
<p>If it's useful, a star is the easiest way to keep it within reach for your next interview, and it helps other developers find it too.</p>
<h2>Go further</h2>
<ul>
<li>OWASP, <a href="https://owasp.org/API-Security/editions/2023/en/0xa1-broken-object-level-authorization/">API1:2023 Broken Object Level Authorization</a>.</li>
<li>OWASP, <a href="https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html">Authorization Cheat Sheet</a>.</li>
<li>GitHub Docs, <a href="https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api">Troubleshooting the REST API</a>: why private resources answer 404.</li>
<li>Pang et al., <a href="https://www.usenix.org/conference/atc19/presentation/pang">Zanzibar: Google's Consistent, Global Authorization System</a> (USENIX ATC 2019): authorization at Google's scale.</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[I added one server to my cache and 80% of the keys moved]]></title><description><![CDATA[Adding a 5th server with hash % N moved 80% of cache keys. A hash ring moved 17%. Real numbers from a runnable Python demo, and the fix.]]></description><link>https://mannietech.hashnode.dev/add-one-server-80-percent-keys-move</link><guid isPermaLink="true">https://mannietech.hashnode.dev/add-one-server-80-percent-keys-move</guid><category><![CDATA[System Design]]></category><category><![CDATA[Python]]></category><category><![CDATA[distributed systems]]></category><category><![CDATA[interview preparation]]></category><category><![CDATA[caching]]></category><dc:creator><![CDATA[MannieTech]]></dc:creator><pubDate>Thu, 03 Dec 2026 00:17:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6a8fafe486730c5c86384c46/6bd54ce3-d984-4db1-9d4e-43c41915d7f2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Your cache is too big for one machine, so you split it across 4 servers. Every app server needs to agree on which one holds <code>user:42</code>, and the obvious rule is:</p>
<pre><code class="language-python">server = hash(key) % 4
</code></pre>
<p>It works fine. Then traffic grows, you add a fifth server, and the rule becomes <code>hash(key) % 5</code>.</p>
<p>I simulated exactly that with 10,000 keys. Here's what happened.</p>
<h2>The number</h2>
<pre><code class="language-text">[2/3] Add a 5th server. How many keys now live somewhere else?
      method                    keys moved    share
      hash(key) % N                  7,960    79.6%
      hash ring, 100 vnodes          1,656    16.6%
      The ideal is 1/5 = 20%. Of the ring's moved keys, 1,656 went to the new server
      and 0 moved between old servers.
</code></pre>
<p>Adding one server moved <strong>80% of the keys</strong>.</p>
<p>For a cache, "moved" means "missing". The app asks the server the new rule points to, the key isn't there, and the request falls through to the database. So your hit rate collapses and your database takes the full load at the worst possible moment: right when you were adding capacity because load was high.</p>
<p>The worst part is that most of those keys didn't even move to the new server. They moved between the old servers, which hadn't changed at all. <code>% 5</code> just reshuffles almost everything.</p>
<h2>Why % N breaks</h2>
<p>Take a key whose hash is 42. With 4 servers it lives on <code>42 % 4 = 2</code>. With 5 servers it lives on <code>42 % 5 = 2</code>. Lucky, it stayed.</p>
<p>Now try 43: <code>43 % 4 = 3</code>, but <code>43 % 5 = 3</code>. Also lucky. Try 44: <code>44 % 4 = 0</code>, <code>44 % 5 = 4</code>. Moved. 45: <code>1</code> then <code>0</code>. Moved. 46: <code>2</code> then <code>1</code>. Moved.</p>
<p>A key only stays put when both answers happen to match, and that's true for about 1 key in 5. Everything else moves. Changing N changes the answer for nearly every key.</p>
<h2>The fix: a hash ring</h2>
<p><strong>Consistent hashing</strong> puts servers and keys on the same circle, called the <strong>ring</strong>.</p>
<img src="https://raw.githubusercontent.com/mannietech15/system-design-mastery/main/assets/03-consistent-hashing.svg" alt="A hash ring before and after adding a server" style="display:block;margin:0 auto" />

<ol>
<li>Hash each server's name to a number and place the server at that point on the ring.</li>
<li>Hash each key the same way. The key belongs to the first server found walking clockwise from it.</li>
<li>When server E joins, it only takes over the keys in the arcs just before its points. Every other key keeps its server.</li>
</ol>
<p>That's why the ring moved 16.6%, close to the ideal of 1/5. And every one of those 1,656 keys moved <em>to the new server</em>. Zero moved between old servers.</p>
<h2>The catch: virtual nodes</h2>
<p>With one point per server, the gaps between points are random, so one server can own a huge arc and another a tiny one. Same simulation, 5 servers, ideal is 2,000 keys each:</p>
<pre><code class="language-text">[3/3] Why virtual nodes? Keys per server (5 servers, ideal 2,000 each):
      vnodes/server     fewest    most  most / ideal
      1                      7   6,369         3.18x
      10                   929   3,001         1.50x
      100                1,656   2,347         1.17x
      1000               1,925   2,082         1.04x
</code></pre>
<p>With one point each, the busiest server got <strong>3.18x</strong> its fair share, and the quietest got 7 keys. The fix is to give each server many points (<code>cache-a#0</code>, <code>cache-a#1</code>, ...), called <strong>virtual nodes</strong>. Each server then owns lots of small arcs, and the sizes average out. With 100 points each, the busiest server is at 1.17x.</p>
<p>Virtual nodes have a second benefit. When a server dies, its many small arcs are picked up by all the other servers, instead of one unlucky neighbour taking everything.</p>
<h2>A bug you might not expect</h2>
<p>You might reach for Python's built-in <code>hash()</code>. Don't:</p>
<pre><code class="language-text">PYTHONHASHSEED=1: hash('user:42') % 4 = 1
PYTHONHASHSEED=2: hash('user:42') % 4 = 2
</code></pre>
<p>Python salts string hashes with a random value per process, so two app servers would disagree about where the same key lives. Every client must compute the same number, so use a fixed hash. The demo uses MD5, which is fine here because the goal is an even spread, not security.</p>
<h2>What interviewers listen for</h2>
<p>If you're asked to shard a cache in a system design interview, these are the red flags:</p>
<ul>
<li>Sharding with <code>hash(key) % N</code>. Adding one server moves about 80% of keys.</li>
<li>Using a language's built-in hash that differs between processes.</li>
<li>Consistent hashing without virtual nodes. One server got 3.18x its share.</li>
<li>Claiming consistent hashing fixes hot keys. It balances key <em>counts</em>, not traffic. A single viral key still lands on one server.</li>
</ul>
<h2>Run it yourself</h2>
<p>Every number in this post came from a real run, not from a slide. The whole thing is about 150 lines of plain Python, with nothing to install:</p>
<pre><code class="language-bash">git clone https://github.com/mannietech15/system-design-mastery
cd system-design-mastery/03-consistent-hashing
python demo.py
</code></pre>
<p>This is chapter 3 of <a href="https://github.com/mannietech15/system-design-mastery">system-design-mastery</a>, 24 system design concepts you can run, from rate limiters to Raft to distributed tracing. Each chapter has a demo, tests, interview questions and two challenges that fail until you build them.</p>
<p>If it's useful, a star is the easiest way to keep it within reach for your next interview, and it helps other developers find it too.</p>
<h2>Go further</h2>
<ul>
<li>DeCandia et al., <a href="https://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf">Dynamo: Amazon's Highly Available Key-value Store</a> (SOSP 2007), sections 4.2 and 4.3.</li>
<li>Apache Cassandra, <a href="https://cassandra.apache.org/doc/latest/cassandra/architecture/dynamo.html">Dynamo-style architecture</a>: tokens and virtual nodes.</li>
<li>Lamping and Veach, <a href="https://arxiv.org/abs/1406.2294">A Fast, Minimal Memory, Consistent Hash Algorithm</a> (2014).</li>
</ul>
]]></content:encoded></item></channel></rss>