DAVID WINER: Hi, my name’s David Winer I’m a product manager at Google focused on the Kotlin language, compiler, and libraries JAMES WARD: Hi, I’m James Ward, a developer advocate at Google Cloud, and I’ve been doing a lot of Kotlin lately DAVID WINER: And we’re here to talk today about what Kotlin is and why it’s a great language for Cloud So let’s get started First, we want to give an overview of what Kotlin looks like and what some of its features are Kotlin is a JVM language, so it looks a lot like Java in some ways and uses a lot of the same machinery under the hood There are some cool kind of differentiating features, though The first is that nullability is baked into the type system in Kotlin, so this question mark operator that you see here actually makes it so the compiler and tooling catches a lot of the potential null pointer exceptions you might be used to seeing at runtime Kotlin also has first-class support for lambdas through this nice trailing lambda syntax And functional programming is a really core part of Kotlin as a language Kotlin also has this template expression syntax that make it so you don’t have to mess with string concatenation as much And this is a small one, but semicolons are optional, so one less thing to think about when you’re programming There are a lot more larger features that are kind of core to what Kotlin is as a language Kotlin has coroutines, which are these great primitives for writing, reading, and reasoning about concurrent code It’s also 100% interoperable to Java, so you don’t have to migrate your whole project over in order to start using Kotlin You can start with just one class or one file Kotlin is also expressive and concise We routinely hear from teams how nice it is to start writing in Kotlin because you can say more with less code And last but not least, Kotlin’s trailing lambda syntax, which you kind of just saw an example of, lets you create domain-specific languages, or DSLs, really easily This lets you do things in a concise way that you wouldn’t expect from a type-safe JVM language, like generating markup, configuring your build, or really concisely configuring routes for a web server So we talked about what Kotlin is and now wanted to dive into the history of Kotlin and why teams are adopting it The Kotlin project was originally created 10 years ago, but it really started accelerating in 2016 That’s when 1.0 was released and Android announced official support for it in 2017 It grew really quickly on Android, which is one of the largest Java language platforms in the world And by 2019, more than 50% of the top 1,000 apps on the Play Store were using Kotlin And this year Kotlin pulled ahead to become the number two language on the JVM, ahead of Scala It’s routinely reported as the number one fastest growing language in the world, and we’ll look at a couple of concrete examples of that So here’s just a sampling of companies that are using Kotlin in production today, and you’ll notice that many of them are mobile-first companies A lot of them started using Kotlin on Android, but many are growing it on the server side as well The main takeaway here is that Kotlin has had a lot of time to incubate and grow through usage in some of these flagship companies It’s production ready And at Google, we’re also going all in on Kotlin It’s our preferred language for Android app development now And here’s an example of just some of the results the Google Home app team saw when moving 100% of their new feature development to Kotlin They saw a 33% reduction in the null pointer exceptions they saw from their app And when they moved classes from Java to Kotlin, they saw on average a 30% reduction in the lines of code Additionally, we’re also seeing a lot of teams internally within Google adopt Kotlin on the server side now So metrics teams, a number of our logging teams, platform teams for a number of our largest products have just started introducing Kotlin And the internal server framework that powers Google applications is migrating to Kotlin as well Lastly, I wanted to bring the concision point to life This is one of the reasons that people seem to enjoy programming in Kotlin the most So what you see here is a plain old Java object that you might notice has a lot of boilerplate code– PersonJava So it has getters, setters, an equals method, hash code, and a lot more It might look familiar to you Now, here’s the exact same object in Kotlin, and you might be wondering what happened to all the extra code

Kotlin language features take care of generating the necessary bytecode for you, so those getters, setters, hash code are still there Only you don’t have to worry about handwriting them Instead, you can spend time being creative, implementing new features that your customers actually need, and not worrying about writing hundreds of lines of boilerplate So now I’ll hand things over to James to talk about how Kotlin can work with some of the server tools, frameworks, and APIs that you might already be using JAMES WARD: So Kotlin works great on a server and on the cloud It works great on Google Cloud, so if you’re using just raw compute instances or you’re using Kubernetes or Cloud Run or any of the other compute platforms, Kotlin is awesome because it runs on the JVM, it interoperates with Java, and a lot of the modern Java-based frameworks are now supporting Kotlin as well So, for example, Spring Boot has added great Kotlin support There’s a new framework called Micronaut that also has great Kotlin support There’s support now in Kotlin for gRPC, and then there’s a [? really ?] [? new ?] project called Cloudstate And so we’ll go over some of these, but let’s start with Spring Boot Spring Boot is the most popular enterprise framework, and usually people are using that with Java But now with the new Kotlin support, you can use Kotlin with Spring Boot, and it feels really natural You still get the annotation-based injection and all the other great parts of Spring Boot But then you, as well as that, get all the features of Kotlin, including coroutines, null safety, and all that great stuff So let’s actually take a look at a demo Let’s look at some code and deploy a Spring Boot Kotlin application on the cloud Let’s take a look at a little demo of a Spring Boot Kotlin application and run it in the cloud I’m on a repo here on GitHub– jamesward and then springboot-kotlin-reactive-demo And before we take a look at the code, let’s go get this thing deploying up on the cloud One way to do that is to hit this Run on Google Cloud button, so I hit that And now it’s going to say, are you sure? I’m going to confirm, and it’s now launching Cloud Shell Cloud Shell is a VM in the cloud with a SSH-like web interface to it And this just allows us to automate a few steps, so we don’t have to do much typing So lets clone that Git repo we were just looking at And then it’s asking me which project do I want to use I will use jw-demo It’s going to enable Cloud Run on that project, and then it’s going to ask me which region I want to use I’ll use us-central1 And now it will go through some steps to deploy this application from that source code up onto Google Cloud So we’ll check back in on this in a second because it’s going to take a minute or so to do that While it’s doing that, we can take a look at a little bit of stuff in this project First, there is the pom.xml This should be familiar This is for the Maven build Nothing too exciting in here other than the Kotlin stuff, Spring Boot stuff And you’ll see that I’m using WebFlux so that we get a non-blocking reactive HTTP server for this Spring Boot project So nothing Kotlin yet, but let’s go take a look at the Kotlin source code This is in the demo application and really basic Spring Boot application here So I’ve got my demo application class, which has my router in it, and we’ll take a look at that method in a second I’m using some Kotlin features here, like a data class called Release, and then I have a function main that starts up my demo application So what this application does– very simple It’s going to make an HTTP request out to the GitHub API to fetch the tags for Kotlin releases And this is using the web client, which is also a non-blocking WebFlux-based HTTP client So because of Kotlin coroutines– and a great feature of Kotlin– we have a suspend function So even though this is a non-blocking async call, we’re able to make it look like it’s just sequential So we’re going to get our releases, which are going deserialize into our list of release And what I was trying to do with this application is just get the actual releases, not any milestone releases So we see some other features of Kotlin here One is that the question mark or the Elvis operator used for dealing with nullability because maybe my releases were null I didn’t have any releases, or I wasn’t able to fetch the releases Then I can do that question mark [? so it would ?] chain on operations when it’s not null, so then I can do a filter with a nice little function syntax there to filter out anything that doesn’t contain a dash or that– filter out all the things that contain a dash And then I’m going to, again, use the nice nullability support in Kotlin, get the firstOrNull, and if it’s not null, then I’m going

to get the name of that release And then if any of those operations in that chain were null, then I’m going to provide a default of not found So some really nice features of Kotlin here in a Spring Boot application Let’s check back in on Cloud Shell and see how we’re doing So it looks like it’s all up and running So it did the mvn package jib:build Jib is a nice Maven or Gradle plugin that can create container images, Docker container images And so it did that for us, created our Docker container image, and stored it up on the Google Container Registry And then once we get our container image for our application up on the Google Container Registry, we can do our gcloud run deploy and deploy this application from that container image, so with some parameters there But our application was deployed, and now it’s up and running, so let’s go check it out here on the cloud and make sure that we see the latest Kotlin release here So it’s now starting up that application, making that non-blocking request out to the GitHub API, getting back that list, getting the first one after applying the filter, and there is our latest release of Kotlin So there’s our really basic Spring Boot Kotlin application running up on Google Cloud using Cloud Run, which is serverless And I’ll show you just real quickly some of the features of Cloud Run We can click through into the UI for Cloud Run, where we can also do deployments and create new revisions and check the logs and see metrics and all that kind of stuff So here we have our application up and running where we can come in and check out the revisions We can see that there was one that was deployed two minutes ago and then check out the logs for the application, see it all up and running, and see details and deploy new revisions and do great things So nice thing about Cloud Run is it’s serverless, so that means there’s no servers to maintain And it scales up and down based on our demand So that is our Spring Boot Kotlin application running up on Google Cloud OK So that was a example of using Spring Boot and Kotlin on the cloud, but now let’s talk about gRPC gRPC allows us to use protocol buffers to define our data objects So we have a language agnostic way to define something like a greeter service and then the messages that that service can handle So this allows us to use gRPC with any language It’s protocol agnostic, support strong typing It’ll generate the objects around those services and messages, and then it’ll also generate the clients and service objects to interact with those services from a client to a server And best of all, it’s super fast because it’s built on HTTP/2 So we now support Kotlin with gRPC through a project called gRPC Kotlin So gRPC Kotlin, what we do is we generate Kotlin idiomatic stubs based on the Java stubs that are generated by the Java gRPC project So this supports coroutines for doing async stuff It supports the Kotlin flow API for doing stream-based processing So that means that your services can take a stream as an input and/or return a stream as an output, and then you use the standard flow API to interact with it There’s plugins for doing gRPC Kotlin for both Gradle and Maven And, of course, it’s available in Maven Central, so easy to get So let’s take a look at a demo for gRPC Kotlin and its stream support and running on the cloud with Cloud Run Now let’s take a look at an example that uses gRPC and Kotlin You can find this on GitHub under the GoogleCloudPlatform/kotlin samples and then run and then grpc-hello-world-streaming There’s a few different examples in here We’re going to look at the streaming one And if you want to deploy this up on Cloud Run, you can just click this Run on Google Cloud button, and that will deploy it And we’ll let Cloud Shell start up and then go take a look at the source code So in the source code for this sample, let’s first take a look at the proto file This is the protocol buffer definition that defines a service called Greeter, and rpc on that service called SayHelloStream It takes a HelloRequest, and it returns a stream of HelloReply And then you can see our HelloRequest and HelloReply message objects down below So the gRPC Kotlin project will then generate the classes to interact with the service for Kotlin, and let’s go take a look at how we actually use those generated stubs Here we have the HelloWorldServer, and we’ll start up the server All that the server does is implement what happens on that sayHelloStream So you can see it takes a HelloRequest,

and it returns a flow of HelloReply because our gRPC returned that stream of HelloReply Flow in Kotlin is a way to do a stream with non-blocking back pressure and all that kind of good stuff So we’re going to emit a HelloReply, and what we’re going to do in that reply is create a new HelloReply from the builder and set the message to hello and then whatever name came in the request So we’re just going to omit that every second So that’s our server implementation Nothing very fancy there Then let’s go take a look at the client that uses– connects to that server So in this case, we’re going to use the generated stubs for this gRPC service, and we’re going to have a function in here called greet, which is going to take a name And then it’s going to do this runBlocking, where it’s going to assemble a HelloRequest and set the name to whatever was passed in And then it’s going to call the stub SayHelloStream, give it that request, and this returns a flow So that’s the Kotlin flow And then we’re going to do a .collect on here with a function that takes the response And in this case, we’re just going to print out the message that was passed in Now, down here we can see our main method where we start up the client, and you’ll see that when we start up the client, we’re going to– if there is an argument, we’re going to use it, an argument to the program If not, we’re going to just pass in world, so you’ll see what actually then gets passed in We set the name to the name that was there Then if we look back at our server, it sets the message that it’s going to emit every second to hello comma and then the name that was passed in So let’s go back over into the Cloud Shell And I’m actually just going to cancel out of this I’m not going to go through the deployment because we don’t need to It’s already deployed And now I’m going to run the client against my server that’s up and running on Cloud Run So what I’m doing is I’m taking the Docker container that was created if we had followed through with that process and let it run to create the Docker container from that source code, but I’ve already done that And then it’s going to run the HelloWorldClientKt application, and then it’s going to pass an argument to the application, which is the server to connect to And if you’ll remember, we take that argument, and we output it after hello every second So let’s now run that Docker container It’s going to go grab that from the Google Container Registry and download that to Cloud Shell to my local Docker daemon there, and so that’ll take a second to pull all that down And once that’s pulled down, it’ll start up the Docker container, run the HelloWorldClient, and then connect it to our running on Cloud Run And, remember, Cloud Run is serverless, so it scales down when there aren’t any requests and then automatically scales back up as needed And you’ll see that it’s started up that gRPC server, and now it’s outputting every second hello with that argument that was passed in So there we go That’s our Kotlin gRPC application running on Cloud Run and doing a streaming response So that was gRPC Kotlin Now let’s take a look at a new project called Cloudstate, which has great Kotlin support as well Cloudstate was created to support distributed programming through some new architectural patterns called event sourcing, CQRS, and CRDTs And I’m not going to go into all the details of what those are, but if you want to do really high-scale distributed computing, then you may need architectural patterns that support that So the new Cloudstate project is open source and has a cloud service built on Google Cloud that we can use with Kotlin to build these massive-scale applications Let’s take a look at a little demo of Cloudstate with Kotlin Let’s take a look at a Cloudstate Kotlin example that uses the Lightbend Cloudstate service to run this on the cloud And you can find the source code on GitHub– jamesward/cloudstate sample fraud OK First, let’s take a look at the code So with Cloudstate, we use protobufs to model the messages that we’re going to send and then the rpc operations that will be performed So we have a transaction with a timestamp amount, location, and description And then we have a user transaction that associates that with a cloudstate.entity_key, which allows us to do the partition of the messages And that also gives us the ability to scale this thing up and down horizontally based on what’s happening to the messages as they’re being partitioned OK And then we have a way to add the transaction, a user transaction, and get transactions

Let’s take a look at the ActivityEntity, and this is where the logic for handling those messages happens So we’ve got an EventSourcedEntity, and then you can see there’s a constructor argument with a user ID, which is our partitioning key So that’s how we’re able to partition this data In this class, we have our transactions, which is our MutableList And so whenever we get transaction messages coming in, then we’re going to be able to add them to this list so that we have our accumulated state here in this entity Then we have a command handler for addTransaction, which takes that user transaction And then we also get a command context [? right ?] [? there. ?] And then we can have our logic for detecting fraud on a transaction So whenever a new transaction comes in, we’re going to use the past data and the new transaction to calculate the velocity And so there’s, obviously, lots of different ways that we would calculate– compute whether or not the transaction is potentially fraudulent For this simple example, I’m just using velocity And if the user’s velocity goes over a max velocity, then we’re going to say that there’s possible fraud And in this case, we’re just going to output to the console But then once we have detected the potential fraud, we are going to emit a message which says, OK, we’ve done our handling of this message And then that EventHandler will trigger This will go on to an event log, which has persisted in Spanner, in Google Cloud Spanner OK So that’s the bulk of the entity processing I’ve got a [? little ?] class that just starts up the server, and then I’ve got a simulator class that is going to simulate some transactions So let’s go over and run this now I already have this up and running on Google Cloud via the Lightbend Cloudstate service, but let’s go start up the simulator, and what this will do is start adding a bunch of transactions to this transaction log And what we’ll be able to– once this starts emitting those messages, we’ll be able to see the messages that are being emitted here in the console And then we’ll go over to Google Cloud Logging to actually see if we get a potential fraud So here we go We’re now feeding a bunch of transactions into that system We’ve got multiple users, so we’re doing partitioning of the data for that entity And now here in Cloud, the log viewer in Google Cloud, now we can see the server messages from my entity that’s running up on Google Cloud and Lightbend Cloudstate So let’s go try to jump to now and see– there we go OK So it looks like we just got a possible fraud alert, and we can see the user ID, the merchant, the amount, when it happened, and then the Google Map linked to where that transaction was And if we let this continue running, we probably will see some more potential fraud alerts coming in as that simulator runs So you can see we’re feeding in tons of messages They’re partitioned based on user ID, so we get the ability to auto-scale the service up and down And that’s great for something like this where the amount of transactions that are coming in is going to fluctuate throughout the day, with probably more transactions happening in the morning and evening, I would guess, and less at night And so great We can scale our service automatically based on that There’s a container image that I built and deployed, which contains my entity code OK So that’s the quick example of Cloudstate running on the Lightbend Cloudstate service So that was Cloudstate for Kotlin And that’s it that we wanted to show you today Thanks for watching, and we hope that you will build Kotlin stuff for Google Cloud, and check out some resources for that You can go to cloud.google.com/kotlin And thanks so much, David, for joining us and showing us why we should use Kotlin DAVID WINER: Yeah Thanks, James We’re really excited to see what you build with Kotlin, and hope you enjoy the rest of Next OnAir