Case study · Stack

How Nubank built a bank on Clojure and Datomic

Nubank is the digital bank that started with a credit card in Brazil and now serves millions of customers across Latin America. From day one it bet on Clojure, the Datomic database and Kafka on AWS, later moved everything to Kubernetes and went on to buy the company behind Clojure.
Copy this stacknubank.com.br Digital bank · Brazil · founded 2013

Language

  • Clojureinfoq.com

    Main language for the backend microservices.

    Why: It pushes code toward simplicity from day one, and financial logic maps well to functional code.

Hosting

  • AWSinfoq.com

    Cloud where the bank runs, with continuous deployment.

    Why: The team did not want to manage physical machines while growing.

  • Kubernetesinfoq.com

    Runs the services, now on Amazon EKS, after replacing CloudFormation.

    Why: CloudFormation scaled poorly with so many services, and Kubernetes made infrastructure changes easier.

Database

  • Datomicinfoq.com

    Main database, an immutable ledger that keeps every change.

    Why: Keeping history on every update gives them auditing for free and point-in-time queries for debugging.

  • Amazon DynamoDBinfoq.com

    Storage backend under Datomic.

    Why: It was easy to operate for a small team.

Mobile framework

  • Flutterbuilding.nu.com

    Default framework for new features in the mobile app.

    Why: It beat Kotlin Native and React Native on testing, hot reload and API stability.

Data fetching

  • Kafkainfoq.com

    High-throughput jobs and communication between services.

    Why: Messages are kept on disk, so they can reset offsets and reprocess old ones.

  • Apache Sparkbuilding.nu.com

    Data transformations, written in Scala.

  • Databricksbuilding.nu.com

    Notebooks where people write and test data queries.

  • BigQuerybuilding.nu.com

    One of the ways people query the processed data on Google Cloud.

Backend framework

  • Finaglebuilding.nu.com

    Foundation for the Clojure microservices, in a hexagonal architecture.

Error monitoring

  • Prometheusinfoq.com

    Real-time metrics that also drive alerts and canary deploys.

  • Grafanainfoq.com

    Dashboards on top of the Prometheus metrics.

How they built it

The 2013 bets

Lucas Cavalcanti, one of the first engineers, explained the early choices at QCon. Datomic is an immutable database that keeps the history of every change, which gives a bank auditing for free. Clojure runs on the JVM and pushes the code toward simplicity, and financial logic fits functional code well.

Kafka was picked because messages stay stored and you can reset offsets to reprocess old ones, which saved them a few times. Everything ran on AWS so a small team would not have to manage machines.

Sources:infoq.com

Scaling by copying the whole bank

Between 2015 and 2016, instead of sharding only the database, Nubank started running full copies of the whole system, each serving a slice of customers. It put off complex code optimizations, at the cost of one shard growing much bigger than the rest.

In 2017 and 2018 they replaced CloudFormation with Kubernetes, migrating shard by shard with the bank still live, and later moved to Amazon EKS.

Sources:infoq.com

Buying the company behind Clojure

By 2019 Nubank described its backend as horizontally scalable microservices written mostly in Clojure with Finagle, using hexagonal architecture, with Kafka between services and Prometheus for monitoring.

In July 2020 Nubank acquired Cognitect, the company behind Clojure and Datomic, which had worked with it since 2014. Rich Hickey kept leading Clojure as an independent open source language.

Sources:building.nu.com building.nu.com

Flutter for mobile

Native mobile specialists were hard to hire, so in 2019 a task force of senior mobile engineers compared Kotlin Native, React Native and Flutter. Kotlin Native had no UI layer yet, and React Native leaned on too many third-party dependencies.

Flutter won on testing and developer experience, and became the default for new features in the app. Native Kotlin and Swift developers still handle platform-specific work.

Sources:building.nu.com

The data platform

Data transformations are written in Scala on Spark, and analysts code in Databricks notebooks. Processed data lands in S3 and Google Cloud, and people query it through Databricks, BigQuery and similar tools.

Sources:building.nu.com

Also detected on their website

Found on the homepage with the open Wappalyzer rules (HTML, scripts and headers). It shows what runs in the browser, not the whole backend.

  • Amazon CloudFront
  • Amazon Web Services
  • Envoy
  • HSTS
  • Next.js
  • React
  • Webpack