<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Irrelon Software on Medium]]></title>
        <description><![CDATA[Stories by Irrelon Software on Medium]]></description>
        <link>https://medium.com/@irrelon?source=rss-f0af76813d31------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BQ1_Wk7cE-kvcf5K.png</url>
            <title>Stories by Irrelon Software on Medium</title>
            <link>https://medium.com/@irrelon?source=rss-f0af76813d31------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:18:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@irrelon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Securing APIs at Infinite Scale]]></title>
            <link>https://medium.com/@irrelon/securing-apis-at-infinite-scale-c67f0f28bc3?source=rss-f0af76813d31------2</link>
            <guid isPermaLink="false">https://medium.com/p/c67f0f28bc3</guid>
            <category><![CDATA[stateless]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[scalability]]></category>
            <dc:creator><![CDATA[Irrelon Software]]></dc:creator>
            <pubDate>Thu, 03 May 2018 08:45:32 GMT</pubDate>
            <atom:updated>2018-05-03T09:25:34.535Z</atom:updated>
            <content:encoded><![CDATA[<p>So you’ve built / are building an API and want to ensure it is well secured but also need your API to scale without putting a massive strain on your database layer? Read on fellow traveller…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5Ex3rna17jjZYdWu." /><figcaption>Infinite Scale APIs Require Stateless Architecture</figcaption></figure><h3>Traditional (Old Fashioned) API Security</h3><p>In the olden days in the long long ago, APIs were sometimes secured using a session ID or token that the user was given when they logged in and was subsequently passed up to the API when making requests. The session ID would be unique to the user so the API could identify who was making the request.</p><p>Once the API had received a request with a session ID, it would go and do a lookup on a database or key/value store or whatever which allowed it to map the session ID to the user record. Once the user record was retrieved the API knew what permissions the user had, who they were and their account details etc. This model works really well for hundreds, even thousands of requests per hour, but what about tens of thousands? What about millions? As soon as you start to receive more API requests, you also have to make more database or key/value store requests to map the session ID to the user. This will not scale.</p><p>This model also suffers from a horizontal scaling issue. If you add more servers to take on more requests, now you have another problem. You have to replicate the data on database server A to database server B so they both know the user in question has a session ID that maps to their account. Every time you log a user in you are generating a new session ID that has to be sent to all your database servers so they are in sync. More and more traffic zipping around just to handle authentication and authorisation!</p><h3>How Do We Scale?</h3><p>In computer programming, the fastest code is the code that never runs. In the case of API security the fastest code is the code that never asks a database for a user record before it performs the requested action. Hitting a database or key/value store or whatever is what we want to avoid.</p><p>Imagine a scenario where the user can provide you with their details, their account ID, their username, their permissions and you could just trust them that they were correct? What if the API call went like this:</p><ol><li>User sends API request to delete a post and says, “I have permission to delete this post”.</li><li>Server looks at the permissions the user sent up and says, “Yup ok, you told me you have permission so I will do as you say”.</li><li>Server takes the requested action without EVER checking on a database or key/value store that the user is who they say they are, or has the permissions they say they do.</li></ol><p>Madness you cry? Indeed it is unless you have a magical way to determine if the user is lying or not. Enter the magical solution: asymmetrical encryption.</p><h3>Asymmetrical Encryption</h3><p>So let’s talk briefly about asymmetrical encryption. This type of encryption generates two “keys” (or long-ass strings of letters, numbers and symbols that mean nothing to a human being). These two keys are special. They are bound to each other. If I encrypt a piece of text with one of the keys, I can only *decrypt* that text using the other key and vice versa.</p><p>In laymans terms this system of encryption solves a really big problem of how I can keep something secret. Imagine that you want to send a secure letter to your friend “Jim”. Jim lives hundreds of miles away so you want to use the postal service. If you post the letter to Jim someone along the way could open your letter, read it, put it in a new envelope and deliver it. Jim would never know and your letter would have been made insecure.</p><p>How about we do this a different way?</p><ol><li>Jim sends you an incredibly secure metal box with an incredibly secure padlock on the front for which Jim has the only key. The padlock has been left unlocked. This arrives at your house.</li><li>You place your letter inside the box, close the padlock and send it back to Jim, and you also send your own box with your own padlock that only you have the key to. Along the way, no-one can open Jim’s box with your letter in it, the box is locked and secure.</li><li>The box arrives at Jim’s house and he unlocks the padlock with his key, takes the letter and reads it. After writing his reply letter he takes the box you sent him, puts his reply letter in it and closes the padlock and sends it back to you.</li><li>You receive your box back with Jim’s reply and unlock the padlock with the key only you have.</li></ol><p>That is secure communication in a nutshell and how asymmetrical encryption works. You hold a key allowing your padlock to be unlocked.</p><p>How do we apply this to API security? How can we trust the user’s assertion that they are *allowed* to delete a post? The user’s assertion might look like this:</p><p><strong>Figure 1: User Assertion Object</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*IK-Odg7IwbIw1BCpao8PIg.png" /></figure><ol><li>User logs on to the system. When they do so instead of us providing a session ID, we take the user’s data that we are ok being publicly accessible such as their username, user ID, what permissions they have (such as being allowed to delete their own posts) etc.</li><li>After we have built all the data we want to receive each time the user talks to our API, we digitally “sign” it using our private key. Only we know our private key. Taking the user’s data as the input, our private key as the encryption key, we encrypt the user’s data and out comes a long string of characters.</li><li>We send both the user’s data AND the encrypted version which we call our “signature” to the user.</li><li>When the user sends a new request to the server, we decrypt the signature and check that it matches the user data they also sent up. If it doesn’t the user is trying to lie. If it does, we can trust that the user is telling the truth and allow them to take action.</li><li>This means we never need to look up the user data in the DB since the client sends it to us!</li></ol><p>And that is it. Modern, scalable API security.</p><h3>But What About…</h3><p>So at this point most if you will probably be thinking… this all looks great in theory but what about a serious glaringly obvious problem: session invalidation.</p><p>Session invalidation is the act of “killing” a user session. A good example of when this might be required is where the user changes their password or wants to log all other clients out of their account, or perhaps you as the API developer want to make sure that sessions don’t persist forever etc etc. There are many reasons to want this feature.</p><p>This is the trade-off with stateless token-based auth, you either lose the ability to invalidate a single token (per user), invalidate ALL tokens (change your signing key), or opt for a fairly short expiry (like 10 minutes or something). Nothing is a perfect system.</p><p>In a stateless world where you trust the signed user data the only way to handle this without a DB lookup is to set an expiry on the token you generate and make it short, like 1 hour or less, then store an in-memory blacklist of users who have updated their password or logged out all sessions etc and clear that blacklist data every hour as well. This still requires an annoying replication of data between in memory stores, but the number of users logging out / invalidating their sessions every hour will be orders of magnitude less than those logging in so traffic in replication of this blacklist will be small.</p><p>If anyone has a better way of handling session / token invalidation please leave a comment! The ideal solution would reduce the requests to a DB or key/val store back to zero.</p><h3>Implementation</h3><p>For more information about this type of token-based authentication and authorisation, check out JSON Web Tokens (JWT) here: <a href="https://jwt.io/">https://jwt.io/</a></p><p>There are loads of implementations including DIY options, or SaaS options if you prefer.</p><p>Enjoy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c67f0f28bc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>