<?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 Denis Dubo-Chevalier on Medium]]></title>
        <description><![CDATA[Stories by Denis Dubo-Chevalier on Medium]]></description>
        <link>https://medium.com/@denis.chevalier?source=rss-3c25ef78a37e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_cb3TYcDHU5du2xIF91j0w.jpeg</url>
            <title>Stories by Denis Dubo-Chevalier on Medium</title>
            <link>https://medium.com/@denis.chevalier?source=rss-3c25ef78a37e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 20:10:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@denis.chevalier/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[Scaling the bank]]></title>
            <link>https://medium.com/qonto-way/scaling-the-bank-ae69ce951ca?source=rss-3c25ef78a37e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae69ce951ca</guid>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[product-design]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Denis Dubo-Chevalier]]></dc:creator>
            <pubDate>Fri, 31 Jul 2020 09:04:06 GMT</pubDate>
            <atom:updated>2020-07-31T13:01:55.460Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Qonto tech&amp;product" src="https://cdn-images-1.medium.com/max/960/1*BGliNhsfkv3o4DkO0xkHTw.gif" /></figure><p><strong>TL;DR:</strong> <strong>The Qonto Ledger</strong> is responsible for keeping track of all transactions and balances. We achieved a great deal of scalability through careful performance optimizations. We’re planning to get to the next level by improving the separation of concerns between <strong>the Ledger</strong> and <strong>payment systems.</strong></p><p>At Qonto, we create a product that is easy to use for our internal clients (other cross-functional teams, operations and back-office team, finance team, …). To that end, we need <strong>the Ledger</strong> to be implemented in the simplest way possible, following the <a href="https://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>.</p><h3>Introduction</h3><p>Where do we come from? At Qonto, we have always wanted to deliver a quality product and to deliver it fast. To that end, we first created our banking product based on an external core banking system (CBS). This lasted for roughly our first two years of activity.</p><p>This allowed us to take some time to build our own CBS, somewhat in a “fast to market” way. To be able to ship fast, without losing on the quality of the existing product, we decided that this CBS should mimic the API of our external partner: that way, only the CBS would have to change. It was a great idea and helped us deliver it on time.</p><p>Why did we want our own CBS though? Third-party solutions either target big banks (with a price tag that follows) or develop features on their end, making them the bottleneck of our ambition. As soon as our first clients started to be onboarded on our CBS, we were able to provide them more targeted features: more <strong>payment systems</strong> like SWIFT and checks, international deployment in Germany, Italy, and Spain with local IBANs, etc. Also, by having our own CBS, we keep control over the quality, which is at the core of our value proposition.</p><p>However, with each new feature comes a cost: our CBS has always been kind of monolithic. There are dedicated services for dedicated tasks like transaction authorization, accounting, SEPA, and more. But the coupling of those services is high. We lack a clear framework to easily add new <strong>payment systems</strong> and features.</p><p>That is why we decided to take a step back and look into how we could improve on the existing to provide this framework. How could we make the bank we all love continue to grow without increasing the product and tech debt? That’s when we decided to make <strong>the Ledger</strong> <strong>agnostic</strong>.</p><h3>Definitions</h3><p>Before we go any further, let’s define some important terms that we’ll use throughout this article:</p><p><strong>What is the Ledger?</strong> From a technical point of view, <strong>the Ledger</strong> is a big storage unit that keeps each state of each transaction for each of our clients, to provide accounting and authorization. It is the set of services built around that store that permits financial, back-office, and accounting operations. Those services are responsible for ensuring that our regulatory constraints are met regarding statements, funds protection, exports, etc. The authority that enforces those constraints is the <a href="https://en.wikipedia.org/wiki/French_Prudential_Supervision_and_Resolution_Authority">French Prudential Supervision and Resolution Authority (ACPR)</a>.</p><p><strong>What do we mean by agnosticism?</strong> In the context of this article, let’s define <strong>agnosticism</strong> as being able to operate without any internal knowledge of the origin of an operation. For instance, a new transaction in <strong>the Ledger</strong> does not need to hold any information about the network it comes from.</p><p><strong>What is a payment system?</strong> A <strong>payment system</strong> is an abstraction of all operation (transaction statuses) originators in <strong>the Ledger</strong>: SEPA Direct Debit (SDD) is a <strong>payment system</strong>, as well as SEPA Credit Transfers. Card is another, and check, and SWIFT. To be able to become <strong>agnostic</strong>, we must be able to find the least common denominator between all of those services. This is the <strong>payment system</strong>.</p><h3>Technical summary — to date</h3><p>Let’s dive a little bit into the inner workings of the current CBS, shall we?</p><h3>How does it work?</h3><h3>Core principles</h3><p>CBS is constructed in a few blocks:</p><ul><li><strong>Payment systems</strong> like SWIFT, card, etc. handle connections to external networks and all transactions transiting through one of those.</li><li>The <strong>Back office</strong> block handles the actions of the operations team.</li><li>And all of those blocks are connected to <strong>the Ledger</strong>.</li></ul><p>This last block is responsible of:</p><ul><li>Handling the authorization of all transactions and keeping track of balances;</li><li>Keeping all clients accounts statuses;</li><li>Making the financial accounting as well as enforcing the regulatory constraints;</li><li>Interoperating with partner banks.</li></ul><p><strong>The Ledger</strong> block must be fast (for authorizations), accurate and auditable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d9Yp14GrL9ZNt9bpLyMsBw.png" /></figure><h3>A case study</h3><p>This is all very abstract, so let’s dive into an example flow of a transaction so that you can see how all of this works. Let’s, for instance, imagine that you just went to your local bakery to buy an amazing pie. It costs €10. What happens at Qonto then?</p><ol><li>When you enter your card into the terminal (POS), it sends an <strong>authorization request</strong> to MasterCard.</li><li>MasterCard then determines that you are a Qonto client: they need our approval before allowing you to pay. They transfer the <strong>authorization request</strong> to the <strong>Card</strong> payment system service.</li><li>From there, the <strong>Card</strong> service checks if the request is technically valid, then sends it to <strong>the Ledger</strong></li><li><strong>The Ledger</strong> then checks your balance, the limits of your card, your options, and decides on whether or not you can pay. For the sake of the example, let’s say you have the funds, and <strong>the Ledger</strong> authorizes your payment.</li><li>The <strong>authorization responses</strong> then come back to <strong>Card</strong>, then to MasterCard, and eventually, the POS tells you “payment accepted”.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlB9O_3PJXJ6SZm93R2kFw.png" /></figure><p>That’s it, you’ve got your pie, everything is fine and done. Or is it? Actually, at this point, not a single cent has left your account. Not a single cent has reached the bakers’ account as well. All that happened is us, Qonto, promising MasterCard on your behalf that when the times come, we will pay them the €10 requested. And then we will debit you.</p><p>Let’s take some time to analyze the consequences of this: you have a limit on your card and you are not allowed to let your account go negative. How do we handle this when not a single cent has left your account? How do we prevent you to reach those limits before the funds move?</p><p>For that, we use a concept known as <strong>authorized balance</strong>. This is a secondary balance (not the financial one), computed and stored by <strong>the Ledger</strong>, that takes into account all your pending transactions. This is the main balance we show you in the user interface. This is, at any given point of time, your <strong>liquid</strong> funds, that you can use.</p><p>But this <strong>authorized balance</strong> has no meaning from an accounting (and accountant) point of view: as said before, some of the funds are still yours. The actual balance of your account is what we call <strong>settled balance</strong>. What happens next then? Let’s continue with our previous example, shall we?</p><ol><li>At the end of the day, the baker wants to get paid: he goes to his POS and asks for <strong>clearing</strong> of all his transactions of the day.</li><li>The POS sends the requests to MasterCard, who dispatches it to all the clients’ banks (including us).</li><li>We receive a big file with all the operations that you and our other clients have made. This is an order of transfer, to pay the bank of the vendor, like our baker. They will then transmit the funds to the individuals.</li><li><strong>Card</strong> parses the file, it acknowledges <strong>The Ledger</strong> of the new status from the transaction: it is now final and cannot be changed anymore. The file is the definitive source of truth about the amount and status of the transaction.</li><li><strong>The Ledger</strong> then orders your funds to be moved:<br>a) Out of the <strong>virtual vault</strong> holding your funds to a <strong>transit account</strong> in our partner bank that holds the funds by ACPR regulation,<br>b) Out of the <strong>transit account</strong> (aggregated with all card transaction funds for the day) to MasterCard,<br>c) The next day it checks with the external bank that the payment has been successfully done and marks the transaction as completed. This process is called <strong>reconciliation</strong>.</li><li>In the meantime, <strong>the Ledger</strong> maintains a mirror image of all the movements on our partner bank accounts in its own storage.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gAcSmXsVCyPx-BReDnTGqw.png" /></figure><p>And <em>voilà</em>, the funds have finally left your account and your pie is long eaten — it was delicious, wasn’t it?</p><p>Your <strong>settled balance</strong> is now debited as well (you can see it in the interface by clicking on the <strong>authorized balance</strong>), the transaction is no longer <em>pending</em> in your history, it will show up on your next <strong>account statement</strong>. (By the way, this shows why sometimes your <strong>settled balance</strong> is different from your <strong>authorized balance.</strong>)</p><h3>Adding a new actor</h3><p>We’ve seen how everything works currently. Let’s now see what we would have to do to handle a new <strong>payment system</strong> with our current architecture: Instant SEPA, for instance:</p><p>We need to connect to the SEPA network (we already have a service for that). Then we need to update the SEPA service to take into account this whole new <strong>payment system</strong>: how to orchestrate it, when to authorize and settle it, etc.</p><p>Then in <strong>the Ledger</strong>, we have to add the rules for the authorization of this specific kind of operation. For Instant SEPA this would be:</p><ul><li>Are the limits for Instant SEPA reached? (and update them)</li><li>Check that the account has the right options.</li></ul><p>Similarly, we have added a whole set of rules:</p><ul><li>Which internal accounts are impacted by the <strong>payment system</strong>?</li><li>How to route transactions to those accounts?</li><li>Which operations have to be performed on external banks? (Which banks, how?)</li><li>How do we check that everything worked as intended (for accountability)?</li><li>etc.</li></ul><p>As you can see, this is not only limited to the <strong>Transfers</strong> block but we, <strong>the Ledger</strong> also have to modify our services to account for the new <strong>payment system</strong>. And we must do so without impacting any existing <strong>payment system</strong>, nor impacting the performances.</p><h3>Are we happy with it?</h3><p>So, what are the advantages and disadvantages of this way of working?</p><h3>Pros</h3><ul><li>For start, the current CBS implementation was fast to implement, and at least at the beginning, adding new <strong>payment systems</strong> was also quite fast.</li><li>As a <strong>building-from-scratch</strong> strategy, it helped us learn a lot about the specificities of an SME targeting banking system, things that you won’t find in any book. We learned a lot and continue to learn!</li><li>Also, having big fat ledger storage makes it easy to monitor and debug: all information being centralized, as well as all logic, we only have one point to look into to analyze any misbehavior of the system. This was crucially important at the very beginning of the CBS, as we needed to make sure that it is reliable for you to use.</li></ul><h3>Cons</h3><p>On the other hand, there are a few issues with this way of working:</p><ul><li>Performance and scalability wise, we needed to spend a lot of time in the optimization of code and queries to make sure that this would scale: When you put all the logic in one place, it is more likely that you will end up with some sub-optimizations of the code.</li><li>Adding new <strong>payment systems</strong> (or even new operation types on existing ones) is proving more and more difficult as time goes by, as the complexity of the systems increases with each new one.</li><li>Onboarding new developers on the team is becoming longer, has the sum of information needed to perform even the simplest change is also growing exponentially.</li><li>Developers and product managers from other teams also have to have some knowledge about how <strong>the Ledger</strong> works, making their work more and more difficult.</li><li>Most bug fixes require cooperation between two teams, leading to slower fixes.</li></ul><h3>Why is it not agnostic yet?</h3><p>You may then ask “Why is it not <strong>agnostic</strong> yet, then?” The truth is, being <strong>agnostic</strong> is not that easy. <strong>Payment systems</strong> are inherently different:</p><ol><li>They follow several, not inter-compatible protocols (you can look online for the <a href="https://en.wikipedia.org/wiki/ISO_8583">ISO8583</a> card protocol and the <a href="https://en.wikipedia.org/wiki/ISO_20022">ISO20022</a> SEPA protocol for instance)</li><li>Every <strong>payment system</strong> follows a different calendar for the various operations of a transaction (rejection, cancelation, settlement, etc.) with its own set of rules to apply them.</li><li>Every <strong>payment system</strong> as its own set of internal accounts (<strong>transit</strong>, <strong>abnormal handling</strong>, etc.): we need to be able to operate them.</li><li>Every single transaction doesn’t hold the same level of risk: some <strong>payment systems</strong> are <em>safer</em> (for transactions inside the eurozone for instance). We need to acknowledge that with different accounting rules.</li></ol><p>This is why <strong>the Ledger</strong> is full of payment-specific rules.</p><h3>Scalability via performance</h3><p>Nevertheless, we already attained a pretty high level of performance and stability via continuous improvement of the existing codebase.</p><p>We did it in multiple phases: We knew from the start that optimization of database queries would be critical, so we organized it in a way that would minimize the reads necessary to perform any operation. We also took some long sessions in analyzing the queries performances, to optimize their use of indexes, cache, and memory.</p><p>We also decided early on to minimize the time spent on the network: using big prepared queries, and putting the logic in them to avoid too many exchanges between <strong>the Ledger</strong> and the database cluster.</p><p>However, we still have some inherent scalabilities issues. Due to the fast growth of Qonto — new customers, new countries, new features — the transaction rate grows exponentially. This makes scalability a primary concern. Having more and more clients at Qonto means that we need new features and new <strong>payment systems</strong> to be added, to suit your needs.</p><p>The needs of our internal clients such as ops and finance are also growing. We need tooling to assess the risks, to handle <em>abnormal transactions</em> (for instance transactions on closed or lost cards — they won’t impact you, but we need to deal with them on a daily basis). We need to be able to report to the ACPR, to ensure that we follow their guidelines, etc.</p><p>Consequently, we had to put some DevOps in our scalability strategy. Came into consideration, not only the resources associated with our Kubernetes pods and databases but also the localization of our servers to ensure fast network exchanges.</p><p>And while we scale, we must remain as stable as possible: We built extensive non-regression test suits and added monitoring at every level of CBS. We built procedures to handle unexpected events. We stress-tested every aspect of the software and database.</p><p>That is the story of how we dealt with scalability issues up until now. This has proved useful, <strong>the Ledger</strong> is still very stable, and we have a lot of headroom to handle more and more clients.</p><p>We could have continued on this track, and we would still be fine in one year. What about after? What about the long term? The system is becoming more and more complex, and adding new features and tooling becomes more and more costly. We need to take a step back, digest all the experience we’ve gained since we wrote the first line of code on the CBS, and imagine a truly scalable solution: We need to put back some simplicity in the system. We need to be able to plug in any <strong>payment system</strong> painlessly. We need <strong>agnosticism.</strong></p><h3>Scalability via agnosticism</h3><h3>Why?</h3><p>Indeed, <strong>agnosticism</strong> in architecture design brings a lot of benefits:</p><p>If the system is <strong>agnostic</strong>, then we can delete all <strong>payment system</strong> specifics from the code: mathematically, we reduce the risk of bugs. We also have a better <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a>, to ease the maintenance of the codebase.</p><p>A truly <strong>agnostic</strong> system also allows us to ship faster: only one team needs to work to add a <strong>payment system</strong>, <strong>the Ledger</strong> stays the same. We don’t lose time in meetings across various teams. We don’t need to accommodate the rhythm of features and can continue or main work: keeping ledger stable, auditable, and accurate.</p><p>An <strong>agnostic</strong> system also provides a lot of benefits for our internal teams: the ops only need to learn one pattern to analyze transaction sequence from any <strong>payment system</strong>. The finance controls are easier. Bug resolution can now be performed by one team at a time, etc.</p><p>Finally, adding new <strong>payment systems</strong>, launching Qonto in other countries, adding new partner banks, all those operations become a lot easier and faster. We can dream big!</p><h3>Case study</h3><p>Let’s review our previous case study, but this time with the new <strong>agnostic</strong> system, shall we? This way we will be able to show the principles and technical choices behind our new <strong>agnostic</strong> architecture.</p><p>You decided to buy another one of those delicious pies. You went to the bakery, chose it, and put your card into the POS.</p><ol><li>Then, the POS contacts MasterCard, which sends an <strong>authorization request</strong> to the <strong>Card</strong> block.</li><li><strong>Card</strong> parses it, and checks that the card is active, the limits are respected, the options are respected. If it weren’t the case, we would immediately refuse the transaction.</li><li>It calls <strong>the Ledger</strong> to request authorization of the transaction. <strong>The Ledger</strong> doesn’t need to know that this is a card operation. All it needs to know is that a debit of €10 has been requested for your account. It checks the account status and balance, and answer: yes, you have the funds.</li><li><strong>Card</strong> answers to MasterCard with an <strong>authorization response</strong>. You have your pie.</li><li>In the meantime, <strong>Card</strong> pushes the <em>pending</em> transaction to your history so that you can see it, edit the VAT rate, add invoice, etc. and <strong>the Ledger</strong> pushes your new authorized balance so that you have an up to date view on it.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c2fgvqpPTRT5SHbmW2HE8A.png" /></figure><p>As you can see here, this already allows some profound optimizations: By putting back the card-specific logic in the <strong>Card</strong> domain, we can implement early return paths. We also are allowed to have a much simpler authorization service, that can be blazingly fast at answering your request.</p><p>Then comes the second part of the payment: the actual movement of the funds. Let’s look into it:</p><ol><li>The baker collects all is payments from the day and asks the POS to be paid. MasterCard dispatches it to the client banks, and <strong>Card</strong> receives the <strong>clearing</strong> file.</li><li><strong>Card</strong> parses it, and finds your transaction. It compares it with its status, and checks on <strong>the Ledger</strong> to see if your account is still open. It is!</li><li><strong>Card</strong> then creates an asynchronous message with all the information of your payment and pushes it on a <strong>payment-agnostic</strong> topic: your account needs to be debited of €10, to be paid at MasterCard with a specific id (MasterCard will then pay the baker’s bank).</li><li><strong>The Ledger</strong> consumes the message, stores the transaction on your account, and moves the funds out of the <strong>virtual vault</strong> to the <strong>transit account</strong> on the partner bank.</li><li>In the meantime, <strong>Card</strong> prepares another asynchronous message to indicate that it expects a total movement for the whole <strong>clearing</strong> file to be sent to MasterCard.</li><li><strong>The Ledger</strong> consumes this message, orders the partner bank to move the funds to MasterCard and stores it as an expected movement.</li><li>The day after, when it checks the actual fund movements on our partner banks (<strong>reconciliation</strong>), it compares it with the expected movement to ensure that everything worked well.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jeq01ULWN6sKp5m0Tn7u_w.png" /></figure><h3>How?</h3><p>As you can see, we are now able to operate <strong>the Ledger</strong> without the need to know who calls it. To achieve that, we had to compare all of our <strong>payment systems’</strong> transactions to find their greater common divisor. This would become what we call a <strong>ledger movement</strong>.</p><p>A <strong>ledger movement</strong> consists of a few information:</p><ul><li>A transaction identifier, that links movements together;</li><li>A movement identifier;</li><li>An amount (with currency and direction);</li><li>A few status fields that are used to set the accounting rules used for the movement;</li><li>A few introspection fields used to facilitate the debugging.</li></ul><p>To achieve <strong>agnosticism</strong>, we also need to unify exchanges: before the changes, almost all <strong>payment systems</strong> used different endpoints to communicate with <strong>the Ledger</strong>. Now we only need a limited set of HTTP endpoints for <strong>the authorization process</strong> (which has to be fast), and asynchronous messaging consumers for everything else (which should be asynchronous).</p><p>With that model and interface, we can now focus on the inner workings of <strong>the Ledger</strong>: We can strip down all <strong>payment system</strong> specifics from the code:</p><p>In <strong>the Ledger</strong>, we are moving from a <em>distinct set of rules</em> for each <strong>payment system</strong> toward a <em>ruleless</em> way of working: All internal and external accounts are now tied to real accounts (with IBANs, etc). The <strong>payment systems</strong> are responsible for the funds that transit through them, so they are able to notify <strong>the Ledger</strong> when funds need to be moved. For reconciliation, as we receive an expected remittance from the <strong>payment systems</strong>, we are now able to simply make a comparison between an expected entry and the actual one. All operations on <strong>the Ledger</strong> are simply new rows in our double-entry bookkeeping that mirrors our partner banks.</p><p>This means transferring back responsibility to the relevant teams. This is a complex yet gratifying task still currently ongoing. Once done, we will be able to greatly improve the data model and database performances, as well as greatly reduce the complexity of the authorization algorithm.</p><p>To show it, let’s consider what are the entries for our case study:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06a695276506c1471a80fee42b34c703/href">https://medium.com/media/06a695276506c1471a80fee42b34c703/href</a></iframe><p>Having all of our operations recorded in that double-entry bookkeeping way allows us to easily integrate with our finance team tooling. We can easily generate exports, provide metrics, etc.</p><p>We have multiple internal accounts like the <em>transit account</em>. Notably, we have accounts to hold abnormal transactions or fraudulent ones. This allows us to easily show them to the operations and back-office team, and to permit them to easily operate on those.</p><p>With this, you see how easy it is to plug in a new <strong>payment system</strong> or partner bank to <strong>the Ledger</strong>:</p><ul><li>Add rules in <strong>the Ledger</strong> on how to contact the partner bank (on which accounts, with which <strong>payment system</strong>, etc.), <strong>if they don’t already exist</strong>.</li><li>Create the <strong>payment system</strong> service to target the <strong>agnostic</strong> API of <strong>the Ledger</strong>.</li></ul><p>And we’re done. We no longer have a tight coupling between the <strong>payment system</strong>-specific logic and <strong>the Ledger</strong> logic. We only have to make sure that the <strong>payment system</strong> nows how to communicate with <strong>the Ledger</strong>.</p><p>Of course this architecture change — that we are currently deploying for all our existing and new <strong>payment systems — </strong>is not the end of the game. A lot of the communication from or to the <strong>payment systems</strong> is redundant (the SEPA <strong>payment system</strong> makes similar calls than check <strong>payment system</strong> for instance).</p><p>Having a long-term vision is what permitted us to be the neobank you all love. We might change it with experience, but the ambitious goal drives us to always create the product you love.</p><h3>Towards credit license</h3><p>And now for the icing on the cake, let’s see what wonderful change being <strong>agnostic</strong> will allow us to make to Qonto: A credit license! To get it, we need ACPR approval, but we also need a lot of changes in <strong>the Ledger:</strong></p><ul><li>We will be able to operate with a large number of partner banks.</li><li>We will be able to add multiple new <strong>payment systems</strong>. We can even invent our own!</li><li>We will be able to provide you many features we’re sure you’ll love.</li></ul><p>Because all of this can now be done with far less code than before and a better separation of concerns, it means that we can focus on the innovation. Less is more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae69ce951ca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/qonto-way/scaling-the-bank-ae69ce951ca">Scaling the bank</a> was originally published in <a href="https://medium.com/qonto-way">The Qonto Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we ensure a fast response time for card authorization]]></title>
            <link>https://medium.com/qonto-way/how-we-ensure-fast-response-time-for-card-authorization-1004afaf21ab?source=rss-3c25ef78a37e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1004afaf21ab</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[banking]]></category>
            <category><![CDATA[relational-databases]]></category>
            <dc:creator><![CDATA[Denis Dubo-Chevalier]]></dc:creator>
            <pubDate>Wed, 13 Mar 2019 09:44:27 GMT</pubDate>
            <atom:updated>2019-03-14T11:06:14.708Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DK8oeJ3Lb8EHLJl1JH0ebw.png" /></figure><h4>Service design at Qonto</h4><p>At Qonto, we provide a bank account and credit cards to SMEs and entrepreneurs. Having a world-class card authorization system is a top priority: we face tens of thousands of card transactions every day and growing!</p><p>This is why we chose to build our own core banking system instead of using a third-party solution. This choice will allow us to provide you with new and innovating features, and to tailor our product to your needs.</p><p>One of our biggest challenges is to respond to card authorization requests in 15 milliseconds on average. Going this fast means faster answers when you pay with the Qonto card. It also implies a reduced infrastructure cost to scale the service. As a matter of comparison, the average response time from third-party products is between 150 and 300 milliseconds: we are 10 to 20 times faster than this standard.</p><p>This article discusses <strong>how we manage to get those response times while keeping a clean and maintainable code-base</strong>. We invite you on a journey through the design phase of the service, to actual implementation with two concrete examples.</p><h3>TL;DR</h3><p>Your Qonto card is crazy fast and rock solid.</p><h3>The recipe: from needs to plan</h3><h4>The ingredients</h4><p>Starting from a blank page. What do we need? What are the problems to solve? Where could our new Core Banking System (CBS) bring something better to the table?</p><p>You would think card payments are straightforward: insert your card into a payment terminal, enter your PIN code, and voilà! You would be astonished by the variety of sequences of events we can actually receive. We discovered that early on while mapping out the different use-cases.</p><p>At the same time, with the technical documentation of the card network, we draw a flowchart of what inputs and outputs we would have to receive and send. Let’s see it with some common activities.</p><ul><li>Imagine you entered the wrong PIN code at your favorite restaurant. The following sequence is generated:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tNmkup3ucmsfUJ8hkPvaew.png" /></figure><ul><li>Next, you visit a website to purchase a product. This is what happens:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R8QrPeWVAnR6mUKJ0J6DnQ.png" /></figure><ul><li>After buying a pair of airplane tickets, you decide to cancel one of them:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7p8fV66edmLB9jWHA5j1g.png" /></figure><p>At this point, we have a clear overview of what is expected from us — functionally speaking. We need to define clear goals before starting to dive into the technical details of implementation.</p><p>Dropping our computers, we met with everyone in the development team around a sheet of paper. We wrote a list of objectives:</p><ul><li><strong>Be fast</strong>: To provide the best possible user experience to our customers. It will lessen the cost of scaling. Not to mention, improve the system’s resilience.</li><li><strong>Be accurate</strong>: We must not allow any false positive nor false negative authorization. When you have money, you can use it. When you don’t, you can’t. This might seem obvious, but better said than not.</li><li><strong>Be maintainable from the start</strong>: What we build must last long and be easily re-usable.</li><li><strong>Ship on time</strong>. Keeping that in mind helps us simplify things.</li></ul><h4>The recipe</h4><p>We have those starting elements, the ingredients for our CBS authorization service. Let’s plan out the recipe to put everything together.</p><ol><li><strong>Separation of concerns</strong></li></ol><p>Have you heard of the <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a> principle? This design rule, allegedly coined by Dijkstra, explains that each <em>aspect</em> of a program should be tackled separately. This enhances the program’s readability and maintainability. So, which aspects is the authorization service cut into?</p><ul><li><strong>A protocol (</strong><a href="https://en.wikipedia.org/wiki/ISO_8583"><strong>ISO8583</strong></a><strong>) decoder and encoder</strong>: For communication with the card network.</li><li><strong>An authorization handler</strong>: We must be able to answer any authorization request.</li></ul><p>Both will be implemented as microservices and communicate through a REST API.</p><p><strong>2. Defining the inner flow of services</strong></p><p>Next, we begin to divide the work under each microservice: the code should be readable and re-usable. Each part should be oblivious to what the other parts are doing.</p><p>We divide the protocol handler into three parts:</p><ol><li>A <strong>TCP Client/Server</strong> to send and receive ISO8583 Frames from the card network,</li><li>An <strong>ISO8583 decoder and encoder,</strong></li><li>A <strong>client for the authorization service,</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5dnTYeVvgVtw1gV-MAP7Aw.png" /></figure><p>We divide the authorization service into three parts as well:</p><ol><li>An <strong>API server</strong> for communication with the protocol handler,</li><li>A <strong>database (DB) client</strong> that will contain and execute DB queries,</li><li>An <strong>error handler</strong> that would provide feedback on authorization choices.</li></ol><p>Concerning the error handler, we expect that errors (or refusals) could occur at multiple levels. As we want to be knowledgeable, we want to keep proceeding with the normal path in order to see all potential errors and refusal reasons. The error handler will have the task to centralize all errors and deliver the final answer cause.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nUFvo_4KzK34D7ZxEtv-CQ.png" /></figure><h3>Cooking the starter: parsing ISO8583</h3><h4>A complex format</h4><p>Let’s follow up on the credit example from earlier: a credit request arrives at Qonto. We receive an ISO8583 <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol">TCP</a> frame looking very much like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d8e05407ec1f93c6b0aa7ea6f5c05c7/href">https://medium.com/media/7d8e05407ec1f93c6b0aa7ea6f5c05c7/href</a></iframe><p>The first four octets are the length of the TCP packet. The two following ones (0400) indicate the kind of message. Afterwards, you get one or two bitmaps indicating which fields are present, then in order all of those fields.</p><p>Each field has a different encoding. Some are variable length, some contain optional sub-fields, some are decimal values encoded as hexadecimal. <em>(You read that right: see the </em><em>0978 string in the example? It really reads </em><em>978, even if the hex value is actually </em><em>2424 — and represents the</em><a href="https://www.iban.com/currency-codes"><em> ISO4217 code for the euro currency</em></a><em> –.)</em></p><p>Depending on the field type, data is grouped per octet (that is the case for text) or per <a href="https://en.wikipedia.org/wiki/Nibble">quartet</a> (for digits). Even in one field, that grouping might change. For instance, a variable length field has its length encoded as normal hexadecimal per octet, and its data can be numeric and grouped by quartets.</p><h4>Being fast</h4><p>Here is the actual challenge of the protocol handler: parse this messy format and transform it into intelligible <a href="https://fr.wikipedia.org/wiki/JavaScript_Object_Notation">JSON</a>. And if possible, read it only once: because we need all information from the frame, <a href="https://en.wikipedia.org/wiki/Computational_complexity">O(n)</a> is the optimal parsing complexity.</p><p>First things first: we create an array, describing all possible fields in order of appearance (some of them being potentially in two positions, we put them twice in the array). As we will advance in reading the frame, we will advance on this array.</p><p>Here is a sample part of it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a8a77df2e2d3e98db79630734ed4971/href">https://medium.com/media/4a8a77df2e2d3e98db79630734ed4971/href</a></iframe><p>We also need a reception struct with methods to input fields:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2337003c97c3ad87cb6184a75720845/href">https://medium.com/media/c2337003c97c3ad87cb6184a75720845/href</a></iframe><p>Then the parser is straightforward:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a5b7e66eac929746d67d08e2ab99b2d/href">https://medium.com/media/6a5b7e66eac929746d67d08e2ab99b2d/href</a></iframe><p>Of course, this pseudo-code dismisses any error checking or fail safety. It implies that a Quartet Buffer type has to be implemented, as well as a Quartet type because Go doesn’t support types whose length is less than one byte.</p><h4>Handling quartets</h4><p>Given that the output of the protocol handler is pure JSON (a string), we decide to drop the quartet to ints tuple conversion (we would have done it with a few <a href="https://en.wikipedia.org/wiki/Bitwise_operation">bitwise operations</a>). Instead, we create three functions:</p><ul><li>A converter from one byte of ISO8583 digits to two ascii digits;</li><li>One from a byte of text (in <a href="https://en.wikipedia.org/wiki/EBCDIC">EBCDIC</a>) to an <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> character;</li><li>A <em>nop</em> converter that only copies the byte as is.</li></ul><p>Given that the two first ones are just alphabet conversion, we just use a map:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30f1b6010fc6f0ada8b77885250a7cbf/href">https://medium.com/media/30f1b6010fc6f0ada8b77885250a7cbf/href</a></iframe><p>As such, we can use a simple <a href="https://golang.org/pkg/bytes/#Buffer">bytes buffer</a> to handle reading the frame.</p><p>After all those improvements, let’s benchmark the protocol handler. From end to end, it takes on average 150 µs to receive and parse, and 150 µs to write and send a full frame. The code is clean with little testable functions and great separation of concerns.</p><p>We could have improved more. For instance, we could have used arrays instead of maps for conversion. But that isn’t needed: we are fast enough, and that would make the code less readable. We can move on to the authorization service.</p><h3>The main dish: How we use Postgres to its fullest capabilities</h3><p>Treating your card payment does not end with parsing the card network request! Let’s dig into the authorization service: how we decide to accept your payments.</p><p>The authorization service can be quite complex. If we implement it strictly as the naive flow describes it, it will take a very long time to process a payload.</p><p>The question is: what is the most important latency cause in the process? Network! Where does this flow generate the most network calls: database calls. We need to simplify it. Ideally, an authorization should only require one call to the database.</p><h4>Designing the database</h4><p>Let’s write down all the information we need to store:</p><ul><li>Account information</li><li>Card information and options</li><li>Balance</li><li>Requested amount and balance delta (as partial authorizations might be allowed)</li><li>Insertion, validity and authorization date</li><li>Operation type</li></ul><p>That list is short enough to be put in only one table. And with a clever choice of indexes, we can make sure that any lookup in this table will be fast. We opt for a primary key of account id, card token, insertion date and id. This way, we ensure that Postgres stores data in that order. And the table is partition ready, be it per account or date.</p><p>The next question is: how to identify transactions in sequence (say a clearing after an authorization)? We add a field: transaction id, for this purpose.</p><p>Any row with a null authorization date is ignored in calculations. That way, we keep track of refused authorizations while maintaining data readability.</p><p>The final table is ready to be designed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01df365bab1749d5c8a60375a3507501/href">https://medium.com/media/01df365bab1749d5c8a60375a3507501/href</a></iframe><p>If we were to describe the flow, we could take the analogy of a cassette tape. For each query, we roll it until we get to the line matched by the key. However, with the choice of the primary key we made, we ensure that data of a card and account are grouped. Which means that we can make simple queries that roll up to one point and then move forward and backward the tape for only limited distances.</p><h4>Forging fast queries</h4><p>With this one table, the next focus for each payload received by the authorization service is to only trigger one query execution. This query has to:</p><ol><li>Get the <strong>account balance</strong> (an account can have multiple cards),</li><li>Get the <strong>account status</strong>,</li><li>Get the <strong>rolling limits</strong> of the card on the last month,</li><li>Get the<strong> last status</strong> of the card as well as its options,</li><li>Get the possible <strong>previous transaction</strong> (if multiple linked transactions such as in the first case at a fuel dispenser),</li><li>Compute<strong> authorizable amount </strong>(the balance delta),</li><li>Decide whether to<strong> authorize, partially authorize or refuse</strong> the transaction,</li><li><strong>Insert a new transaction</strong> or (in case of idempotency conflict) return the previous transaction.</li></ol><p>Eight queries to distill into only one! Thankfully, SQL provides many ways for us to achieve it. We use <a href="https://en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL#Common_table_expression">Common Table Expressions</a> (CTE) to fetch the necessary data:</p><ul><li>One for account balance and status,</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d12bb0ef859f2b58c66d8408ceb7f866/href">https://medium.com/media/d12bb0ef859f2b58c66d8408ceb7f866/href</a></iframe><p>One for the last month of operations of the card (including its last status),</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dc34aaa936f83d76b96da41426cd54b/href">https://medium.com/media/8dc34aaa936f83d76b96da41426cd54b/href</a></iframe><ul><li>Two for idempotency, searching into the last month CTE.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5b152a2759f89d00023d08164dd5a0a/href">https://medium.com/media/a5b152a2759f89d00023d08164dd5a0a/href</a></iframe><p>We then use a subquery in the from clause for authorizable amount computation. To be able to insert or return the previous transactions, we use an <a href="https://www.postgresql.org/docs/11/sql-insert.html">Upsert</a> construction.</p><p>Implementing the decision tree is then only a matter of carefully designed <a href="https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS">CASE</a> expressions.</p><p>After some fine tuning, here we are, answering in less than 8 milliseconds from end to end on the authorization service.</p><h3>The dessert: what we learned, how to improve</h3><h4>Be fast: Not everything has to be improved</h4><p>There are a few indicators to monitor in order to be fast and in time:</p><ul><li><strong>How fast</strong> is the code running?</li><li><strong>How much resource</strong> does it consume?</li><li><strong>How long</strong> does it take us to write it?</li><li><strong>At which cost</strong> will we maintain it in the future?</li></ul><h4>Software metrics</h4><p>The two first ones are easy: using the go tools, running a benchmark and profiling an application is straightforward.</p><p>Here is one example of how we use them: having almost finished the development of the protocol handler, we start to benchmark and profile it. It takes around 300 µs from input to output (so 600 µs total). That seems ok. But what a surprise, analyzing the profiling output, to see that most of the time is spent in <a href="https://golang.org/pkg/fmt/#Sprintf">Sprintf</a> calls and reflection!</p><p>It turns out that the logger library we chose makes heavy use of those, constantly slowing down the process. That decided us to go from it to <a href="https://github.com/uber-go/zap">Uber’s zap logger</a>. With its focus on performance, we managed to go down to the 150 µs time we presented you earlier.</p><p>We use the same metrics to analyze our implementation: to ship in time, we do not have time to optimize each and every part of the application. We rather work on some part, profile it, and focus on the critical path. Why lose 10 hours writing a blazing fast function, if it is only run 1% of the time? That time is better spent improving the one function that runs most of the time.</p><h4>Team metrics</h4><p>When you develop, you easily lose yourself in small details. To stay focused on our objectives, we use several good practices:</p><ul><li><strong>Split the work</strong> left to do into small units: that way, we always have a clear and attainable goal. Also, patches are small and easy to review.</li><li><strong>Use visual feedback</strong>: with <a href="https://en.wikipedia.org/wiki/Kanban"><em>Kanban</em> <em>(看板)</em></a>, we constantly keep track of what is done, on review, in progress, or in backlog. Discussing it on a daily basis helps us select the right priorities.</li><li><strong>Continuous peer review</strong>: having split the tasks into small units, we are able to ship many times a day. Each time a developer creates a merge request, they look at those still open, reviewing what the others have done.</li></ul><p>By maintaining those principles, we have been able to ship within the expected delay. We started conception in April 2018, and the authorization service passed all <a href="https://en.wikipedia.org/wiki/Quality_assurance">QA</a> by the end of December. It is a very fast implementation and is highly maintainable.</p><h3>Ready to serve? There is always room to improve</h3><p>Can we say “the end?” The system is already in use by beta testers. It handles all of our expected use-cases. It interfaces with the card network seamlessly. It is fast. It is accurate. It is resilient. It is a strong ground for all payment methods you throw at it.</p><p>We can still go further: we said earlier that the queries are very large. We can split them and — using stored procedures — get a nice speedup by foregoing the SQL compilation.</p><p>We took great care to give you the most robust payment experience. Go ahead and use your Qonto card everywhere!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1004afaf21ab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/qonto-way/how-we-ensure-fast-response-time-for-card-authorization-1004afaf21ab">How we ensure a fast response time for card authorization</a> was originally published in <a href="https://medium.com/qonto-way">The Qonto Way</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>