Video details

Server less, Code more - Aydrian Howard - Prisma Serverless Conference


With Serverless deployment you want to focus on the code and nothing else. You need a scalable backend that can provide low latency, always-on experience for your applications. CockroachDB Serverless gives developers instant access to an auto-scaling SQL database that they can spin up in seconds without having to worry about hardware details or capacity planning and pay only for what they use. This talk is geared towards developers looking to streamline the developer experience and time to ship with JavaScript, Prisma, and CockroachDB Serverless. With CockroachDB Serverless, developers can build what they dream of without worrying about their database.
đź—ŁSpeaker: Aydrian Howard Aydrian is a Hoosier living in the big city. He enjoys mentoring at hackathons and live coding on Twitch. Aydrian began his career helping bring technology to schools by setting up computer labs and training teachers and staff.
Connect with Aydrian:
đź“Ł Prisma Serverless This talk has been recorded at the Prisma Serverless Conference on November 18, 2021.
You can learn more about the Prisma Data Platform here:


Welcome to Serverless Cod Moore. This is Atticus. He's the best. This boy. He's my eight month old tricolor Pembroke Welsh Corgi. You're probably going to see him running around in the background while I do this talk. Hi, I'm Adrian. I'm Atticus Human. I'm also a developer advocate at Cockroach Labs. You can find me most places on the Internet, Twitter, Instagram, Twitch as it's Adrian. And if you're interested in seeing more pictures of this guy, you can check out my Instagram. It's basically all it is now. Okay, we're going to let you down. All right, so the term serverless has been overused. Let's take a minute to make sure we're on the same page. Serverless doesn't mean serverless. There are actually a lot of servers, but what serverless allows you to do is to deploy your apps and services without worrying about the infrastructure. It should really be called infrastructure list, but it's more of an out of sight out of mind than not at all. As the demand for your application increases, the serverless framework takes on spinning up new instances to meet the rising demand. And as that demand recedes, the framework will spin down no longer needed instances. Serverless is just a computing model that takes into account the following properties on demand and instant compute. Instances that are dormant won't use compute. The task of scaling, planning, and configuring the infrastructure are handled for you, and you only pay for the compute. You use this frees developers to focus on application logic rather than the infrastructure. This serverless execution model has been picking up speed over the past five or so years. The top three clouds, Amazon, Google, and Azure all have offerings to handle the serverless compute functions and app development. There are companies today that completely run on serverless. When I started working with Serverless four years ago, it was a hard sell, but we're at an inflection point where more and more companies are turning to it. Now that we're on the same page, what about serverless data? This is where it gets interesting. Data. Complicates Serverless today you're responsible for determining the size and scale of your database up front for a serverless database. You'd want these decisions to be made for you depending on your usage. The data should also exist as close to the user as possible on the edge, if you will. So that way, your Sydney user can have the same experience as your New York user. And lastly, you want to be sure that the state of your data is preserved when your usage scales down to zero. This is what the team at Cockroach Labs had envisioned when creating CockroachDB. In many ways, CockroachDB was architected to be a serverless database from day one, CockroachDB scales horizontally. Given its distributed architecture. By automatically spreading copies of data across different data centers, we can ensure it's always available and reliable. Since the data are replicated and spread across the world, it ensures that all users have the same experience, and finally, CockroachDB supports asset transactions to ensure consistency of the highest level, and developers can use any Postgres wire compatible tool to work with it. Cockroachdb Serverless builds on top of the core capabilities of CockroachDB. While CockroachDB can scale horizontally, Cockroach DB Serverless performs that scaling without any manual user intervention. Cockroachdb Serverless automatically replicate your data three times across three different data centers so that the data is always highly available. Consumption based pricing is a key part of serverless, and as a user, you only pay for the compute when the database is actively used. Cockroachdb Serverless automatically spins down any compute resources that are not used and back up again when needed, all without any user intervention. Traditionally, cold starts were a problem for databases. Cockroachdb Serverless can spin up dormant clusters in 500 milliseconds or less, and we're able to keep costs low with a multi tenant solution, meaning there are several end user clusters on a single Cockroach DB environment. These six capabilities constitute our definition of a serverless database. This is our North Star as we continue to build, expand, and evolve Cockroach CB Serverless. With that in mind, let's see what the end user experience looks like for CockroachDB Serverless. As an end user, the only decision you have to make is the monthly spending limit. This can be zero. If you want to be on the free tier or up to ten K a month, we take care of everything else. Whether that's scaling based on your usage or ensuring data is replicated and highly available, we will never charge you more than your spin limit. That is our promise to you. If you want to kick the tires without any commitment, we offer a generous free tier, no credit card required. All clusters are free by default. The nice thing is that regardless of your spin limit, if your actual usage is within our free resource limit, you don't get charged. So, for example, if you have a $25 spin limit, but you use less than 5GB of storage and 250,000,000 request units, then you won't be charged for that month. Now, if you do hit the upper limit of our free tier, you have two options. You can add a spin limit as low as one dollars a month, or you can continue using the database cluster, but your performance will be throttled because we want CockroachDB Serverless to be as easy to use as possible for any developer. We've partnered with the Prisma team to build the Prismacockdb Connector. Support for the Prisma client and Introspection will be coming soon with Prisma Migrate support to follow. If you're like me and want to start using the Prisma client, now, you can connect using the Postgres Prisma Connector. Let's take a look at using Prisma and CockroachDB Serverless using the Prisma Postgres Connector. Since CockroachDB is Postgres wire compatible and strives for Postgres SQL compatibility, developers can often take advantage of existing Postgres integrations with Cockroach TV. This demo is interactive, so feel free to grab your phones and text your GitHub username to the following Twilio phone number. For my example, my GitHub is Adrian, so I text Adrian. Don't include the app symbol. I'll give you a moment to send your text. You should receive a personalized response like the following. This application will accept a Twilio SMS. Perform a GitHub user lookup using the API and respond with a generated image. Let's take a look at the two Netlify functions that make up this application. And again, if you're still typing in the phone number, I'll give you a few seconds. The first function is responsible for accepting a post from Twilio, writing information about the request to Cockroach DB and returning the appropriate TwinL response. So we have Brittany here. I'm going to call her Brittany. She's got a ponytail. She sends the text of her GitHub username to Twilio Twilio, then post it to my Netlify function. The first thing it's going to do is verify the signature. Now this is a step that I recommend with any web hooks processing. So that way you're verifying that the request is coming from who you think it is. So we verify the signature. If it's not correct, we will return a 422 and stop functioning. If it is correct, we'll use the Prisma client to write to our Cockroach DB database and then we'll return Twiml response so the Twin will response looks something like this, but the media URL in the TwIM will response will actually call our second nullify function. This function is responsible for querying the GitHub API and returning a generated image. So whenever we give the media part to the Twin will response, it will make a get request to this URL. It will use the query string parameter of username to get GitHub user info from the GitHub API, and then it will use that to generate the image. The image is created by using the Playwright API, which takes a screenshot of the react component that is rendered with the GitHub user info. Let's take a look at how we use the Postgres connector with CockroachDB serverless. First we create our model, specifying the PostgreSQL provider. We then need to provide a database URL. I chose a store mine in an environment variable. You'll want to format your URL like this, you can find all the values in the connection parameters tab of the connection info modal in your Cockroach cloud console. You'll also want to make sure your database name includes your cluster name and tenant ID. For example, mine is Courtescorgy 744, and then we'll fill out some information about our message model. So I'm collecting the ID the GitHub username the SMS location, which is being stored as a JSON string. The function that's used for this case it's a JavaScript function and the date that the record was created on one caveat until the Cockroach DB Prisma connector is released and supports migration. You'll have to create modifier database tables manually. It's not fun, kids, but here I've created a SQL script to create the messages table for our message model. I can execute this using the SQL shell and the Cockroachdp client. Now we can run the Prisma Generate command and use the Prisma client on our serverless function. Here is the function responsible for accepting the post from Twilio and writing the request information to CockroachDB serverless. For each request, we insert a new message containing the GitHub username and location of the sender phone number to the database. Capturing the location of the phone number instead of the phone number itself allows us to store some interesting data without running into personal private information issues. I promise I'm not harvesting your phone numbers. It should be noted that if you acquired your cellphone number in one area and then moved to another, the location is associated with the phone number, not your actual location. So for example, I live in Manhattan, but I got my phone number when I was living in Indianapolis. It starts with three, one, seven. So my location is always going to be recorded as Indianapolis, Indiana instead of Manhattan where I live. If you're interested in looking at the source for this application, you can find it in my GitHub repo. It includes the functions to generate the image and how to deploy the Prisma client using Netlify. Also check out some of our other serverless resources. Thank you and Atticus is asleep, but he says goodbye.