In the presentation How we write Haskell for production at Klarna, Felix Moulder discusses Klarna’s adoption of Haskell for microservices development, debunking common misconceptions about the language’s complexity and slow performance. He highlights the importance of user-friendly tools and the Haskell community’s focus on creating them. Moulder demonstrates writing Haskell code for production, using type declarations, JSON handling, Swagger documentation, and libraries. He also discusses the benefits of using Haskell generators for server definition and property-based testing. Despite challenges like learning the language’s operators and working in high-security environments, Moulder emphasizes the versatility and capabilities of Haskell for building production applications.
How we write Haskell for production at Klarna: A comprehensive overview
The shift to Functional Programming
Klarna’s journey to Haskell began when the team recognized the potential benefits of functional programming in handling complex event-driven systems. Starting with the Pearson functional programming library, the team gradually realized that Haskell, with its immutability, type safety, and functional purity, could offer more significant benefits. While the transition wasn’t without its challenges — particularly with team members coming from object-oriented backgrounds like Java and Scala — the team eventually grew more comfortable with Haskell’s paradigm.
One of the key takeaways from this transition was the power of abstraction and how Haskell allowed Klarna’s developers to “wrap things” in ways that made their code more maintainable and scalable.
Misconceptions about Haskell
Felix addressed several common misconceptions surrounding Haskell. One major myth is that Haskell is slow, which Felix found interesting, especially compared to other mainstream languages. He also dispelled the notion that Haskell is only for “senior engineers,” emphasizing that while Haskell does have a steep learning curve, it can be accessible with time and the right tools.
He also highlighted the Language Server Protocol (LSP), which has dramatically improved the developer experience for those working with Haskell, ensuring that even beginners can navigate the complexities of the language more smoothly.
Haskell in production at Klarna
So, why does Klarna favor Haskell in production?
Felix pointed out Haskell’s advantages, especially in terms of type safety, which helps reduce errors before code reaches production. He also mentioned that Haskell’s type system allowed the team to encode essential business logic directly into the type declarations, making code more predictable and maintainable.
The team’s use of packages such as “Service” and “Hedgehog” for property-based testing ensures that their services are well-tested and reliable. Felix demonstrated a simple example of a GET request using Haskell to retrieve data for a pet ID, showcasing how succinct and expressive Haskell code can be while still being powerful.
Type declarations and JSON handling
Another fascinating aspect of Haskell’s production use at Klarna is how the team handles type declarations. Felix explained the colon slash slash (`://`) syntax that separates the type signature from the implementation, adding a layer of clarity to the code.
When handling JSON requests, Haskell offers robust capabilities through its libraries. Felix highlighted how the team uses Swagger for documentation, and how they encode types directly into Swagger to ensure the documentation is accurate and aligns with the actual implementation.
Simplifying Server Definition with Generators
One of the standout points in Felix’s talk was how Klarna defines servers in Haskell using generators. By providing examples of input and output, they can easily generate server definitions, which simplifies the process and reduces manual coding. For instance, defining a server to return pet information could be done with a few lines of code, thanks to Haskell’s functional abstractions.
Additionally, property-based testing plays a key role in ensuring that their servers behave as expected. By feeding various test cases into the system, Klarna can verify that the server’s output matches the expected results, ensuring stability in production.
Challenges in High-Security Environments
Haskell isn’t without its challenges, especially in a high-security environment like Klarna. Felix discussed the hurdles they face when dealing with outdated clients and the breakages that can occur between APIs when updates are delayed. In these cases, changes need to be carefully coordinated with other teams to avoid introducing bugs or security vulnerabilities.
Felix also touched on the learning curve associated with some of Haskell’s operators, noting that even experienced engineers occasionally need reminders. He humorously suggested using stickers to help new developers familiarize themselves with Haskell’s symbols and operators.
The Role of the Haskell Community
Felix emphasized the importance of community in Haskell development. The Haskell community has been instrumental in building tools and libraries that make the language more approachable for developers. The Haskell in Production manifesto, which Felix mentioned, is an initiative aimed at improving Haskell’s tooling and libraries to make it easier to use in production environments.
One of the examples Felix shared was the Haskell HTTP client, which can be used as an alternative to popular clients in Java or C#. With the documentation and strong support from the Haskell ecosystem, Klarna’s engineers are equipped with all the resources they need to build and maintain production-ready applications.
Conclusion: Why Haskell works for Klarna
Despite the challenges, Haskell’s functional programming paradigm provides Klarna with the right balance of reliability, type safety, and expressiveness. Its extensive library ecosystem, strong type system, and the backing of a committed community make it a versatile and powerful tool in production environments. While there is a learning curve, the benefits far outweigh the initial challenges, especially when it comes to building scalable and maintainable systems.
Felix’s talk serves as a great introduction to how Haskell can thrive in a production environment, and how Klarna’s commitment to functional programming has helped them stay ahead in a competitive industry. Whether you’re already working with Haskell or are just curious about its potential, Klarna’s journey offers valuable insights into why Haskell could be a game-changer for your development practices.