Conjure: simple, polyglot RPC

Robert Fink · Reversim Summit 2019 transcripts · Markdown

Machine transcript. It can contain errors. Click a time to play the video from that point.

00:09 Okay. I work for a company called Palantir. Can I see some hands, who knows Palantir by name? Oh, almost everyone. Good. Perfect. I'm going to show you the title, you know, maybe a little misleading, Conjure, simple, polyglot RPC. I'm going to explain to you the following, why on earth did we build our own RPC system? So if you're a software developer, you know, never build your own operating system, obviously, your own database system, probably not a good idea. RPC, probably also not a good idea. There are many good options, why would you build one? Uh I'm going to show you that it's actually not so crazy that you could end up with your own RPC system, and we're actually pretty happy where we are and we see how we got there. So, um, I'll show you sort of the history, so going back 2013 uh to today, why we ended up here, then how and why we started building Conjure, so our RPC system. I'll show you how it's architected, uh how you would use it, and then some fun ideas that you can do with an RPC system when system when you have your own. Uh for those who don't know Palantir, uh very briefly, so we're a software company, um we're about 2,500 people, and our mission is to build software that um, you know, helps our customers solve their data-driven problems.

02:07 So, here are some examples. Uh we work with many of the big pharmaceutical, medical companies in the world, medical researchers, doctors, and so on, to work on cancer research. Uh we work, our software is used by a company called C4ADS, up there in the top corner. Uh if you don't know these people, Google them. They're a really cool company. They use open source intelligence to fight some like really complicated problems. foundry one of our products is a 200 something micro services, probably now 300, I'm not sure. And this is the entry point to the RPC bit of this story. So, uh going back to when we started building Foundry, uh this is about 2013. Um and then, you know, you you start with service number one, and then you have service number two, and then your question is, well, how do they talk to each other?

04:02 And there are a couple of canonical tech choices. So, most people would use things like Dropwizard, uh for servers, and they would use libraries like maybe Thain or retrofit, something in that space for clients.

05:03 Well, you have a couple of choices. Um, so option one, you could return this name as a plain string. So my name is Robert, so it's, you know, six ASCII characters long, and you say this is context time context type text plane. The endpoint here asks us to use a post endpoint and use the server URL. So we're supposed to construct a URL that has the string app date, and then this curly brace business here means encode your date somewhere and put it in the URL.

07:01 Um, so here we are. I showed you three very simple edge cases and we ran into these more or less right away, and at the time you don't really think about it very hard, you just make a choice and move on. And then as the team grows and the software grew into, you know, 10 teams, 20 teams, three geographies, and it's not so easy to just walk over to your colleague and say, hey, should I return null here or two or four or what is your thing?

09:18 Ah, so you can see we're not really in a happy place anymore. Um, so this is sort of the birth, birth, uh, of Conjure. So we looked at what we had and we thought, how do we get out of this? And there are a couple of options for for how to get out of this. Uh, the alternatives are, well, uh, Jax RS is where we are today. So that didn't really work because it's too flexible. There were too many edge cases you have to consider, too many ways that people can disagree. And it's also not platform independent, so it wouldn't really work for us. Um, so other things you would look at, and this is roughly 2015, maybe 2016. Uh, other things you would look at, um, Swagger, gRPC, Thrift. Well, gRPC didn't exist at the time, so that's a little unfortunate. It would would have been great, but there was no gRPC. Uh, Swagger has the same problems as Jax RS in the sense that it was too flexible, right? If you if you know Swagger, uh, or I think now it's called Open API, it basically lets you encode the whole HTTP spec and it's just there are too many choices for people. I don't want people to think about RPC, I just want them to make their method call and be done with it.

10:18 Uh, Thrift would have been a fun option, but the main problem with Thrift was back compatibility. So we had all these HTP JSON endpoints and we thought that moving dozens of developer teams and 20, 30, 40, I'm not sure at the time, uh, endpoint sorry, services with hundreds of endpoints or thousands of endpoints to a different mode would be extremely disruptive. Um, so what do what did we do? Well, we wanted to keep our wire format backwards compatible, uh, but we wanted to get the benefits of simplicity and platform independence. So we we came up with a simple, sort of JSON format that would describe our APIs in a simple way and an implementation that fits our current wire format. So, and we call this Conjure. Uh, so at this point we had implemented our own RPC, and it's not actually so crazy, right? We we got there with sane choices and all of a sudden we have our own thing. Uh, so just to repeat, the idea would be it should be backwards compatible to what we had, uh, and it should be language independent. And the benefits we got was, well, we could move at our own pace. Uh, we were not relying on someone to merge our poll requests when someone didn't work.

11:20 Um, and, um, also, you know, we we narrowed down the specification for Conjure to be as simple as possible, to express just the thing we need and nothing else. So this is the big contrast to things like, um, Swagger, for instance. Cool. What's the architecture? Uh, it's very, very, very simple. Uh, who has used gRPC in the room? Uh, okay. So for all of you, this is not rocket science, and I wrote this on here. So this is very simple and very standard. We did not invent this. So as a user, you write a thing called a Conjure API definition, and then Conjure comes with a number of generators, these errors here, that generate native code in your particular language. It's exactly the same architecture as gRPC, Thrift, all of these places. So if you're living in Java world, you run the Java generator. If you're living in TypeScript world, you run the TypeScript generator. And our task was basically to implement these errors. Um, so I mentioned we wanted Conjure to be as simple as possible. So what what are the ingredients? Um, so there are two main ingredients. The first one are objects. So imagine in a standard API, you talk to a URL and you typically send some request object.

12:23 And the standard choice, obviously, is JSON. And what what ingredients do do you have in JSON? Well, you have things like primitives, so strings, Booleans, doubles, date times and so on. You might have collections of these things. Uh, they might be ordered or not ordered, so maybe you have lists and sets, maybe you have optionals, which are singleton collections, and you have nested objects. So these are the sort of nested JSON, uh, objects. Uh, so this would allow you to encode something like a person is a name and a date of birth and a list of phone numbers and a list of addresses that where that person lives. Cool. So these are objects. Uh, the second thing is endpoints. In Java land or object-oriented programming, these would be method calls. Uh, if you talk HTTP, you have sort of four main verbs. You have get endpoints, put, post, and delete endpoints. You have different types of parameters. You can put your parameters into, uh, the the path of the URL, into the query, so the question mark thing at the end, or in the body, which is, um, where the JSON content would go. And you have return values, which are the HTTP return values. And, uh, as a special ingredient, because we want all of our API API and endpoints to be authorized, uh, we put a special thing for authorization.

13:30 Uh, so in in a nutshell, this means that every endpoint that you define in Conjure has to have a first parameter that is an authorization token. You have no other choice. And this is sort of in your face for developers. You want to talk to this endpoint, where's your authentication? Uh, what does it look like in practice? You can't see the top line, unfortunately, it says types up here, so these are the objects that you can define. And I'll show you a quick example of an API that is basically a flight listing service. Uh, when you talk about flights, you might want to talk about airports, and you can see you can define aliases of strings, uh, and then there's there will be two endpoints, one to search, um, and one to list flights. And a search request contains things like where you're flying from, where you're flying to, and when you want to fly. And then as a result, you'll see in a second, you get connection objects, and a connection object is simply, uh, where does the flight come from, where does it go to, and what's the flight number? Here's the example of those those two endpoints. There's a search endpoint, the top one, uh, it's a post endpoint, uh, unfortunately in HTP, you can't use get with, uh, uh, with payloads, with bodies. So we I just use post here.

14:31 There's the URL, and it's it's extremely simple, right? You can provide arguments. So in this case, there's just a single argument that is the search request and you get a single response. Um, the list one is a little more complicated because here I decided to encode some of the information in the URL. So you see, you know, it's from airport and a particular time, and these two arguments here, they will be automatically mapped to the, uh, path parameters that you specify up here. And it returns the same thing, a search results. Cool. Uh, we provide developer tools. So we have hundreds of developers. We don't want them to run clients themselves, so it's integrated in their standard tool chain. So in Gradle, um, uh, you know, this would be, sorry, in Java, this would be Gradle tooling, so there are, there are some Gradle tasks that compile Conjure and compile objects or TypeScript objects separately. Um, we have, you know, this was one of the good lessons for me, uh, if you want people to use your stuff, you have to make it simple. Um, nobody is going to use your thing if they first have to read documentation for two days. So by, by integrating it into Gradle, uh, it was basically a no-op adoption for people, right? We provided these tasks and with the standard build task, you would get this for free.

15:35 Uh, so here's an example of what this looks like in Java. Um, I don't have to explain too much, most people will have seen something like this. So this is a Jax RS interface that was generated and it, you see the one-to-one correspondence between the, uh, API definition and Conjure and this thing. However, this thing here is Java specific. It's useless for anyone who's not talking Java. Uh, this thing here is not Java specific at all. Um, it's mildly HTP specific, but only because of some small little notions like this post endpoint here or this get here. Everything else is not really super HTP specific. So it's really very, very simple and just describes the API you want to expose. Um, to use this, well, you have to make a client, uh, we have some factory methods to make clients, so this thing makes a client for a, uh, I omitted the class, but it gives you a flight search service, so an implementation of the thing I just showed you. Uh, you have to provide a URL and some SSL, all of our APIs are secure, so you have to provide tokens, but also provide SSL configuration. And then you use it just like any other Java method.

16:35 So right here, in in these two examples, you can search or you can list flights. You have no idea that you're even talking RPC. It just looks like Java. And again, this is not rocket science, you know, gRPC and all these other libraries are doing exactly the same thing. Uh, here's an example in TypeScript. This is maybe a little more rocket science because most other libraries don't really provide TypeScript support out of the box, uh, we use TypeScript for all of our front ends because we like the type safety. Uh, and it looks very similar, right? You make a search request with your static information and you fire it off and you get your result. There are some differences between the different languages, so you want, we wanted it to be idimatic in every language. So in TypeScript, um, you can see that it's a little weird, you define a function, uh, that eventually returns some sort of anonymous lambda, something something. This is how TypeScript developers like the world in Java, you don't really have this, but we wanted it to be idimatic for all the languages that we support. Um, cool. So quickly, um, how do you implement such a thing? So we, I said we made our own RPC system. What does that actually mean? Well, you have to first say what you actually mean by, what are the semantics of your API definition?

17:39 So you write such an API definition, what does it mean? And in, in RPC systems, this is called the wire format. So, in other words, when someone calls this code that you generated, what happens, uh, at the HTP level? When you have that, you write a code generator, so you write basically a thing that takes this, uh, YAML document that I showed you and generates code in different languages. And then you typically write some plumbing library that does the connection. So when someone calls your method, then something has to happen at the wire format level. So this plumbing library is the implementation of the wire format, if you want. Uh, and you know, this is not particularly complicated, again, not rocket science. So when someone calls your flights object, this is the thing you generated, this flights thing here, and calls dot list with some parameters. The only thing you have to do is you have to use your HTP library of your choice, and you have to issue a HTP call that looks like this. That's all you have to do. So the most of the work is actually serialization and deserialization, uh, of these objects. And then there are some nuances around the exact HTP specification and specifics, so what do you do with certain error codes?

18:41 Do you retry them or not? Are there timeouts, um, and so on and so on. But, you know, the the basic thing is actually not very complicated. And so, retrospectively, was it so crazy to write this? Actually not. I mean, the prototype for this was maybe a week, and then obviously there's like a lot of fixing and there are bugs and so on, but it's not actually insane. It's way less insane than a database, for sure. Um, Moving on to some cool stuff. So now that we have our own RPC system, I showed you how we got there. I showed you the architecture, I showed you how to implement one roughly, I showed you how to use it. Now we have one, what can we do with it? So here are a couple of fun things you can do with it. The first thing is, um, Contra itself is defined in Contra. So what I mean by this is the specification that you saw for APIs itself is a valid Contra document, and this is pretty powerful because it allows you to do a thing called bootstrapping. So if you've had compiler classes in university or you wrote your own compiler, you know what bootstrapping is. It's the idea that when you write a compiler for a language, say L, uh it's really nice to write that compiler in L. And why is that? Well, because in L you you you often have primitives that are really useful for manipulating L itself.

19:52 Um, this is called meta programming in in many languages. So Scala for instance has very rich support for parsing Scala, but also for generating Scala, for doing this kind of meta programming. So how do you do this? Well, you don't have an L compiler, so how on earth are you supposed to write a compiler in L? Uh there's a trick. So the trick is you you first make a simple compiler for L in a totally different language, and can be the dumbest compiler on the planet, can be slow, ugly code, no comments, no documentation, uh formatting is all screwed up. It doesn't matter, because the only thing you use L for is to compile itself once, and then all of a sudden you have an L compiler. And then you have L code and then you can work with that. So it seems a little weird, and you know, just like bootstrapping it shouldn't work, but it actually does work. Um, so in another word for this is self-hosting. So this the idea of self-hosting is that the the language describes it hosts itself. Uh, and today, so when we started with Contra, all Contra compilers were written in Java. Um, this was great for the Java generator, but it was terrible for all other ones. If you try to generate TypeScript in Java, it's really not a good time at all. So, at some point, about half a year in, we we realized this, um, and we I forgot the numbers, but I think we went down like 75% in code complexity for the TypeScript compiler, just by moving from Java to TypeScript as the compiler itself. Uh, so this was a really good lesson. Um, another fun trick. Uh, I call this back to the future. So, this is sort of the retrospective on our journey. It's 2013 or 14. Uh, this is the uh the mountain of enlightenment for RPC. And clearly, in optimization problems, you always want to climb to the top. You want to be as enlightened as possible. So, in 2014, we thought, you know, if we climb up here to Jackz and Drop Wizard and Faning, we'd be awesome, super enlightened. So, that's exactly what we did. We went up here, we chose the best tax stack we had at a time. [21:42] And then 2015, uh, you know, GRPC comes along, obviously, it's much more shiny, it's a much higher mountain, the weather is better, everything's great. But, unfortunately, we walked here. That's sad, but you know, that's the choice we made. And it's really painful to climb back down and climb up the other way. Or in concrete terms, these API migrations, they're really, really disruptive for all the developers and it's not a cheap thing thing to do for an organization with, you know, thousands of software engineers. [22:06] Uh, what do you do about it? Well, I'm going to convince you that there is a magic blue balloon that just takes you from here to here, and unsurprisingly this balloon is Contra. So, how does this work? So, how do we take what we have today and move to GRPC if we if we wanted to? It's not something we have planned today, but we could if we wanted to. [22:23] Uh, and my claim is by moving from Jax RS to Contra, it's much easier for us to actually migrate here if we ever want to. [22:31] So, it's very simple. So, the first thing you do is you write a thing called Contra Protobuf. So, this is a generator that takes the Contra API, so the ones that we have, and it generates a corresponding GRPC API. [22:41] So, a Protobuf file, so it takes a Yaml file for Contra APIs and generates a Protobuf file for GRPC APIs. [22:48] And you implement a thing called a Contra GRPC bridge. So, this is the thing that eats a Protobuf definition, a GRPC definition, and implements a bridge to a Contra API. [22:59] So, it basically just does both. [23:01] So, at this point, you have dual mode for free. [23:03] So, you implement one service in GRPC, you put the bridge on top, and you get an endpoint in GRPC and an equivalent endpoint in Contra. [23:12] So, now your service speaks both at the same time without any extra work, you just implemented once. [23:18] Uh, and then eventually, you know, you migrate people to the GRPC endpoint, and when you have zero left, you just remove Contra support and you're on GRPC land. [23:25] And the developer of your service didn't actually notice at all, because all of this stuff is something that I, in this case, as the Contra maintainer, can just do behind the scenes. [23:33] I write this compiler, I like put it into their build system, they don't notice. [23:37] Well, they might see the pull request, but it doesn't really harm them. [23:41] Um, all of a sudden their services speak two languages, they still don't know, they implement the same stuff, and eventually they no longer speak Contra, and they still didn't know. [23:47] They see the same code in front of them, they still don't know that something changed. [23:51] Uh, and so by doing this trick, we basically moved ourselves from where we are today to a future world that might be more glorious or maybe not, I don't know. [24:00] Um, why does this work? [24:02] This is sort of the good lesson here. [24:03] It only works because, as I wrote on here, uh, Contra is smaller than GRPC in the sense that it's simpler. [24:08] You can only do these translations from simple systems to complex systems, and that's why it's really beneficial to do something that's as simple as possible when you start with this journey. [24:19] Um, the same idea would apply to things like GraphQL. [24:21] If you're a front end developer, I'm sure you've heard of or have used GraphQL. [24:25] Uh, it's sort of the new hot thing for expressing multi-service, multi-functional, um, front ends. [24:31] And we can do a very similar trick. [24:32] If we wanted GraphQL for our back end services, um, we could do the same thing that we could do for GRPC. [24:38] We would implement the bridge and we get everything for free, more or less. [24:41] It's a little more complicated here because you have cross-service dependencies, but this is a very simple first step in that direction. [24:48] Uh, some other cool ideas, Contra back compatt. [24:49] So, uh, is this new API compatible with this old API? [24:53] So you're a maintainer, you uh, you want to make an API change and you wonder, does this break my consumers? [25:00] Well, because we have these really simple definitions, it's very simple for us to write a checker, and we wrote one, uh, that takes these two documents and says, is the new one actually compatible with the old one? [25:11] Um, I won't go into detail on the other ones, but Contra supports API docs, so we can render API documentation for every language that looks like the documentation in every language. [25:19] We can think about deprecations, so what happens when you want to remove an endpoint, uh, logging metrics, you get proxies for free, more or less, so you can take a proxy that speaks something on the on one end and something else on the other end and so on. [25:30] Uh, so a lot of cool stuff you can do once you're in control of your RPC system. [25:36] Uh, wrapping up. [25:36] So, Contra today, uh, defines all of our APIs across all of our services. [25:41] So, these are, I think, 500 plus different APIs, each of them with between, I don't know, 5 and 100 endpoints. [25:48] Um, it has thousands of users inside Palantir, so all of our developers use it every day. [25:53] And many of our customers write custom applications against Contra APIs, uh, and we open sourced it last year. [25:58] So, if you're interested, take a look. [26:00] Uh, you can just search for, I don't know, Palantir Contra or Contra RPC or something, or memorize this URL. [26:05] Uh, and you can check it out. [26:07] Um, and finally, some lessons learned. [26:09] So, these are lessons, mostly my own lesson, lessons. [26:12] So, first of all, Contra made me a better developer. [26:15] So, a lot of the things that I discussed, I didn't really know much about when I started. [26:19] So, now I know all the internals of RPC and of HTTP. [26:22] I read GRPC source code, was very enlightening. [26:24] It was quite cool. [26:26] Um, so it made me a better developer for sure. [26:29] And then the main two lessons, uh, the first one, simplicity is better than expressiveness. [26:33] So, when you design these things, make them as small and simple as possible. [26:37] I mentioned before, this sort of blue balloon is not possible if you start with something complicated. [26:42] So, that's one part of the of the equation here. [26:44] The other part is, if you have thousands or 2,000, 1,000 or 2,000 developers to heard, uh, give them something that's really simple because they're not interested in understanding the nuances between, you know, get, post, put, serialization dates, this way, optionals. [26:58] They just want to use it and be happy. [27:00] Uh, so the simplicity is really sort of the the strongest selling point for us internally. [27:05] Um, the the the same point from a different angle, consistency is better than independence, at least for RPC. [27:11] When you read blog posts and so on on microservices, they will tell you that, uh, microservices are great for independence, right? [27:17] Every team can choose their language, it's polyglot, it's great. [27:20] RPC is the one place where this is not true, in my opinion. [27:23] Uh, you have to give all people exactly the same thing because the RPC system is the thing that couples all of these services together and it's the one true cross domain concern. [27:33] And it doesn't matter if you use event sourcing or if you use restful APIs, um, it it doesn't matter, the RPC thing, the system that different, uh, uh, components used to talk to each other must be the same for all of them, and it must be as simple as possible. [27:48] Uh, so, you know, looking back, I started with is building an RPC system a stupid idea? [27:53] It probably is if you start from scratch. [27:55] I would never do it today. [27:56] But if you look at the history, I believe we actually took the logical step in every in every point in time. [28:02] Uh, we picked the best tool we had at the time, and we evolved with it, we fixed our bugs, and today we're actually pretty happy. [28:08] So, there's no plan for us to move somewhere else because we're in control of our own pace and we can fix our own bugs. [28:13] Um, we can add features that we want, and we can reject features that we don't want. [28:18] Um, and, you know, it does all the things that we need and is actually pretty simple. [28:21] Uh, last note, if you're interested in some of this, I, I wrote blog posts about this and other tech at Palantir, medium.com/palantir, you can read all about it. [28:30] And I, I hear this is a running joke, but if you really want to work for us, we have offices all over the place, including Tel Aviv. [28:35] Thank you.

← From Clever code to Better code · How we built a scalable end-to-end architecture for real-life machine learning →