Hacker's Handbook

Taming the Spaghetti Monster: The Birth of Kappa

Solving Codebase Complexity at Klarna with a Business Layers Strategy

Posted: 2023-05-26
A plate of spaghetti and layers of Burrata and bacon

In the early days at Klarna, as our codebase grew exponentially, a peculiar problem began to manifest. Our expansive Erlang code base, like an unruly hydra, developed a tendency to morph into a maze of spaghetti code. The issue? Erlang functions, being either local to a module or exported universally, had a propensity for bypassing the carefully laid out APIs of our subsystems. As a result, library functions across subsystems could directly communicate, ignoring the module divisions that were supposed to enforce a clean code structure. It was a classic case of an organizational nightmare – the spaghetti monster was upon us.

Faced with this challenge, my colleagues, Richard and Tobias, and I, knew we had to act swiftly. We devised a plan that involved introducing business layers, coupled with enforcing rules through the build system to thwart any such infractions. This strategy was designed to control how different parts of the codebase could interact with each other. The result was the creation of an internal system that we affectionately named "Kappa".

Kappa was designed to answer the call for efficient means to manage the increasing complexity in our codebase. With Kappa, we were able to trace the ownership of specific applications, modules, or even database tables. It gave us an eagle-eye view of our codebase, pointing out which team was responsible for what part of the code and how to contact them. This made our development process significantly more organized and facilitated a higher level of accountability.

Further, Kappa allowed us to establish 'Application API modules' to curb inter-application calls to a select API set, preventing calls to private modules of other apps. The 'Restricted record fields usage' made alterations to record structures easier by only allowing specified modules to access specific record fields directly. We also introduced 'Code Layers', which essentially divided our apps into multiple layers, each with specific roles and restrictions concerning inter-layer communication.

The effects of implementing Kappa were almost immediate – we saw a significant improvement in code modularity, traceability, and the overall quality of our codebase. The spaghetti monster was finally tamed!

What started as a small internal project to tackle a growing concern transformed into a powerful tool that enabled us to navigate a complex codebase and bring it under control. Today, part of Kappa's code has been open-sourced, offering a glimpse into the structures we used to control our code at Klarna.

The creation and implementation of Kappa serve as a testament to HappiHacking's dedication to finding effective solutions in the realm of software development. Our battle with the spaghetti monster has given us a deeper understanding of the nuances involved in managing a large codebase. Armed with this knowledge and experience, we at HappiHacking are committed to helping you navigate the complexities of your codebase and deliver value as efficiently as possible.

You can find Kappa on Github

- Happi

Happi Hacking AB
Munkbrogatan 2, 5 tr
111 27 Stockholm