How I Built Klarna’s Experimentation System, and a LISP
Dotan Nahum · Reversim Summit 2018 transcripts · Markdown
Human subtitles from YouTube. Click a time to play the video from that point.
00:11 Hi everybody My name is Dotan CTO in HiredScore "jondot" in GitHub and one of the three colleagues in the "Bumpers" podcast Alon and Ran are with me here And I wanted to talk to you today about an experimentation system I build in Klarna and before that if we pause for a bit why do experiments or what is that to begin with? So in the more common name experiments are called A/B Testing everybody knows the example: a green button, a red button what performs better? what will "convert" better? we don't really know, so what do we do? we take the green button, put it in production for 6 months we take the red button, next 6 months after a year, we have results.
01:11 But what if during the last 6 months was Christmas? A red button is good! And what if during the first 6 months the company rebranded to a green color? Green does not really pop up after a green rebranding. So what we do is we take the green and red button, run them side by side at the same time, and check the data and find the winner. That, in essense, is A/B Testing. More reasons we want to experiment for: in big companies, the same red/green button dilemma exists in several instances in parallel It's very hard to follow, even if we want to, to track and find the winner, and decide when to stop the experiment, etc. So in every big company, I assume
02:13 exists such a thing. Another thing is that a small company when it grows, the intuition we had at the beginning, with the Product people, for example slowly departs from reality. The company grows, we have new users, new audiences, new markets we never knew and the same product manager that had good intuitions before, does not anymore. We see it in the company's KPIs and there is some gap. So an experimentation system is a system in which we want to build experiments model them and run them in production while it takes care of the data, of the parallelisation, with all its problems This kind of system supports a company that wants to be data-driven - a company that looks at numbers and not, and does not guess,
03:14 and subsequently create new features or drop them, etc. With time this system becomes a kind of place for the "ground truth" for the company for decision making. This means, that such a system must be reliable, flexible and it's a long-term investment. Meaning, the system that I am now building and integrating, and is used to make decisions... there is some trust that's growing in that system, and it takes time. So essentially, What I did in Klarna, while building it -- and I had a period of research in which I understood the is no reference on how to build such system. There is a lot of material dealing with statistics, when to create experiments, how to achieve significance
04:15 in an experiment. There are a lot of best-practices, a lot of it coming from Bing, by the way, who pioneered this. But there was nothing about the mechanics, how to actually build it. What to do so to make it maintainable, to make it good. This talk comes to fill this gap. Let's do a small exercise, and try to implement such a system. We have a central service, called Experiments. There is a User, called Yossi. He arrives to this system, says "I am Yossi". The system has a database, it takes Yossi, randomly draws a red or green button. It remembers, "Yossi got a red button", and this will stay forever. And Yossi receives a red button. It could be just a CSS rule, or a configuration value. From now on
05:15 that Yossi will "travel" throughout our company and from all the micro services he will have the same experience. Why? Because all the services are connected to that same system and every time Yossi arrives to a product of the company that product asks the system which button Yossi should get. And that's it. We are done, actually. No, not really. That is a naive system. It does not mean it is not successful, or good. But this system is a single point of failure, if it breaks, everything breaks. It's a super painful dependency because if there is one like it in the organization, all the micro services depend on it all the products depend on it. That's pretty bad. And if we recall that we stored all the data in a database
06:16 then we actually created something very bad - we took the user's world, the traffic coming into the system, and for every unit of that traffic, we created a row in the database. It's a kind of a multiplier that depends on the existing traffic. So if we have, say 100 requests per second, so now we have a system that needs to store 300 requests per second. It's bad. The more time that system lives, the more loaded it becomes. And that's how things become worse. And we haven't mentioned, very inherent features like audience segmentation. For example, I want to segment all the beta users of the organization. Or I want to exclude all the company employees. I don't want them to pollute the experiments. So the challenge is to build a system without a database. The only thing I can think of, that we can store
07:17 in this system is experiment configurations: I have an experiment with a red or green button. Maybe more experiments. Maybe the relations between them. It should be a system which is not a single point of failure. And also has no latency. A kind of hell. In Klarna, speci fically, there are something like 12 programming environments. From Erlang to Haskell, Scala, Java, JavaScript, Python. Challenging and interesting. This thing must support a standard, microservices across the organization. It must support pure frontend experiments. And mobile. This company is a payments company so the SLA is a payment-company SLA. Meaning - this system, when built must respond under 1ms. Plus all the trouble and fear of Black Friday and Cyber Monday are relevant also here. So I inherit all the
08:20 overhead of all the product teams. Moreso, audience, user-agent, geo IP - standard stuff. Market. Merchant. And beta users. All these things, I want to segment by them. I want an experiment which is relevant only for beta users. I want an experiment which is relevant only for Best Buy. Etc. So I think we can immediately rule out a central service. Yeah, it will work ok in 12 separate programming languages, because it's irrelevant. Backend, frontend, mobile - that's not new. It will work. It's maintainable because I can deploy versions freely, however I want. But I cannot guarantee latency of under 1 ms. And I cannot guarantee
09:23 not having a single point of failure without creating a little monster - a cluster. Which I don't want to do. So it turns out, the mirror image of building such a system is to build it as a client side library, which is pretty weird. If it was Python it was a Python module, if it was Java - a JAR. And so on. Facebook have a system called Planout, which works like this. OptimizelyX is a new Optimizely product that also works like that. Google from what I understood - it's not "out there" - also work like this. Meaning, they built an experimentation system and integrated it into each of the services and products in the company. And that's how it works. So we don't have latency - it's just a library call. And we don't have a single point of failure because this is integrated in each of our services.
10:25 Backend, frontend - does not matter, again, since it's integrated. But specifically, I had a problem with that because I had 12 programming languages to support. I'd have to create a Ruby GEM, a Python module, JAR for Java. And port an reimplement. Massive headache. Massive matrix to maintain. Facebook did that with Planout. You can check their GitHub repo and see it there. It has a lot of open issues. Glassdor ported it to Java. And their port already have forked from how Planout works. Not something I wanted to maintain. Google did it in C++, which is also something I did not want to maintain. I did it in JavaScript. I will give you a second to laugh. But turns out it fulfills all the requirements. To take and put it
11:25 in some language, say Python or Erlang, I take what I built, embed it in V8 - a JavaScript engine. Binding of V8 in every language is amazing. V8 also powers Chrome. But actually I did something better than that. Instead all this wrapping, I took this engine and wrapped it in a NodeJS service and I deployed it to all the nodes in the organization - more than 100 nodes. Each such node has a "magic" service on a "magic" port, that does experimentation. So we have a system which is kind of feels like a central service but in reality is deployed across the organization. No single point of failure. Local network, local host. Everything worked amazingly. Mobile - so React Native, JavaScript library - it works. Central service - wrap it in a Lambda. Also works. If you chicken out
12:26 and fear going all the way. This option still exists. Usually it's Marketing - PHP services and such. OK. So I fooled you and made all this into a library, but what happened to the database? There are a couple of things to "tick off" for all of this to work. One is Persistent Hashing. That's what allows us to avoid user storage. Audience Filtering is the mechanism to filter audience. Experiment modelling will give us a small system and can model a whole world of experiments. The statistics and data pipeline is out of scope for this talk. Due to lack of time. So let's begin. What is Persistent Hashing. So I contemplated how to explain this point. I decided to tell a short story. When I joined the army, they gathered us
13:29 and asked who has a driving license. I didn't. They said whoever has a license - goes to some unit. I really wanted that unit. And all the rest - go somewhere else. I went to the other place. I don't regret it. What I am trying to say it that even before they asked, my fate was already decided. And how? By me having something, a property - the fact I did not have a license. Let's switch back. All of our users in the product - they all have something that identifies them - a user ID. So if we want to create this user's "future" in our system we can take that ID "4F..." whatever and hash it
14:30 and bucket it. What is bucketing? We have 100 buckets. Let's say the first two letters, "4A" map to the number 18. If we look at the diagram then 18 is actually "B". And that's how we map from a user ID to an experiment, say "B". So this is Persistent Hashing. I am lying a bit - it's a bit more complicated. There is a link at the end. Another challenge - that I had, specifically in Klarna, a big company. Audience segmentation is... if you look at Optimizely, they have pre-defined audiences. For example "Beta users" - you cannot customize it. And Klarna was just not ready yet to pre-define audiences.
15:30 So I said, OK, it's JavaScript. Take a function, do whatever you like. Amazing. I can even inject code. Very quickly I understood that it's not possible. Because I don't know what I'm going to get as input, whether they are going to cause an infinite loop. Pretty terrible. So I turned to sandboxing, and I tried to put a JS VM inside a JS VM. Didn't work. Quickly I understood I need to create a restricted language that contains whatever is needed for matching and filtering but does not contain things that long to run. So this thing is called HyperMatch - it's an open source library, you can use it. It looks very much like LISP. When I finished it - I loved it. Because it looks like LISP. Another thing we meet,
16:31 and it's a recurring theme. What happens when there is little traffic and what happens when there is lots of traffic. In an ad-tech company, there is a lot of traffic, we don't need all this complication. But in a fin-tech company there is little traffic and the experiments don't converge. The more traffic you have the faster the experiment converges. What can we do? We can reuse. We take a user and expose him to several experiments in parallel. But this is dangerous. There might be one experiment that shows a white text, and another one that toggles between black or white background. And theoretically we can have white text with white background. Which is bad. So we must define some constraint. We define an abstraction, called "layer". The middle layer in the slide. And inside a layer experiments are mutually exclusive. So e.g. a this user
17:33 visits "experiment 1", receives "B", then visits "layer 1", receives only "4", not the rest - he must not. Then visits "experiment 5", receives "B". So we can allow a user to visit several experiment in parallel. Now, if you notice, there is some nice thing here. An experiment has "A" or "B" - two "boxes" which are mutually exclusive. A layer also has two "boxes" (experiments) which are also mutually exclusive. Layers, in general sit on top of each other, like a pancake. And layers are not mutually exclusive - a user can visit several layers in parallel. It's a recursive structure unfolding in front of our eyes. If we see it, and define it, we can gain from it. So we have an abstraction called "group" which contains "layers"
18:34 that contain "experiments" that contain "variates". And it's a hierarchical structure. And every building block in that tree has its relationship to others. For example, "variates" are exclusive to one another. And also "experiments" in the same "layer". After I have identified it, I could build a kind of a "language" on top of it. I recognized it as a language. Modeled it as such. The system is a compiler that takes all these concepts - groups, layers experiments, and their relations and constraints, compiles it to an AST - a data-model very specific for experiments, and when I get a request, I have an interpreter that
19:35 runs this request against the AST. It allows me to build diverse experimentation systems using the same code base. For example I can create a system for an ad-tech company where all the experiments are exclusive, they are not shared. And it's possible to create a system with even "CEO" experiments, where I divert traffic which is dedicated so these experiments converge really fast. That's the side-effect when you recognize a data model make it sacred flow with it, make abstractions, language, compiler, etc. The side effect is that the same agent, on all the machines received many experiments - a lot of JSON text compiled all of this into an efficient and compact model
20:35 in memory. And it helped me in performance, where I needed it to run under 1 ms. So takeaways - what I have learned, and saw in other companies: there is no MVP in experimentation. If you build such a system you have to break the rule, and optimize prematurely. "With the knife between your teeth". It must be resilient from day one. It must have data integrity from day one. It must be performant from day one. Backed by tests. Because what we don't want to do is have some MVP, beta, and after 6 months and many decisions you find out the data is off by 50% and every decision until now was a mistake. And this thing happened. In companies we all know.
21:39 That's why there is a "rewrite symptom" with these systems. In almost every company that did this you meet someone busy building the second version of it. This is a takeaway I knew of from the beginning. Another takeaway is that compilation theory is not just for university. That surprised me. As I built it, abstractions were formed and opportunistically I detected it, said "let's stop with the if's and switch's". Hop. Building a language. Another take away - when you have the correct data model the number of lines in the code becomes a joke. This system, if I recall correctly, is maybe 400 LOC. Which is funny. But it's power is in it's data model. The interpreter is compact and the data model is strong. That's the sign you're on the right way.
22:42 That's it. Thank you all. Questions? How many experiments are running in Klarna - when I left it was about 10-15 experiments in parallel. And going up. How much time lasts each experiment? The answer is it depends on the amount of traffic you have. When you segment audiences, and have, say, 5 users per day, it will take time to reach a big enough audience to arrive to a conclusion. Great question: if the system is distributed and the data model is centralized, how does it work? So every machine runs an agent hundreds of agents and contacts some S3 bucket, pulls all the experiments from there.
23:43 We decoupled the administration system - which manages and deploys experiments - from what runs them. Every interval, all the agents reach out to S3, download all the experiments compile them, and we have a new version of the "world". How do results arrive? With logs. Every log tags the "world" version it used. Thank you.