# Googlies! Grubbers! Lollies! How cricket helped us improve our throughput by 200%

- Speaker: Ethan Pransky
- Event: Reversim Summit 2019
- YouTube: https://www.youtube.com/watch?v=aejjRzjGYOE
- Model: gemini-3.5-flash-lite

> Machine transcript, may contain errors.

---

[00:10] Okay. Googlies, grubbers, lollies, how cricket helped us improve our throughput by 200%. Um, okay, we're talking about cricket, the sport, not the animal. Googlies, grubbers, and lollies are three slang words from cricket, they have nothing to do with the stock, they're just funny sounding cricket words. If you want to know what they mean, you can look them up online. But, cricket is very relevant to the stock, and soon I'll tell you why. Um, my name is Ethan Pransky, and I'm a software engineer at Appslire. Been at Appslire for about 2 years, and [00:40] Thank you. And I'm in a team called Partner Development Solutions, and our responsibility in partner development solutions is for all the outbound communication that uh Appslire handles. And what is Appslire? Appslire is a mobile attribution and analytics platform that helps our clients, app marketeers, optimize their user acquisition funnel. [01:03] And the way that this works is that we have an SDK which our clients integrate in their applications, and our application can be found in about 99% of devices worldwide. [01:32] Um, and basically the way that our solution works is that that SDK fires events to our web servers whenever whenever an event happens inside one of our clients' applications. And that sums up to about 60 billion incoming uh events every day that we receive in our web servers. [02:04] And in PDS we handle we end up sending about the same amount in outbound communication. Outbound communication, I mean we send HTTP requests which we call postbacks. So we also send about 60 billion postbacks every day and peaks of 1 million postbacks per second. [02:37] Okay, so, why are we here? [02:51] So, over the past few years, we've grown a lot. Okay? And a little over a year ago, we were handling just a third of the traffic that we're handling today. So about 20 billion events. And we and so that means that over this time we've had to learn how to scale both quickly and efficiently. [03:22] And as developers, we don't want to spend our time doing performance optimization. A lot of people say don't do performance optimization at all. Only do it when it's absolutely necessary. As developers, we're paid to build features and fix bugs, right? [03:55] So, I want to tell you about a story that happened to us about a year ago when we were forced to handle a massive increase in incoming events and how we scaled up to to handle those incoming events. [04:26] And from this experience, how we generated it a simple um an easy framework or methodology for for scale adaptation and performance optimization, which doesn't break your development cycle, and is based on first-order principles. [04:59] And I think this methodology is applicable to any sort of application and any sort of organization. [05:27] Okay, so I told you I would say I would tell you why cricket is relevant to this talk. And cricket is relevant to this talk because at AppsFlyer, um over the past year we've become huge cricket fans, okay? And this guy's name is Rohit Sharma and he's the captain of the Mumbai Indians Indian Premier League cricket team. And he's huge in India, okay? [06:01] And you might be wondering, why is an Israeli R&D organization so obsessed with cricket? Well, a year ago, this happened. [06:33] The first game of the 2018 Indian Premier League cricket season, and it featured the Mumbai Indians, our favorite team, facing off against their rival, the Chennai Super Kings. The Mumbai Indians were the reigning champions, and the Chennai Super Kings were our fierce competitor. And everybody in India was watching this game, including tens of millions of people on their mo- on their mobile devices. [07:07] And it just so happened that one of our clients, Hotstar, had just secured the exclusive rights to stream all Indian Premier League games for the 2018 and beyond. And as you can imagine, those 10 million users, those 10 million Indian fans, started bombarding our servers with events. [07:38] And overnight, we started experiencing, um, an over 50% increase in incoming events. So instead of handling 20 billion events per day, we were handling 40 billion events per day and and more. [08:08] And this continued day in and day out as the season continued, and we were struggling to manage, especially in partner development solutions we were struggling to manage. [08:34] And I'll tell you a little bit about, uh, uh, what we do in partner development solutions to give you a better idea. [09:02]

[09:02] And it takes very little to conceptualize this law. It's really simple. Um, and it helps us, and the power of it is is that it helps us break our large scale systems into little problems that are easy to diagnose and and solve. This is John Little, by the way. He didn't he proved this law. He's still alive. Um, and he's a professor at MIT. Anyway, so I'll define these variables, I rewrote it so it's a little bit more applicable for for software development, for our realm. And the way I like to define it is this, that C, our capacity, so the amount of events in our applications at a given time, is equal to our throughput, which is the exit rate of events from our applications, times the latency of events in our applications. So latency is the amount of time that an event spends in our application. And I'll define one more variable, which is arrival rate, which if throughput is the the rate at which events exit our system, arrival rate is is the rate at which events enter our system.

[10:02] And so how can we start applying this? Well, at a high level, we said that the our problematic system, our applications, the tentacle, right? And the tentacle was consuming from from Kafka, okay? So, how can we model uh this relationship using using Little's law? Let's take an example to to understand this relationship a little bit better. So this is Wan Kidi Stadium. This is where the Mumbai Indians play their home games. And the scenario is that we have a bunch of a bunch of excited Indian uh Mumbai Indian fans arriving to Wan Kidi Stadium, and they're arriving at about 200 fans per minute. And there are ticket stands that are spread outside the stadium, and it takes each fan about a minute to pick up uh his or her ticket. And they said there's 100 stands around the stadium. Okay, so let's play the scenario out for a second. What happens in the first minute? 200 fans arrive, 100 fans go into the ticket booth, 100 fans go into the queue. After the second minute, the 100 fans from from in the ticket booth leave and 100 fans from the queue go into the ticket booth, and we have 200 new fans in the queue.

[11:09] So, now 200 fans in the queue. After the third minute, 100 fans, again, leave the ticket booths, 100 fans from the queue, another 200 into the queue. Now, we have 300 fans in the queue. After 400 after 4 minutes, we have 400 fans in the queue. And what we can see is that Little's law holds up, right? So, we have uh 100 fans as our capacity, our latency is 1 minute, and we can see that always uh every minute we have 100 fans leaving. So, our throughput is actually 100 fans per minute. This is uh and for if it if it wasn't clear, the the ticket booths here represent the tentacle and and the queue here represents Kafka, right? And what's cool about this is that not that we can just think about this system as a whole in terms of little, but we can actually think of the ticket booth area and the queuing area each as their own subsystems and apply little slot to each one of them. So, we saw what happened in the ticket booth. We said that it has a throughput of 100 fans per minute. But, what's happening in the queue? Well, the queue, if you think about it, it has a throughput also of 100 fans per minute.

[12:10] It doesn't have any logic of its own. It inherits the throughput of the ticket booths, right? And what's the capacity of of of the queuing area? Well, it's just the size of the queue, right? So, if we rewrite little law, we can understand that um we can figure out what the latency will be at any given time for the queue, okay? And just as an aside, a lot of people think that when they're using Q in their in their systems that they can make some voodoo uh increasing or decreasing the size of their queue and that'll help their throughput, but this proves that it doesn't, right? Because if we if we just limit the size that a queue can be, it also limit the latency of of that queue, right? And if we don't limit the size of of that queue, we can see that it'll just grow endlessly and endlessly, right? So the capacity will keep growing and growing and growing and it'll approach infinity and since the capacity will approach infinity, also the latency of of the queue will also approach infinity. And so that shows us that from a fan's perspective, even though the system as a whole always has a throughput of 100 fans per minute, from the experience of of the fan, it'll be the sum of the latency of the queue and of the latency of the ticketing error, which is also infinity, okay?

[13:22] And this tells us something profound that queuing theory tells us, which is that whenever we have an arrival rate which is greater than the throughput of our system, the latency of that system will also will always strive to approach infinity. Okay? And so that means that our goal is always to increase our throughput to at least the level of our arrival rate in order to reach a stable system. And so how do we do this? Well, if we rewrite little's law again, we can see that we only have two levers for for affecting throughput, right? We can either increase capacity or decrease latency. That's it. Okay? And if we take this into the the realm of of software development again, if we think about latency, well, what's latency in terms of software development? It's it's doing the same task but in less time, okay? And that means better algorithms or otherwise touching your code, which in general is a is something we want to try to avoid, right? Because we don't want to break our development cycle. We don't want to spend time doing performance optimization.

[14:24] So, so when we're thinking about doing scale optimization, we want to we want to lift towards capacity increase because capacity increase, well, it might not be free, it's it's easier, right? You're usually touching configurations. In fact, in the example with the Kafka and the tentacle, in order to we just had to increase the instances. We didn't actually have to do we have to make any merge requests, we didn't have to commit code or otherwise. All we had to do is increase the amount of instances. So it was so it was relatively easy, right? So the rule of thumb is let's look towards capacity increase before we look at latency decrease. So, we can actually break down this relationship between Kafka and the tentacle a little bit more because what's happening? What's actually a Kafka topic? It's a set of partitions and each partition is its own queue. And we can look at the relationship between a Kafka partition and each of the instances and break down the instances themselves into into their own subsystems, right? And if we do that, we can see in in the tentacle we basically had three subsystems if we break it down.

[15:25] We have a consumer subsystem, which its queue is Kafka and it's a thread that all it does is take from Kafka and it puts into another queue, which is our sender subsystem, which that does the actual io, receives responses, and then puts in the producer queue. And the producer subsystem just produces to Kafka. That's all the service does, okay? So this is a little bit different from the from the example before because now we have multiple subsystems. We don't just have a single subsystem, a single system. So we need to understand how multiple subsystems work and get an intuition for that. So we can expand our example from before. And this time we see that we we always had a throughput of 100 fans per minute leaving our our ticket booths. But now this is a subsystem of a system as a whole. And that 100 fans per minute throughput now determines the arrival rate of our next station, which is a security check. And in the security check, we have 250 stations and it also takes one minute to process them. Okay? And so the potential throughput of the security system is 250 fans per minute.

[16:28] But in actuality, it'll only have a throughput of 100 fans per minute because that's that's the arrival rate, right? So this tells us something profound that little that queuing theory tells us about about when we have multiple subsystems, which is that when we have multiple subsystems, the throughput of our system as a whole will only as be as good as the lowest throughput of our of our subsystems. And it also shows that if we misallocate our resources that we can cause for ourselves sub-optimal throughput, right? Because if you see here, if we could just move 100 of those security checkers over to the tickets, then we could gain the throughput that we need. We'd have a efficiently running system. So now with all this in mind, we can think about this like we all think about this. We can think about this like like bottlenecks, right? We're all familiar with bottlenecks. But now we can define what a bottleneck is. A bottleneck is whenever we have is is the is a subsystem within a system which has the lowest throughput, okay? And and we have the tools to fix that that throughput, we either increase capacity or decrease latency.

[17:32] And we can keep doing this, we can keep increasing the the the throughput of our subsystems until we reach the maximum capacity of our system as a whole, which when we're talking about software, that's defined by our machine, right? So our CPU, our memory, and our disk. That's it. So we can go through this exercise until we reach the maximum of those resources. And so when we looked at our tentacle instances, this ended up giving us a hint at what was going wrong because when we looked at our tentacle instances, we saw that the machines after we upgraded them were only using 30 to 50% of the CPU that each machine had allocated to it. So we had some work we could do. So how do we go about debugging it? Well, this is what was happening in the tentacle. When when we were reaching peak times, our our two internal queues were all filling up. We knew what was happening in Kafka, a lag was building up, okay? So we had some bottlenecks we need to work out. So, what do we do? We want to add capacity. In software development, we add capacity by adding concurrency. Okay? So, in our producer subsystem, we could add another producer thread. There was no reason why we couldn't run more than one, taking from that queue.

[19:10] And when we did that that worked, Littles law held up, and we got the throughput that we needed to clear up that queue. So then we thought, let's do the same thing for the sender subsystem. This uh we still had a bottleneck. And actually when we did that, it didn't work. Okay? So this is kind of confusing because adding capacity should work, right? Well, we were we were trying to figure out what was happening here, and then we started to get a hint once we once we generated for ourselves flame graphs.

[20:10] And so we generated flame graphs, for those of you who don't know, they're a profiling tool, uh made by Brendan Gregg at Netflix. And they helped tell us which functions um in our applications are using the most CPU, okay? And a flame graph looks like this. And on the X-axis of a flame graph, it shows you the time on CPU per function, and the Y-axis shows us the cost stack of those functions. [21:10] And so what immediately jumped out to us here was was this stack here, right? That you you probably can't see it, but what it's showing is that what was using a lot of CPU here was a third-party library called HTTP Kit. We write all our services in Clojure, by the way. Um and this is a a closure library that that we were using. And this is the library that we were using to actually send the HTTP requests. [22:10] Okay? And and why was this weird to us? Well, we were thinking, well, this library only does IO, and IO shouldn't really be using a lot of CPU, especially it's using non-blocking IO, and non-blocking IO uses can use a single thread to manage multiple connections, so it shouldn't be using a lot of CPU, right? When we zoomed in, we can see, yeah, the IO wasn't using a lot of CPU, [23:10] but what was using a lot of CPU was SSL. Okay? So encrypting and decrypting our messages, most of our clients and our partner networks, they they they want to encrypt their messages. So this is what was actually, uh, so this is this was causing a lot of CPU. And why did this? And so this started to give us a hint at what we were missing here, because something was happening in HTTP kit that we weren't taking into consideration. [24:10] So we started digging into the the source code of HTTP kit, and this is what we found, [25:10] is that inside HTTP kit, there's this internal uh class called HTTP client. And when we call the request function of HTTP kit, what does it what does it do? It's it creates a request object and it submits it to this internal queue of HTTP client. And then there's another internal thread within HTTP client that takes those requests and it manages the state of each request. So connect, write, read, and this is where the SSL is happening, it turns out. [26:10] And then when it's done, it submits the response to a thread pool executor, which is just another queue and another set of threads, and that's what executes our our asynchronous callback of our HTTP request. [27:10]
