Serverless and Spring Cloud Function

We have been discussing going to a more serverless architecture at work and so I decided that I should do some research to see where that stuff is now. When I was at Choose we used AWS Lambda to implement the backend of an abandoned shopping cart service. We would then use that data to drive an email campaign to encourage the users to come back and finish purchasing an energy plan. It had a huge effect in driving conversion rates and we were able to implement the service in about 25 lines of vanilla java code. I opted not to use Spring as I judged the startup times to be too slow for it to be worth it. To manage libraries we used the maven shade plugin in our build process to build a fat jar.

In my current role we will deploy on AWS lambda as well. One thing I am looking for in considering serverless frameworks is what allows the developer to have a very nice flow as well, as back when we did the abandoned cart service debugging was a painful experience. At that point I think we did a bunch of console logging messages to figure it out. That won’t scale up for a development team that is going heavy into serverless it is too inefficient.

Then last week I came across this Spring Tips video about a new project Project Riff. I am happy to see Pivotal getting into this space as they build great frameworks and tools. And Riff seems no different it allows a developer to easily install it on their laptop and seems like it has first class support for Google Cloud and anyone’s infrastructure that is already running Kubernetes.

So I really liked the Riff part of that video. But then I was watching the Spring Cloud Function part. Now everyone who knows me knows that I love the Spring framework. I have been using it since 2008, and I think in terms of what it can do for you on Java server apps there is nothing else close. It really accelerates developer productivity and lets them focus on solving business problems. But as I watched this video I was sort of like I don’t get it. Like year I get that people want to use Spring in serverless, but when you watch it take 5 seconds to stand up a function, to me that is a deal breaker. It is too slow and if you are being charged by the second for your service paying 5 seconds to execute a function that will take less than a second is too expensive.

I was pretty much ready to write Spring Cloud Function off as a bad idea based on that then I saw something on Twitter about Dave Syer demoing something at Spring IO this week which brings massively faster startup times. I suspect it has something to do with the spring-context-indexer that Juergen talks about in this video, but I have been unable to find any examples or documentation on how to use it.

The other thing I need to take a look at is the AWS SAM Local tool. The big pain point I had doing Lamda’s a couple of years ago was around debugging them. This looks like it may solve that problem for us by giving us an convenient way to hook this into our development lifecycle.

AWS Lambda or should I call them nano services?

Recently at work I worked on a project using Amazon AWS Lambda. This is a cool concept. Amazon calls it serverless computing, but really what it is, is abstracting the server so that you can just focus on a small task that needs to run.

In this case we had a rest endpoint that just stores some data in a database. ┬áIf we think about a traditional Spring Boot Microservice we would probably do Spring Data JPA, point it at a mysql DB, and then have some rest controllers that talk to a service tier which persists the data. With Spring Boot this isn’t much code, but you still have some embedded Tomcat Server and a fair amount of ceremony for doing something very simple. After building the app you will need to deploy it to Elastic Beanstalk instance or else an EC2 Nano Instance or something similar. That is a lot of DevOps overhead to do something very simple. With Lamdba we can create a simple class that takes a pojo java object (Jackson style). With Lambda you don’t have Hibernate, you are just dealing with raw JDBC but when you are just inserting 1 Row into a Database you don’t really need am object relational mapping. You then use Amazon’s API gateway to send any requests to an endpoint to the lambda function and you are all good to go.

That got me thinking S3 now has the ability to serve an S3 bucket as a website, so you could drop an angular app into an S3 bucket and serve that up and then point it at API Gateway which then hits a Lambda and talks to an RDS instance. If your site didn’t have much traffic it would be a super cheap way to host it as Lamdba is billed based on how much compute time you use and in our case our task runs so fast I am not sure we will even break out of the free tier with all the traffic we get. You could run an entire site with no server instances provisioned which is cool. In reality I think as the app grew it would be hard to manage as separate lambda functions and you would benefit greatly from a proper framework like Spring, but for something very small and light this seems super cool. The other neat thing about Lambda is the wide language support you can use, so I wrote my Lambda in Java and my boss just made a Lambda to do some logging that was written in Node. It is a super cool concept worth checking out.