Larry Ellison was Right (kinda)! TypeScript Stored Procedures for the Modern Age (James Cowling) — Transcript
Full transcript
- 0:05[Music]
- 0:06Nationwide
- 0:10hat sequel or death yo son what are you
- 0:13going to choose yo check it this seminar
- 0:17is filmed at Carnegie melon
- 0:19University thanks a lot
- 0:23Google all right everyone it's a new
- 0:25semester uh we're super excited today to
- 0:28to kick off the new seminar series on
- 0:29SQL or death and the goal of this is for
- 0:32to discuss attempts to replace SQL uh
- 0:36and then attempts to make SQL better
- 0:37through different czers so today we're
- 0:40kicking off with attempt to replace SQL
- 0:42uh we're super excited today to have
- 0:44James calling he is the co-founder of
- 0:47convex which is the the system or the
- 0:49framework he's talk about today uh prior
- 0:52to that he was in charge of all
- 0:54databases at Dropbox for several years
- 0:56and then prior to that he got his PhD in
- 0:59computer science science uh working with
- 1:02with Bara Li call so if you don't know
- 1:03James let me put it this way there's
- 1:05Brandon Greg Steve Irwin James calling
- 1:09those are the three most important
- 1:10people that made the the biggest
- 1:12contributions uh from Australia to
- 1:14computer systems so that's why it's a
- 1:16big deal for us to have James today
- 1:17James the floor is yours go for it thank
- 1:20you Andy if you have any questions for
- 1:21James interrupt as you go along as he
- 1:23goes along that way he's not talking to
- 1:24himself for an hour excent please jump
- 1:26in and if I don't um if I don't notice
- 1:28your chat pop up because I'm like
- 1:29looking forwards um just unmute and yell
- 1:32out um I won I won't mind um so cool hi
- 1:35everyone and today I'm going to talk
- 1:36about this idea called database stored
- 1:39procedures and this is the ability to
- 1:40run real code within a transaction um
- 1:44I'm going to argue that these are
- 1:45actually a pretty good idea even though
- 1:47a lot of people hate them uh and I think
- 1:49that we should revisit these but not in
- 1:51some weird esoteric language but just in
- 1:53typescript right um so Andy gave me the
- 1:56bio gave my bio already um my
- 1:58backgrounds in in distributed databases
- 2:01um design the storage system at Dropbox
- 2:03um which stores multiple exobytes of
- 2:06data and and design the database which
- 2:08runs I don't know several million
- 2:09transactions per second um but um not
- 2:12just um a database Builder a lot of my
- 2:16my time has spent managing teams that
- 2:18have The Misfortune of using databases
- 2:20um and I have a company now called
- 2:21convex um all our customers are
- 2:23application developers have to build on
- 2:25top of databases and so I want to really
- 2:26talk today about addressing some um
- 2:30reasons why databases aren't meeting the
- 2:31needs of application developers right so
- 2:34there's a talk about databases hopefully
- 2:35with this audience I don't need to
- 2:36explain this but there's this annoying
- 2:38um Trend right now to conflate these
- 2:41terms SQL database with rdbms right
- 2:44relational database so my SQL post these
- 2:46are relational databases they have
- 2:48structured data and a storage engine and
- 2:50transactions and some way of quering
- 2:51them and just so happens that way of
- 2:53quering them is is SQL um but they're
- 2:57great databases otherwise right and so
- 2:59this is not an argument against um
- 3:01relational databases this is not a pro
- 3:03nosql talk in the in the sense that no
- 3:06SQL means no relational databases this
- 3:09is just an argument that we're argu
- 3:11we're interacting with our databases I
- 3:13think in the wrong way so um I'm going
- 3:16to start off with some Basics and we'll
- 3:18get more advanced as we go so you know
- 3:20the real core building block of most
- 3:21databases is the transaction right this
- 3:22is the ability to execute stuff
- 3:24atomically it's the a in acid it means
- 3:27you can do multiple things uh and during
- 3:29the transaction you don't have to think
- 3:31about the world changing underneath you
- 3:33right so um databases without
- 3:34transactions is like you know writing
- 3:36multi-threaded programs with shared
- 3:38memory and no locks right they don't
- 3:40really work so a transaction reads some
- 3:42stuff writes some stuff and makes some
- 3:44decisions in the process so let's start
- 3:47with um what I think the most basic
- 3:50transaction kind of everyone knows this
- 3:52is kind the the classic transaction
- 3:53people talk about in textbooks it's the
- 3:55bank transfer example right so this is
- 3:57an example where you have a sender and a
- 3:59recipient and you want to move some
- 4:01money from the senders account to the
- 4:03recipient's account um provided the
- 4:06sufficient funds and my challenge to you
- 4:10is to write this query in SQL uh write
- 4:13this query in actual SQL in in in pure
- 4:15sequel not in another programming
- 4:17language talking to SQL go write this
- 4:19query in real SQL um and if you're
- 4:22watching this on on on video you can
- 4:24pause this and go and try and do this
- 4:25challenge right cuz it's not so easy cuz
- 4:29you're going to want use an if statement
- 4:30in there and you can't do an if
- 4:32statement in in pure SQL so you're
- 4:35probably going to look up various SQL
- 4:37extensions but you can do this in SQL
- 4:39right so here's an answer in um my SQL
- 4:43at least uh this is a this is a um I
- 4:45don't think this works in postgress
- 4:46because I don't think you can do two
- 4:48table updates at once in postgress but
- 4:50you can in my SQL so this is an example
- 4:52of you know a short but relatively
- 4:54convoluted join that will do this thing
- 4:57and here's an answer how you could do
- 5:00this in postgress and I believe this
- 5:01works in postgress there's a there's a
- 5:03case statement in SQL there's no if
- 5:05statement um this looks kind of simple
- 5:09but then if you stare at it a bit longer
- 5:10it's not that simple and I don't think
- 5:12you would have just written this out by
- 5:13yourself if I just asked you and gave
- 5:15you a minute to do so um and this is a
- 5:18really simple example this is a this is
- 5:19a very simple bank transfer a real life
- 5:22bank transfer involves all sorts of
- 5:24checks and logging and fraud detection
- 5:26right and so You' never be able to write
- 5:28this in SQL and you're probably going to
- 5:29say this is stupid James like why are
- 5:31you asking me to implement business
- 5:33logic in a query language this is not
- 5:36how people use SQL and I would say
- 5:38exactly right SQL is not a language for
- 5:40business logic applications are made up
- 5:42of business logic and SQL is a query
- 5:45language um so the reason that was hard
- 5:48is because you want to use control
- 5:49structures what you want to use a
- 5:51procedural language and SQL is a
- 5:54declarative query language right and I
- 5:57gave you the most simple example in the
- 5:58world of a bank transfer most things are
- 6:00much harder than this right and of
- 6:02course no one would ever try to do this
- 6:05in SQL and no one would assume that
- 6:07people do this in SQL except um a few
- 6:10database researchers right so this is
- 6:12this is a this is a you know a bit of an
- 6:15opus this is a classic paper by Andy
- 6:17paver you might know him um and there
- 6:19there's there's two points made in this
- 6:21paper right uh you know it's a bit of an
- 6:23irreverent paper obviously but one is
- 6:25that is that the database research
- 6:27assumes serializability and also
- 6:29database uh research assumes that people
- 6:32use stored procedures run everything in
- 6:34a single Atomic block of of SQL code um
- 6:36but in the real world no one actually
- 6:38does this right so what do people do in
- 6:42the real world so they've got kind of
- 6:44two options in the real world one and
- 6:48these are both real options by the way
- 6:50right one is the one you'd probably
- 6:51expect you write API services in some
- 6:54other language on the server that talks
- 6:56to the database or two which is a very
- 6:59popular and very common model is to just
- 7:01give up on transactions and leak your
- 7:03entire database to the
- 7:05client so I'll talk about the the server
- 7:08side API stuff first this is probably
- 7:10how you think a database normally works
- 7:12or how you normally interact with
- 7:13databases if you're at a larger company
- 7:15this is how you would interact with the
- 7:16database you have a front end and a
- 7:18backend and the front end can't talk to
- 7:19the database right the front end talks
- 7:21to an API service and that API service
- 7:24talks to the database and runs all the
- 7:25business logic right so there's this
- 7:26pretty um hard dividing line between the
- 7:29front end and the back end here and only
- 7:31the back end can talk to the DB and
- 7:34here's an example of how you would
- 7:36implement this bank transfer in Python
- 7:39um it's probably what you think about
- 7:40when you think of a SQL transaction it's
- 7:42not that complicated I mean you have
- 7:44select for update in here which is a
- 7:46little bit of a gotcha you'd have to use
- 7:48because you're probably running this in
- 7:49read committed um but the really
- 7:51important line here is you have an if
- 7:53sender balance is less than amount right
- 7:54you get to do um an if
- 7:57statement um so this runs into a
- 7:59transaction and the transaction stays
- 8:01open for the duration of that with con.
- 8:03cursor block um otherwise you're
- 8:06happy so this seems like the normal way
- 8:09of writing database um code what's wrong
- 8:13with it um so it's easy to do the wrong
- 8:16thing it's quite hard to test SQL code
- 8:18right because it's it's it's a text
- 8:20based language with with with weak weak
- 8:23type safety um small typos can easily
- 8:26destroy a company I've seen people make
- 8:28small typos in SQL and Destroy databases
- 8:30before in practice um it's really easy
- 8:33to blow away your whole company it's
- 8:35really easy to leave at a wear clause
- 8:37and typically in practice no one's
- 8:39really writing SQL queries like this in
- 8:41any larger company you have all these
- 8:43layers of abstraction and ORS which kind
- 8:45of hide the database calls to protect
- 8:48you from
- 8:49them it's very easy to leak uncommitted
- 8:52State this is a this is a big problem
- 8:54that you might not think about right
- 8:55when you're um in having pretty deep
- 8:57call Stacks so you call a function which
- 8:59which calls a function which calls a
- 9:00function which talks to a database it's
- 9:03very easy to externalize uncommitted
- 9:05database state so it's very easy to go
- 9:07and send an email to a customer before
- 9:10the transaction is committed and this
- 9:11happens all the time and people get you
- 9:12know welcome to you know whatever emails
- 9:15and they haven't actually been added to
- 9:16the database
- 9:18yet also it can have long execution
- 9:21times which block database resources so
- 9:23there's latency between the application
- 9:26server and the database um often times
- 9:28you be keeping locks open for the
- 9:30duration of this transaction is very
- 9:32easy to add arbitrary compute in there
- 9:34and take a long time and so you've got
- 9:36snapshots or locks around or undo logs
- 9:38or vacuuming that's not happening and as
- 9:41a result typically you have to ask
- 9:43someone to write this API for you if
- 9:45you're an application developer because
- 9:46it's quite easy to break a database and
- 9:49see this is the real key problem here I
- 9:50think and this is not like a trivial
- 9:52problem this is a real real problem
- 9:54that's affecting the industry right it's
- 9:56this front end and backend divide so
- 9:59typically there's a team that writes the
- 10:00applications on the database like the
- 10:02the team that writes the the app code
- 10:04the front end and a team that writes
- 10:05apis to talk to the database and every
- 10:08company has a bunch of front-end
- 10:10developers that are annoyed because they
- 10:11can't move fast because it takes too
- 10:13long to convince someone to add apis
- 10:15right and so you end up with stuff like
- 10:17graph qwl but even with graph qwl you
- 10:19need a team there to write resolvers for
- 10:20you right there's no kind of Silver
- 10:22Bullet
- 10:24there so all these things culminate in
- 10:27this model being inconvenient to use and
- 10:29you you don't have to trust me on this
- 10:30because developers have tried to move
- 10:31away from this model at least twice I
- 10:33can think of probably more times but one
- 10:36was the nosql movement which despite all
- 10:38the talk about web scale at the time was
- 10:41really about developer ergonomics right
- 10:43so you had this new wave of this Web 2.0
- 10:45app developers we building interactive
- 10:47websites all of a sudden and they're
- 10:49writing the full stack now and they just
- 10:51want to stick some Json in a table and
- 10:53comes along and they give you the
- 10:56ability to do that and so you if you're
- 10:58you know on the youngest side you might
- 11:00not remember this but if you've been
- 11:01around for a while you would remember a
- 11:04time when was was real hot and no
- 11:06one was talking about postgress right um
- 11:10so everyone kind of moved to this nosql
- 11:12model and I'm actually a fan of of
- 11:15and this modern Incarnation but these
- 11:17databases these nosql databases didn't
- 11:18just get rid of SQL they got rid of
- 11:20transactions and relations and large
- 11:22cases type safety right and then this
- 11:24happened right then everyone decided
- 11:26they didn't like no sequel right if
- 11:29became all of a sudden was really uncool
- 11:31to use post and then it became uncool to
- 11:33use no SQL databases right because
- 11:35people wanted transactions and relations
- 11:37and schemers and only after this point
- 11:40did we enter this new modern era of like
- 11:42postgress is cool back then there was
- 11:45like web developers weren't talking
- 11:46excitedly about postgress as if
- 11:48postgress was this exciting
- 11:50thing so people went back to the old way
- 11:54oh have API servers talking to databases
- 11:56but not for long cuz then the serverless
- 11:59movement came along and this is a
- 12:00movement that's that's happening right
- 12:02now we're in this movement and and it's
- 12:04I I don't think it's going away and I
- 12:05hope that it doesn't go away um this is
- 12:08a movement driven by the needs of app
- 12:10developers or what I would describe as
- 12:12the new serverless way of building stuff
- 12:14so how are modern apps developed they're
- 12:17started by front-end developers that's
- 12:19app they call themselves full stack app
- 12:21developers that's who starts the company
- 12:23these days right the apps are typically
- 12:25web apps and they're typically written
- 12:26in typescript and more and more business
- 12:29logic is moving to client code right so
- 12:32a lot of these apps are getting written
- 12:33in typescript on clients in a repo in
- 12:37JavaScript typescript repo that's
- 12:39getting deployed directly to hosted
- 12:40backend services like Vel or netfi or um
- 12:43cloudflare Etc so we had this old
- 12:46way and we've moved to this new way
- 12:49right and this new way I think Pioneer
- 12:51by Firebase but I'm I'm not sure who you
- 12:53know who who really drove I think
- 12:55Firebase really became the popular
- 12:57proponents of this model right and has a
- 12:59lot in common with the nosql movement um
- 13:01but people are doing this with postgress
- 13:02now right it's where you expose the
- 13:04database directly to code running on
- 13:06client
- 13:08applications seems a bit strange but
- 13:10it's a natural shift towards allowing
- 13:12developers to write their own code in
- 13:13typescript and be able to iterate as
- 13:15fast as they want on the app code and
- 13:17deploy um their backend code at the same
- 13:20time as they deploying their front end
- 13:23and in many ways this is a rejection of
- 13:25this old front end backend divide and in
- 13:28respect I think it's a good thing thing
- 13:30right I think the lack of a divide is a
- 13:33good thing I don't necessar think this
- 13:34model is a good thing right so what is
- 13:37wrong with just exposing the database to
- 13:38the client well you know you had all
- 13:40these nice attributes of API services
- 13:42like abstracting a database away right
- 13:44but now we're leaking our data model to
- 13:46the client we've got no abstraction now
- 13:47we're just letting the client code see
- 13:50our implementation inside our database
- 13:52and we need to have now roow level
- 13:54security you need to have figure out
- 13:55ways of mapping authorization domains to
- 13:59rows in tables which is very complicated
- 14:02it's it's it's fine on a demo app on a
- 14:04preview app when an application gets
- 14:06large it's quite hard to express all the
- 14:09abstraction and and authorization rules
- 14:11in in in in a couple of RLS um
- 14:14configurations it leads to thick clients
- 14:17and also it leads to inconsistency
- 14:19because we've largely given up on
- 14:20transactions in this model right you
- 14:22can't have a client on a website start a
- 14:24transaction and then just go away
- 14:27right you trust the client to keep those
- 14:30resources
- 14:31open um so what do we do if we want to
- 14:35run a real Transaction what if you want
- 14:36to do a bank transfer well I don't think
- 14:39you can really I mean um you have to go
- 14:42back to the API model you have to go
- 14:44back to having some code some on a
- 14:46trusted demon running next to your
- 14:48database which is running the running
- 14:49the bank transaction for you or you
- 14:51could use stored procedures which you'll
- 14:52get to but you can't have a client
- 14:55starting a transaction and then just
- 14:57going away so in some respects this
- 14:59movement towards this new model is a
- 15:01movement away from transactions which is
- 15:03which is not a great thing right um so I
- 15:07would argue that the people what the
- 15:09people want is functions right they want
- 15:12the ability to express their business
- 15:14logic as actual functions and and it
- 15:17wouldn't it be great if we could just
- 15:19send that business logic directly to the
- 15:21database and have it run in a
- 15:25transaction so this brings us to my boy
- 15:27Larry right the o G databas CEO founder
- 15:30of Oracle owner of big boats and and
- 15:32other things third in the world yeah and
- 15:36and newly married newly married um yeah
- 15:40second richest person in the world Andy
- 15:41you said third he's third third rich
- 15:43person in the world he bounces back
- 15:44between second and third he's third
- 15:46right now so people in the database
- 15:47world love to hate on Larry right
- 15:49perhaps they're jealous of his boats
- 15:50perhaps some other things right but I
- 15:53want to restore honor to Larry right
- 15:55obviously Larry didn't do all the work
- 15:56here okay lots of lots of database
- 15:58design has did the work for PL SQL right
- 16:01um the people have known for a long time
- 16:03it's very hard to express business logic
- 16:05in declarative ways right so in the 80s
- 16:09Oracle came out with something called
- 16:10plsql and it stands for programming not
- 16:13programming it stands for procedural
- 16:14language for SQL so procedural language
- 16:17for structured query language not the
- 16:19best acronym right but it's the ability
- 16:21to do procedural things like if
- 16:23statements and control flow inside a
- 16:26database query that looks like SQL and
- 16:29you might hear these refer to as stored
- 16:31procedures they're a stored procedure
- 16:33when you store the pl SQL on on the
- 16:36database like an API that you can call
- 16:38right so this is what it looks like um
- 16:41this is the bank transfer example in PL
- 16:45SQL it's its own programming language
- 16:47right it's got its own Syntax for
- 16:49exceptions and I don't think I added any
- 16:51logging here I guess there's exceptions
- 16:53here you can there's logging you can
- 16:55call log statements and you can do all
- 16:56sorts of things it's a it's a real PR
- 16:58pring language and you can write real
- 17:00business logic and run it directly
- 17:02within the
- 17:04database so why were these invented
- 17:08right it was an attempt to make SQL
- 17:10queries into a real programming language
- 17:13um it's a real programming language
- 17:14right it moves business logic to the
- 17:17database it supports
- 17:18loops and conditional logic it minimizes
- 17:22round trips to the database which is
- 17:23quite important if you want um good
- 17:25performance right instead of having an
- 17:26API server sitting somewhere maybe even
- 17:29on the edge quite far from your database
- 17:31right and your database you know um in a
- 17:34different location with all this round
- 17:36trip times between your application code
- 17:37in Python perhaps and your database it's
- 17:39sitting right there inside the database
- 17:41without these round trips gives you
- 17:43error handling it gives you modularity
- 17:46right sick sounds awesome right solves
- 17:48all our
- 17:50problems we can just run programs in the
- 17:54database but I want to go back to this
- 17:56paper right this paper keeps bugging me
- 17:59right cuz it says no one uses stored
- 18:02procedures right why is that why does no
- 18:04one use stored
- 18:07procedures I think you can guess the
- 18:09answer I think you can guess the answer
- 18:11because I would ask you do you want to
- 18:13use this language to write your
- 18:16application because no one wants to use
- 18:18a weird esoteric language to write their
- 18:20business logic
- 18:21right um Andy were you going to jump in
- 18:24there I saw you lean in it's based on
- 18:27Ada or P like which is the successor to
- 18:30Pascal right so it's it's it's from the
- 18:3280s it's ugly yes yes and and this is a
- 18:35bank transfer that does no Bank does a
- 18:38transfer like this right you don't bank
- 18:39doesn't do like minus one and plus one
- 18:42right there's all sorts of stuff going
- 18:43on in there right some fraud detection
- 18:45and Computing exchange rates and all
- 18:46sorts of things like that right so real
- 18:49applications Implement um business logic
- 18:51with real libraries and do you want to
- 18:53build an entire stack in this
- 18:56programming language um that doesn't fit
- 18:58at all with your developer workflow you
- 19:00probably don't want to use it right so
- 19:01people don't use it now stop press I
- 19:05realized I got something wrong so as I
- 19:07was making these
- 19:08slides I Googled how do you implement
- 19:12the the bank transfer example in
- 19:13superbase quite quite curious if anyone
- 19:15had done this right and I found a video
- 19:18that actually suggests that you should
- 19:20do this using postgress functions and I
- 19:22looked into what a postgress function
- 19:23was and a postgress function is is um
- 19:26something called plpg SQL it's it's PL
- 19:28SQL right right um so our boy Larry's
- 19:31back right um there is a Resurgence in
- 19:35popularity in stored procedures right
- 19:38it's not the Oracle version of this it's
- 19:40the postgress version which is called
- 19:43plpgsql which again fairly terrible
- 19:46acronym U but this is still this is not
- 19:49this is not how you build your
- 19:50application people aren't building real
- 19:52business logic in plpg SQL right it's
- 19:56just as hard to use as the Oracle
- 19:57version right
- 20:00so what's wrong with stored procedures
- 20:02sometimes you have like a really good
- 20:04idea I think stored procedures are a
- 20:06really good idea but you're going about
- 20:08it the wrong way um and it reminds me of
- 20:10this article I just found this article
- 20:11just before this talk not wanting to
- 20:13pollute the ocean Larry Elon employs a
- 20:15man who follows his super yacht in the
- 20:17speedboat his sole job is to retrieve
- 20:19basketballs to go overboard now I don't
- 20:21know if this is true I hope it is it's
- 20:23it's a funny article right I felt this
- 20:26is amusing this is some good intentions
- 20:27but not quite in the execution I think
- 20:29stored procedures there's a lot of good
- 20:32ideas there not quite right what was
- 20:35wrong with stored procedures one it
- 20:37doesn't solve your software engineering
- 20:39challenges right you want testing and
- 20:41types and libraries and git integration
- 20:44and standard Lo logging and to use
- 20:46Sentry or whatever right you want to be
- 20:48in the same language that you always use
- 20:50that's the whole software engineering
- 20:52thing right so it doesn't solve the
- 20:53software engineering challenges and also
- 20:56you want some isolation from the
- 20:58database so if you work at a um a bigger
- 21:00company that has dbas right you should
- 21:03go to your dbas and ask them hey can I
- 21:06can you install this um plsql store
- 21:08procedure for me and they will say no
- 21:11right they will say no because they
- 21:13don't trust you to run some code inside
- 21:16the database right unless your Finance
- 21:19you know or using Oracle might be
- 21:22different right but for any application
- 21:23code databases aren't designed to have
- 21:26huge amounts of general purpose compute
- 21:27running inside of them right and if the
- 21:29plsql goes wrong it can do bad things to
- 21:32the database right so typically they
- 21:34don't have very good isolation between
- 21:35the execution and the storage so good
- 21:40idea but it kind of solved none of our
- 21:42problems right it was inconvenient to
- 21:43write API services but store procedures
- 21:46are I think more inconvenient and it
- 21:48doesn't make life any better for
- 21:49application developers because they have
- 21:51to write everything in this weird new
- 21:53language and duplicate all their
- 21:55business logic into it right and that
- 21:56makes Larry Ellison sad right everyone
- 21:59critiquing plsql and store procedures
- 22:01and I can't stand by and watch that
- 22:03happen so what are we going to do about
- 22:05that so what if we rethink store
- 22:07procedures a little bit right if we go
- 22:09back to this code back here where is it
- 22:10here we go
- 22:13right what if this code was just in
- 22:15typescript what if was in a regular old
- 22:18programming language that we use all the
- 22:19time right what we in the most what if
- 22:21it was in the most popular programming
- 22:23language in the world right it would
- 22:25solve a lot of our software engineering
- 22:27problems right if we just integrated it
- 22:28directly into our workflow the same
- 22:30people writing the application would
- 22:32write the queries in the database right
- 22:34but then we have to make it safe we have
- 22:35to make it sure that it um oh someone
- 22:38said isn't SQL the most popular I don't
- 22:40know SQL the most popular language I
- 22:42don't know let's say let's say close
- 22:43enough SQL made the most popular query
- 22:45language uh it was uh there was a survey
- 22:49last year where from I where SQL is the
- 22:51number one programming language that
- 22:53students want to know python is number
- 22:56two want to know yeah they need for
- 22:59their careers I think if you look at
- 23:01lines of code getting written these days
- 23:03I I believe it's typescript that's my
- 23:05but hey maybe I'm wrong it's close
- 23:07enough right um I think if you if you go
- 23:09build a web app these days it's going to
- 23:10be it's going to be JavaScript and
- 23:12typescript by the way yes they're the
- 23:14same thing effectively right um all the
- 23:18stuff I'm going to say applies fairly
- 23:19well to JavaScript as well right um so
- 23:22we need to make this um yes we get all
- 23:25the software engineering benefits we
- 23:26have to make it safe right um it feels
- 23:29weird to run typescript in our database
- 23:31and so we'll get to that right so we can
- 23:33move the business logic back to where
- 23:35the database is as server functions and
- 23:38we just let the application developer
- 23:39write it in the language they're already
- 23:41using right so let's talk about the
- 23:43software engineering experience of
- 23:46building a basic app that talks to a
- 23:48database using typescript right so
- 23:50here's um a basic little shopping cart
- 23:53demonstration I've used a few times um I
- 23:56guess the source codes online there it's
- 23:57obviously a trivial little app right and
- 24:00so you can add items to the cart and the
- 24:03stock available goes down and the amount
- 24:05in your cart goes up and the prices
- 24:07increment Etc and this is a multiplayer
- 24:10application so if someone else puts the
- 24:12hovercraft in their cut it should
- 24:14disappear for sale from you right and
- 24:16there's lots of different components on
- 24:17the screen here that have to stay in
- 24:19sync um so how do we write the queries
- 24:22well we just write them as regular
- 24:23typescript functions that execute
- 24:24automically as a transaction here's a
- 24:27very very simple function that fetches
- 24:29um fetches the items available for
- 24:32purchase it just looks up all the items
- 24:34table uh where remaining is greater than
- 24:37zero but you can do something more
- 24:38interesting right so here's here's the
- 24:41mutation that adds something to um to
- 24:44your cut and it does just the obvious
- 24:46things it first checks if you're
- 24:47authorized to to do so that's out of
- 24:50that's out of screen here I cop that out
- 24:52uh then it looks up the items and sees
- 24:53if there's a remaining stock and then it
- 24:55adds adds it to um the cart and then it
- 24:57deducts it from um from available in
- 24:59stock and it just does the basic stuff
- 25:02in procedural language and in practice
- 25:04you'd call out to a bunch of libraries
- 25:05to do stuff so in practice when you're
- 25:07checking if someone's authorized to do
- 25:09something you're not going to manually
- 25:11look up the off table there's a library
- 25:13that you've written called check or and
- 25:15you just call that right so we have
- 25:17folks uh using convex who have really
- 25:21huge code bases running uh in these
- 25:23functions right it's just general
- 25:25purpose typescript or
- 25:26JavaScript and so the code just sits
- 25:28there in your folder hierarchy um with
- 25:31the rest of your application code just
- 25:32like you're building a regular web app
- 25:34right and then you know if you're
- 25:35running um you know a a web app and
- 25:38you're running mpm runev to
- 25:39automatically sync um sync changes to
- 25:42your development server it automatically
- 25:44syncs all your all your queries too it's
- 25:46just this interactive developer
- 25:47experience that developers have come to
- 25:49expect and and experience right and then
- 25:51you can run tests on them right so
- 25:53here's a simple test for an app I wrote
- 25:55is using Vest V vest vest um so it um it
- 25:59does a lot of things it mocks out um the
- 26:01user identity it mocks out time it
- 26:03advances timer it's obviously a short
- 26:05test but you can write this just like a
- 26:07real test right which is really really
- 26:09hard to do for something like a SQL
- 26:11query um and by the way it uses the same
- 26:14types as the rest of your application
- 26:16not like you know 10 different numeric
- 26:18and and and text or blob types like you
- 26:21find in in most databases and then with
- 26:24all that you get you get type safety and
- 26:26you get auto complete like you've to
- 26:28expect from a you know a first class app
- 26:31development experience you're not kind
- 26:32of leaving this nice programming
- 26:34language world into this weird text
- 26:36based world all of a sudden just to be
- 26:38clear the the typescript function is not
- 26:41running you're not like converting to
- 26:42something that runs in post grass or
- 26:44whatever Sy data system is it's there's
- 26:46a there's a JavaScript engine s on the
- 26:49server side sitting in front of the data
- 26:50system Absolutely I'll explain that in a
- 26:52second yeah but it's um it's running
- 26:54real um real code in V8 yeah got it um
- 26:58and um and then you can run rpcs you
- 27:01know you can call your client code
- 27:02directly you know to call these
- 27:04functions like rpcs or you even buy them
- 27:07to a to a react component and have your
- 27:09client update automatically so this is
- 27:12um this is the query this is the um the
- 27:14client side code to render all the items
- 27:17available for stock and it just binds
- 27:19the results of the query called get
- 27:21items and just refreshes the component
- 27:23anytime um data
- 27:25changes so this feels like programming
- 27:27because it is it's just programming
- 27:29right it's about as ergonomic I think as
- 27:31you can get as an application developer
- 27:32I think it's kind of the Holy Grail of
- 27:34interaction patterns and so you might
- 27:36ask why did this not exist already right
- 27:39uh one people don't like changing things
- 27:42in the database world right and this is
- 27:43not written in SQL so I'll give you that
- 27:45all right but the other is this has to
- 27:47be implemented correctly or it doesn't
- 27:49solve a lot of the safety issues I
- 27:50mentioned before a lot of the isolation
- 27:52issues so what would we have to do to
- 27:54kind of convince the DBA that this is
- 27:56okay well we'd have to make sure that
- 27:59these um these typescript functions
- 28:01don't have side effects they don't leak
- 28:03any any state out of the transaction and
- 28:05we have to make sure that we have pretty
- 28:06good isolation from database compute and
- 28:09memory and and transaction
- 28:12protocol let's talk about the convex
- 28:14runtime so the convex runtime is
- 28:16implementation of typescript stored
- 28:18procedures we have these two concepts of
- 28:20queries and mutations queries are pure
- 28:22functions they just read data and
- 28:24mutations can read and write right um
- 28:27the types of system matches JavaScript
- 28:29typescript we run I'm putting arbitrary
- 28:32in quotes arbitrary typescript there's a
- 28:34couple of catches right it's side effect
- 28:36effect free so we don't let you call
- 28:38fetch we don't let you talk to the file
- 28:40system that's that's pretty common and
- 28:42pretty easy to do in V8 also we happen
- 28:45to make these um these functions
- 28:47deterministic so we marck out time and
- 28:50random it's a bit of an implementation
- 28:51detail that helps with that caching um I
- 28:53think you wouldn't necessarily have to
- 28:55do that um and we run the
- 28:58all in a JavaScript environment that can
- 29:00call into the database so here's a
- 29:02conceptual model right so you have the
- 29:04database yeah you can spin up V8
- 29:07contexts these days in hundreds of
- 29:08microseconds um and V8 enforces side
- 29:12effects free and determinism and then we
- 29:14expose the assist calls that let you
- 29:17call into the database so db. getet db.
- 29:20with index for example and they kind of
- 29:21let you call into the database and I'll
- 29:23talk more about that in a second um is
- 29:25V8 so it actually supports WM um we have
- 29:28some some some customers running go code
- 29:31uh in these queries so it's not
- 29:32specifically typescript um but obviously
- 29:34it's more ergonomic um when written with
- 29:39typescript um but you can't um you can't
- 29:43actually run V8 inside a database oh
- 29:45maybe you could but you wouldn't want to
- 29:47right because databases aren't designed
- 29:48for um being highly efficient Compu
- 29:51engines right um in in in the case of of
- 29:55convex we do this kind of separating
- 29:57storage from exec ution thing right
- 29:59where all the concurrency control logic
- 30:01is is performed in our backend engine
- 30:03but all the index State and storage is
- 30:05stored in a big distributed MySQL
- 30:06cluster and con convex actually works on
- 30:09MySQL and postest and sqlite doesn't
- 30:11really matter the differences that from
- 30:12my perspective relatively esoteric um so
- 30:16the database can focus on doing reliable
- 30:18database things and we just run it on
- 30:19RDS and then all the kind of um logic
- 30:23around transaction coordination and
- 30:25executing um code in V8 runs on this
- 30:28high performance scale out execution
- 30:30cluster right so you might ask well how
- 30:33do you get transactionality if the code
- 30:35isn't running on the database machine it
- 30:37looks a little bit like the API server
- 30:39model is it not the same thing as an API
- 30:41service um not quite right so um I agree
- 30:46real code needs real transactions right
- 30:49and so this is even more important when
- 30:50you're doing like arbitary code right
- 30:53because this code can do a lot of
- 30:54complicated things and run lots of
- 30:56different C libraries you don't know
- 30:58what that library is doing inside of it
- 30:59sometimes those libraries talk to the
- 31:00database too um it's really nice if that
- 31:03can execute as a transaction extra nice
- 31:05if it supports serializability you don't
- 31:07have to reason about interactions
- 31:10between the two different
- 31:12functions um and so how do we do it um I
- 31:16deleted a slide about why we don't use
- 31:18two- face locking I can talk about that
- 31:20afterwards if you'd like two- face
- 31:21locking gets a little bit complicated
- 31:22with deadlock detection and um and and
- 31:24locks being held open for too long um
- 31:27but we use use a type of optimistic
- 31:28concurrency control called multiversion
- 31:30concurrency control and you might not be
- 31:32familiar you might be familiar but I'll
- 31:33explain what these are we use fairly
- 31:35standard techniques so we store the data
- 31:38in such a way in the database in my SQL
- 31:41for example that it's very efficient to
- 31:43read a snapshot of the database at a
- 31:45given point in time right so so it's
- 31:47basically log structured data we can say
- 31:49as of this timestamp give me a snapshot
- 31:51and for every query we run that query on
- 31:54a snapshot and at the at the end we try
- 31:57to commit if that snapshot's valid so
- 31:59I'll give you a bit more detail
- 32:01here I think we do something relatively
- 32:04similar to databases like Foundation DB
- 32:06maybe 4 DB it's more of a kind of a
- 32:08timestamp Oracle kind of model here
- 32:10right so um you have this execution ma
- 32:14machine sitting there and it gets a
- 32:15request to run a query and so it picks a
- 32:19time stamp asks an oracle for a time
- 32:21stamp time stamp T and then it goes off
- 32:23and runs it runs the entire transaction
- 32:25as of timestamp t uh and it records and
- 32:29remembers all the read ranges all the
- 32:31data ranges it reads and then it buffers
- 32:33up all the rights he wants to write back
- 32:34to the database right and then you get
- 32:37to the end of the transaction you have
- 32:38to decide is it safe to
- 32:40commit so we have a relatively efficient
- 32:43structure that can um Can can see if um
- 32:47any changes have happened between two
- 32:49timestamps so at the end of the
- 32:50transaction the latest time stamps time
- 32:52stamp n we see if any rights have come
- 32:56in that conflict with any of our reads
- 32:58if they are that's an optimistic and
- 33:00currency control conflict we retry the
- 33:02transaction otherwise we just give the
- 33:04transaction the latest time stamp and we
- 33:06commit it right this might seem a little
- 33:08bit complicated if you're new to this
- 33:09but it's a it's a pretty standard
- 33:11technique um you know I think often
- 33:13times you know there's these two ways of
- 33:15going down the path of distributed
- 33:16databases there's like two-phase commit
- 33:17style models and there's like timestamp
- 33:19Oracle kind of models and this is more
- 33:21the foundation DB way of doing things
- 33:28all
- 33:29right I'm GNA talk about the query
- 33:31language briefly but I think the query
- 33:32language is not the most interesting
- 33:34part there's a quick question on that
- 33:35this is super interesting do you have
- 33:38other transaction models too like this
- 33:40is not going to get you full
- 33:41serializability but maybe that's not the
- 33:43goal your applications you're targeting
- 33:45may not need that for example you have
- 33:47to read whatever you read earlier to get
- 33:50that uh uh component so uh are you
- 33:54targeting a certain class of
- 33:55applications the banking example
- 33:56probably needs a little bit more right
- 33:58yeah so this this really is serializable
- 34:00because we keep uh read ranges
- 34:02everything is like we don't we don't
- 34:04have like um Phantoms for example like
- 34:06we are keeping real read ranges um but
- 34:09at the end I'm going to talk a bit about
- 34:10serializability because there are
- 34:12situations where you don't want
- 34:13serializability and I and so we'll put
- 34:15that in Hollow them we'll come back to
- 34:16that towards the end because I want to
- 34:17talk about situations where you want
- 34:18want to um um relinquish
- 34:22serializability um thank
- 34:25you yeah yeah um
- 34:29and okay let's let's where are we cool
- 34:30so I'll talk about the query model again
- 34:32can I can I ask one question here so you
- 34:35obviously decided to implement
- 34:37transactions in the V8 containers rather
- 34:39than reuse the transactions that exist
- 34:41in MySQL or
- 34:42postest you could have you could have
- 34:45done that you could have inserted a big
- 34:47begin transaction end transaction and
- 34:49you could have used the backend
- 34:51transactions not to yes so so the way we
- 34:56actually use the database
- 34:58um is not um how to describe it we use
- 35:01MySQL basically as a as a wrer head
- 35:04log um so as a version right ahead log
- 35:07so basically we store um all all the
- 35:10timestamps and and and and the states as
- 35:12as of that timestamp um and then and
- 35:15obviously the indexes as well all the
- 35:17transaction coordination logic is done
- 35:19in um in user space I guess or in our
- 35:22rust codebase so um we really use the
- 35:26database when it comes comes down to it
- 35:28for a durable wrer head log um and the
- 35:31ability to perform queries on the on on
- 35:34on a on a on a um timestamp
- 35:36snapshots um and and this gives us the
- 35:39ability to
- 35:40um well it gives us the ability to to to
- 35:43have our own kind of custom schema like
- 35:45like the the user schema for example is
- 35:47not in the database right everything is
- 35:50done application Level right we can do
- 35:52our own concurrency control um and then
- 35:55really we're using the database as a
- 35:56storage engine
- 35:58and I can talk in more detail about that
- 36:00towards the end um if there if there's
- 36:02more questions so the back end could
- 36:03have been a regular key Value Store it
- 36:05didn't have to be a abely post it could
- 36:09be a key value store that allowed range
- 36:11reads right because we do um we do treat
- 36:14it somewhat like a like a b tree um so
- 36:17so we um our query actually I'll talk
- 36:19about the query model right now our
- 36:21query model is just ranges of berries
- 36:22really um and so um we don't have a
- 36:26query planner and that was a choice we
- 36:28made um it was a choice we made based on
- 36:31our experience running large databases
- 36:33right because in in large organizations
- 36:35you don't really want a query planner in
- 36:37fact in any large installation I've seen
- 36:40you typically Force indexes and and use
- 36:42pretty limited subsets of SQL and you're
- 36:44not doing you know random joins in in
- 36:47production um so our our kind of
- 36:50postulate is that um it's sufficiently
- 36:52ergonomic to just to just describe your
- 36:55queries in a declarative way in in an
- 36:57imperative way rather than having to
- 36:59have a query planner in there that slows
- 37:01things down and can make things
- 37:03non-deterministic and also could lead to
- 37:05unexpected performance degradation um we
- 37:09could have supported SQL in this model
- 37:11and I'll talk about that a little bit
- 37:12later as well right so our query model
- 37:14literally is either get from an ID and
- 37:18internally that gets translated from get
- 37:20from an ID and our query language to
- 37:22underneath get from an ID and a Tim
- 37:24stamp or it's range scan over a table or
- 37:28range scan over an index and then
- 37:31internally that's translated down to
- 37:32range scan over um an index as of a
- 37:36timestamp and so that's all we require
- 37:38from the database right um so this is
- 37:41this is how um this is how reads happen
- 37:44um you know query with index and then
- 37:46you can range over that
- 37:47index you can insert um now everything
- 37:51else in convex is in user space right
- 37:53and this is maybe a maybe a contentious
- 37:56thing so if you want to do a join in
- 37:58convex you just do it in the application
- 38:00Level um now what is a join I think
- 38:03people have this um perception a lot of
- 38:06people who use SQL think joins are
- 38:07somehow magical that somehow the
- 38:09database can do some magic query
- 38:11planning thing that doesn't look like
- 38:13walking over a table and looking up
- 38:14another table typically that's that's
- 38:16that's not happening right so so if you
- 38:19want to do a joint in convex we do that
- 38:20at the application Level in parallel
- 38:22just like this using a parallel map um I
- 38:26think in practice this is maybe a little
- 38:28bit inconvenient right and so we have a
- 38:30helper for joins and we're going to have
- 38:31a new version of um a new version of
- 38:34convex that will have better support for
- 38:36joins just just as a as a concession
- 38:38towards um user ergonomics um but but
- 38:42you know basically the the a core part
- 38:44of convexes um API is to make expensive
- 38:49things look expensive and make cheap
- 38:51things look cheap um so we want joint we
- 38:53want a joins to look like a what a
- 38:55joint's doing um
- 38:58now that said SQL could compile down to
- 39:01this too right we could take a SQL
- 39:03statement obviously we'd want the
- 39:04typescript there but the actual queries
- 39:06that do like db. getet or whatever you
- 39:08could write them in SQL right and we
- 39:10could run a static query planner and
- 39:11compile them down to range scans on the
- 39:13database um and we actually used to have
- 39:16this the very early version of convex
- 39:18had this in there we stripped it out it
- 39:19was getting a bit clunky um it's not
- 39:21type safe it's less expressive um but
- 39:24hey there's a there's a possibility we
- 39:25add this back sometime if people really
- 39:27want to run a SQL query instead of
- 39:29calling with index whatever there's
- 39:31nothing stopping us doing that right all
- 39:33we have to do is translate this down
- 39:34into range scans as of a timestamp and
- 39:37that's what's going on underneath um
- 39:39underneath our
- 39:40system
- 39:42cool
- 39:43um so how does the world look different
- 39:46now that we can run code in the database
- 39:49right so we've kind of we've I'm going
- 39:51to I'm going to claim that we've solved
- 39:53the ergonomics and the isolation
- 39:54problems but what else can we get what
- 39:56other benefits can we get well one is
- 39:58that caching comes for free and when I
- 40:00say caching I don't mean um row caching
- 40:03I mean query
- 40:04caching so caching validation is
- 40:07classically one of the hardest problems
- 40:08in computer science query caching is
- 40:10when you cache the result of a function
- 40:13so you run some complicated thing read a
- 40:15whole bunch of data do some computation
- 40:17um and then you cach the result of that
- 40:19right uh query caching is is is really
- 40:23powerful it's a very efficient way to
- 40:24cach you don't have to run use code over
- 40:25and over again but it's very hard hard
- 40:27to do right manually managing a query
- 40:29cach is quite tricky right but in compex
- 40:32caching comes for free because we know
- 40:34the reads set for every query and we
- 40:36know the right set for every mutation
- 40:39and we have an an efficient intersection
- 40:40mechanism for um for determining whether
- 40:43a mutation intersects with a with a
- 40:46reset so we just cash a query until it's
- 40:49invalided about mutation right um you
- 40:51get that for free right and so you might
- 40:53say well caching is hard right there's a
- 40:55lot of gotchas when it comes to caching
- 40:57and and and you're right there are a lot
- 40:59of gotchas
- 41:00um I'll give you an example of a gotcha
- 41:03with caching just say we we cach a
- 41:05result of a query with a certain version
- 41:07of the source code and then you go and
- 41:09change the source code you go and you
- 41:11know change the story procedure to do
- 41:12something slightly different you'd have
- 41:14to go and invalidate all those previous
- 41:16cach values right and so typically when
- 41:18people are writing caching code partly
- 41:20why they consider it so hard is all the
- 41:22corner cases and gotches you have to
- 41:23deal with right but in convex these
- 41:25problems just go away um automatically
- 41:28for example in the source code example
- 41:31every query depends on its source code
- 41:33every query has to read its source code
- 41:35before it runs so the source code is in
- 41:38the read set for the query and if you
- 41:40upload a new version of that of that
- 41:42source code that's a mutation that
- 41:44invalidates that previous the previous
- 41:46uh read set right and so that is
- 41:48conflicts and invalidates the
- 41:51cache but also if you have caching if
- 41:53you have query caching subscriptions
- 41:55come for free right because if if you
- 41:57know whenever data is is whenever the
- 41:59result of a query changes you can just
- 42:01subscribe to that query you can say give
- 42:03me the latest version of that query
- 42:05whenever the data changes underneath and
- 42:06you can do that very efficiently you
- 42:08don't have to do this kind of spin
- 42:09looping or polling you get this for free
- 42:12so what can you do with subscriptions
- 42:14well you can build a sync protocol right
- 42:16this is kind of the end of polling you
- 42:18don't have to you know you're pushing
- 42:19the interface with the database right
- 42:21out to the application Level right where
- 42:23you can just say bind the results of
- 42:26this query
- 42:27to this
- 42:28component and refresh whenever the data
- 42:31changes so this was the query to this
- 42:33was the react code to render the items
- 42:36list um and as you see here in the left
- 42:40every time the data changes on the on
- 42:42the back end that component just
- 42:43refreshes automatically there's no need
- 42:45for um no need for manual polling logic
- 42:48or manual consistency logic um so if
- 42:50you're familiar with react and
- 42:52functional paradigms that update
- 42:53components whenever data changes this
- 42:55will be very familiar with you it's just
- 42:57react but it react all the way to the
- 43:00database um and again there's a there
- 43:03always gotes right and so you got to
- 43:04kind of um pressure test this to see if
- 43:06it's if it's designed correctly right do
- 43:09we get true endtoend
- 43:12consistency because there's at least two
- 43:14queries here there's a query that gives
- 43:16you the list of items for sale and the
- 43:18query that gives you all the items in
- 43:20your card and it would be pretty
- 43:21unfortunate if these got out of sync if
- 43:24you if you had something in your cart
- 43:26but it wasn't in your or um if it was
- 43:28still in available for sale and a lot of
- 43:30um complex logic and a lot of modern
- 43:33applications deals with State Management
- 43:35how to keep all the stuff in sync with
- 43:37each other right but in convex this
- 43:39problem is done automatically for you
- 43:41because we know what queries have
- 43:43subscribed to and all this stuff is
- 43:45serialized over a websocket so we just
- 43:47send you all the changes in lockstep you
- 43:48get the changes in a Serial order and if
- 43:51two if two um queries are invalidated at
- 43:53the same time those components will
- 43:55Refresh on the browser
- 43:58atomically all right awesome we have
- 44:00real store procedures they're expressive
- 44:03and they're ergonomic to use Larry is
- 44:05happy he was right all along this is a
- 44:07real book by the
- 44:08way U it's the difference between God
- 44:12and Larry Ellison God doesn't think he's
- 44:13Larry Ellison so that's the updated
- 44:16cover but yes are you you've got this
- 44:19book Andy I give it out I used to give
- 44:20it out the students wow okay maybe I
- 44:23should read it maybe I should read it
- 44:26I'll after us keep going okay um and by
- 44:29the way I'm just talking about Larry
- 44:31ellon because there's lots of funny
- 44:32pictures about Larry ellson obviously
- 44:34props to the engineers and and database
- 44:36researchers who designed all this stuff
- 44:37originally right cool so we have store
- 44:40procedures and Andy someone I care about
- 44:42can go back to having a happy fulfilling
- 44:44life except there was two claims in that
- 44:47paper right it's a long paper so you
- 44:49might have missed it the two claims
- 44:50where no one uses store procedures and
- 44:52no one uses
- 44:53serializability and why don't people use
- 44:55serializability cuz there's a belief
- 44:57that serializability doesn't scale and
- 44:59it's not a dumb belief like there's
- 45:00reasons for this belief so what is
- 45:02serializability um probably most people
- 45:05on this call know what serializability
- 45:06is serializability um in some extent is
- 45:09the strongest version of consistency
- 45:12slightly less than linearizability right
- 45:14but serializability means that if you
- 45:16have a serializable database and you
- 45:18interact with it multip people
- 45:19interacting with it at the same time it
- 45:21looks as if to the users everyone's
- 45:24running it one at a time in a Serial
- 45:26order right you don't have to to think
- 45:27about what anyone else is doing you
- 45:28don't need stuff like select for update
- 45:30and all these all these scotches the
- 45:32serializability does have scaling
- 45:34limitations right because if you have
- 45:37queries that touch each other's data in
- 45:39every query they can only happen one at
- 45:41a time and if they can only happen one
- 45:43at a time you can only get as much
- 45:44throughput as the latency it takes to
- 45:47run you know a single um single query
- 45:50now in practice worldd designed apps um
- 45:54try to design them in such a try to be
- 45:56uh application developers try to design
- 45:58their their queries in such ways that
- 45:59they don't conflict with each other
- 46:01right but sometimes you don't need
- 46:04serializability sometimes what you want
- 46:06is asynchrony so asynchron is a very
- 46:08important um property of a of a of a
- 46:11system with strong guarantees right your
- 46:13database gives you serializability if
- 46:16you want it but you can also opt out to
- 46:19just doing things later right so because
- 46:22we control the runtime in convex we just
- 46:24added a schuer to it right so there's
- 46:27also uh a function called ctx.edu Runa
- 46:31this this is actually saying run after
- 46:33zero and what this is saying is once
- 46:35this mutation
- 46:36commits just just run this other
- 46:39function later on you know immediately
- 46:42after um whenever you get a chance it
- 46:43looks some so you can you can also run
- 46:46after one second there's various things
- 46:48you can do with a scheduler but the
- 46:49ability to run scheduling and by this is
- 46:52this is scheduling State that's
- 46:53committed to the database and is
- 46:55transactional right so the the ability
- 46:57to do scheduling allows you to chain
- 47:00together the uh you know these these
- 47:02transactions into workflows that
- 47:03resemble something like temporal you
- 47:05know but have them be transactionally
- 47:07consistent with the
- 47:09database all right I've talked about
- 47:12queries as code right they allow you to
- 47:14express business logic in
- 47:16typescript they're deeply integrated
- 47:18with application developer workflow so
- 47:20app developers can can use them happily
- 47:23they provide you consistency and
- 47:24isolation so you can build correct apps
- 47:27that don't get over complicated and you
- 47:29get catching and subscriptions and sync
- 47:31for free so you don't need to do stuff
- 47:33like polling and and and cash and
- 47:35validation right and something I didn't
- 47:37have time to talk about but is kind of
- 47:40real new research we're doing we're
- 47:42finding that um llms really love writing
- 47:46queries as code right um for a variety
- 47:48of reasons one strong type safety and
- 47:51and and and and good highle building
- 47:53blocks but also you can build good evals
- 47:56very easily so if you're writing queries
- 47:58as code that have strong guarantees that
- 48:00commit automically that have
- 48:02serializability right it's very easy for
- 48:04an LM to attempt to build something and
- 48:06then run tests to see if it works so um
- 48:08we've got some more um um information
- 48:11coming out soon but we're getting very
- 48:12very promising um results uh using llms
- 48:16to to to write convex code in particular
- 48:19um and so you know kind of furthers my
- 48:21belief that you know what's good for the
- 48:22humans is good for the robot uh and vice
- 48:24versa so that's it that's that's that's
- 48:26that's my argument towards queries is
- 48:28code um and I tried to keep this pretty
- 48:30General and not about convex I just
- 48:32think this is a this is a good idea in
- 48:34general all the stuff does exist it's
- 48:37available at convex dodev you can use it
- 48:39today thanks awesome I will slap on
- 48:43behalf of everyone here uh James
- 48:45fantastic uh we have about 10 minutes or
- 48:47so uh for questions so if you have a
- 48:48question for James go ahead and unmute
- 48:50yourself and fire
- 48:52away James this was an excellent talk of
- 48:56so Oracle and snowflake has JavaScript
- 49:00as a stored procedure as a UDF language
- 49:05yeah does that get you a back door to
- 49:07doing this without having to do all the
- 49:09other setup in the database or you still
- 49:12uh need all the other additional service
- 49:15side stuff that you're doing that's a
- 49:16good question now I mean snowflake it's
- 49:19quite different right so snowflake could
- 49:20be all app all app right I mean
- 49:22presumably offering in the all app
- 49:23product um and I'm not familiar with
- 49:27with um I'm not familiar with udfs in
- 49:30Oracle um but I I imagine I mean if
- 49:33they're doing something like this
- 49:34similar to this that's great but I think
- 49:36what makes this powerful is is really
- 49:39putting it in the developer workflow the
- 49:41customers of this uh web developers and
- 49:44application developers the customers
- 49:45aren't dbas you know and so I'd be quite
- 49:49curious as to the ergonomics of that and
- 49:52and and how well that that fits into the
- 49:53platform but you know um in terms of how
- 49:56this is so comvex is open source
- 50:00um at um on Thursday so um in in you
- 50:03know 3 days from now um we we're open
- 50:06sourcing a version that um right now the
- 50:09open source comvex runs on sqlite we're
- 50:11open sourcing the the version that runs
- 50:13on on distributed MySQL and on postgress
- 50:16um and so for us it's um it's
- 50:18advantageous to have this pretty clear
- 50:20separation between the storage layer and
- 50:23and the rest of of of comx and you know
- 50:25I I have built storage before I don't
- 50:27particularly want to build a storage
- 50:28engine again so for us it's nice being
- 50:30able to use an off-the-shelf storage
- 50:33engine good I have a second question
- 50:35that I see else wants to
- 50:38go I I saw a question come through here
- 50:42um why isn't the pl seel something like
- 50:44WM where you could easily Implement
- 50:45compilers from different languages
- 50:46throughout the need for extra va8
- 50:47context perhaps why not even Implement a
- 50:50DB that uses wasm as a store procedure
- 50:52language yeah that's interesting um so
- 50:56yes this um this is um this does support
- 50:58WM right so so V8 will execute wasm code
- 51:01for you um we very occasionally do that
- 51:04um we haven't had tremendous demand from
- 51:08folks wanting language other than
- 51:10typescript um although that this could
- 51:13be selection bias um but I I think the
- 51:16ability um supporting typescript and
- 51:20having like nice typescript types and
- 51:21having a really ergonomic um interface
- 51:24for using typescript has been what
- 51:26people have been happy with so far um
- 51:28but yeah I mean I'd totally be down to
- 51:30support other languages um but I do
- 51:33think you know now you have this
- 51:35question about is V8 the right
- 51:37runtime um because V8 is not as
- 51:41expressive as node for example right so
- 51:43so you can't run necessarily depending
- 51:46on what what um what run time you're
- 51:48running you can't necessarily run all
- 51:49Library functions um in V8 um but the
- 51:53nice thing about V8 is makes it very
- 51:54easier for us to to kind of intercept um
- 51:58calls to the database Etc um but yeah I
- 52:01think pretty key to this is this
- 52:04separation of storage and execution I I
- 52:06I'm I'm not really going to advocate for
- 52:09someone running real code in a database
- 52:12um unless we design the database from
- 52:14the ground up and yes we could do that
- 52:15but you know databases are complicated
- 52:17enough as as they are they they have
- 52:19enough um you most of them have 30 years
- 52:22of baggage about you know managing State
- 52:25and memory Etc um and I don't think
- 52:28throwing like general purpose compute in
- 52:30them is is The Right
- 52:32Move um is is conx handle ordering in
- 52:37the client side H for getting the events
- 52:40and
- 52:41everything yeah so um there is a there
- 52:43is a sync protocol and so my um two
- 52:46co-founders Jamie and sujay um actually
- 52:48wrote the the Dropbox sync protocol so
- 52:50they wrote the desktop client at um at
- 52:53Dropbox and so they've got a lot of
- 52:55experience building syn protocols and so
- 52:57we actually have a a websocket protocol
- 52:59when you're um writing a convex app uh
- 53:02typically there's a single websocket
- 53:04that's sitting there and all um requests
- 53:06and responses go over that websocket um
- 53:08there are kind of out of band ways of
- 53:10sending requests out of band to avoid
- 53:12head of line
- 53:13blocking um but um we order all updates
- 53:18um serially over the websocket so
- 53:20there's no reorderings required locally
- 53:22um but yeah so the the the there's a
- 53:24there's like a a client Side Library
- 53:26which does all this ordering going up to
- 53:28the to the to the back end and on the
- 53:31way back this is for the sync side of
- 53:33convex you can also just use convex like
- 53:35an RPC server you could just call um an
- 53:38HTP end point and talk to the database
- 53:40like that but without any of the and the
- 53:42syn and then you wouldn't get end to-end
- 53:43consistency you'd only get consistency
- 53:45in the
- 53:51database James if I may um I I'll first
- 53:54ask a question and then provide a bit of
- 53:56a interesting context for it uh so uh
- 54:00just as a confirmation as I imagined
- 54:02conx convex is designed for mostly
- 54:07transactional
- 54:09ootp loads and not not for all app like
- 54:13analytical queries and whatnot yeah I
- 54:15have a lot to talk about that but yes
- 54:16the the short answer is yes it's forp
- 54:19but there it's an interesting discussion
- 54:20there uh because fun fact I work at a
- 54:23company which writes all of it business
- 54:25logic in SQL we have very difficult
- 54:28networking algorithms completely encoded
- 54:30in sqo which frequently have like 15
- 54:32joints and uh and in the process of
- 54:36working with it I came come came to the
- 54:38appreciation of how extremely complex uh
- 54:41SQL op query optimization engines are
- 54:44and how much they filter out unnecessary
- 54:47computation and I it came to my mind
- 54:49when I saw your example uh of like
- 54:52everything being in user space where you
- 54:54sort of uh queried over one one table
- 54:57and then joined it with another and you
- 54:58loaded everything into the memory and
- 55:00you sort of join them manually and I can
- 55:03imagine that would be very very slow
- 55:05when you have like a sort of an
- 55:07analytical workload or or a query which
- 55:10like simply does a join over to Big
- 55:13tables do you have do you have plans for
- 55:16adding sort of I would imagine a library
- 55:20that would
- 55:21um hide the complexity of doing a proper
- 55:25join like let's say like all the join
- 55:27algorithms that postgress has I don't
- 55:29know mer join hash join Etc that hides
- 55:31this behind and hides hides this behind
- 55:34the scenes and sort of allows you to
- 55:36write efficient analytical workloads
- 55:39yeah this is a great question so so
- 55:41firstly yes convex is designed for live
- 55:43site ootp workloads where you want your
- 55:45queries to be relatively fast because
- 55:47there's a user in the critical path and
- 55:49I I should have made this more clear but
- 55:51almost everything I'm saying today is
- 55:53really about olp I think ol's quite
- 55:55different because I think the difference
- 55:56is typically with olp you know um what
- 56:00you want and you know how to achieve it
- 56:02generally it's it's relatively simple
- 56:03things like oh you know um add this
- 56:05person to this thing look up this state
- 56:06right whereas olap is is obviously by
- 56:09definition more analytical right you
- 56:11kind of want some kind of answers you
- 56:13don't necessarily know how to fetch them
- 56:15and you and you don't care about tight
- 56:17latency guarantees with olap typically
- 56:19right so olap quite different the
- 56:21planning is really important because
- 56:22there's huge amounts of data getting
- 56:23queried um and you have they come with a
- 56:26complex query plan and so I'm pretty
- 56:28like down with like um with with um
- 56:31declarative query models for olap I
- 56:34think makes a lot of sense um I think
- 56:36olp typically almost everything you're
- 56:38doing is indexed right if you're doing
- 56:40any kind of complex like um query
- 56:43planning in an olp query is probably
- 56:45something going wrong right um so so so
- 56:49there's two things you said there one
- 56:50about joins one about what about if you
- 56:52want to do analytics right so joins yeah
- 56:54there's a few things we could do we
- 56:55could do push down so so right now
- 56:57you're right like we do um read all that
- 57:00St to memory right um now there's
- 57:02there's optimizations we do prefetching
- 57:04we do various things under the covers
- 57:06right we could also do push down where
- 57:08we we statically analyze the code and um
- 57:11and Implement more of it on the data um
- 57:15we don't do that right now to a very
- 57:18extensive degree um and also I think I
- 57:21think we probably should add a join
- 57:22helper to be honest I think you're right
- 57:23yeah but the other thing you touched on
- 57:26is is analytics so I have no ambition to
- 57:30compete with with Snowflake and data
- 57:31bricks right but every now and then in a
- 57:35in a website in a live app you want to
- 57:37do something kind of i' call baby all
- 57:39app right you want to like read over all
- 57:41your users and complete the high score
- 57:42compute the high score or something and
- 57:44it's not easy to do that efficiently um
- 57:46and so even if sometimes a query takes
- 57:4830 seconds so we don't we only we don't
- 57:51allow you to run like a 30 second query
- 57:53right we keep everything very tight so
- 57:54it's so it's it's high performance I
- 57:56think there's room for allowing people
- 57:58to run less efficient queries and um
- 58:01we're exploring two things currently one
- 58:04is keeping these snapshots around a lot
- 58:05longer so right now it's with retention
- 58:07it's a little bit hard for us to keep
- 58:09these snapshots around for a long time
- 58:10at a time stamp um and if we keep them
- 58:13around longer you could just run a a
- 58:14really long query a stale query um the
- 58:18other is we have a prototype of running
- 58:20duck DB on convex where we just um dump
- 58:23all our data in Duck DB now assuming
- 58:25it's it tractable data sizes to be
- 58:27queried by dub um anyway I guess the
- 58:31short answer is uh after a long answer
- 58:33is that you're you're right this is
- 58:35really targeting allp but there is a
- 58:37demand I think for for baby olap right
- 58:40for little analytics and I think we need
- 58:42to add some more features for
- 58:44that and thank you a lot thanks a lot
- 58:46and a very short uh like sort of upper
- 58:49upcoming question uh as a developer of
- 58:53Bas of of Conex do you think it is
- 58:56possible at all to sort of do an a
- 58:59complex analytical join in a language
- 59:02like tab script without encoding your
- 59:04queries farer in like SQL and sending
- 59:06them to the database and getting the
- 59:08results back like does it would it be
- 59:11ever possible to fit that logic into
- 59:14like an imperative language yeah Andy
- 59:16would know the answer better than me but
- 59:17there are people doing this right people
- 59:19are using imperative languages for
- 59:22analytics queries people did that
- 59:24with doop that's dead right there's so
- 59:27many different choices you have to make
- 59:28on on an optimal query plan you do not
- 59:30want your programmer writing this right
- 59:33this is the the classic squel debate
- 59:35from the 1970s right because any
- 59:38assumption you bake in your code about
- 59:40what should be the right join not or or
- 59:41how to push down C you know certain
- 59:44operators if the data evolves and
- 59:46changes you don't want to go back and
- 59:47rewrite all your
- 59:49code so you for baby things sure or like
- 59:53again the you know go- getting the last
- 59:5510 things but like uh anything anything
- 59:59very complicated it's be a bad
- 1:00:04idea okay
- 1:00:07um yes there's there's there's that
- 1:00:09paper yes from I just saw a comment
- 1:00:11about stat I I'm not familiar with his
- 1:00:12work but I do know both Alvin Alvin and
- 1:00:15Sam so I should check out status quo
- 1:00:17Alvin's PG thesis was on um uh basically
- 1:00:20pushing down like hibernate code like if
- 1:00:23if it detects you're doing a join in
- 1:00:25Java it then pushes that down to the DAT
- 1:00:27Ser but it can actually move it back and
- 1:00:28forth based on the load of the
- 1:00:30system other thing I'll say is I'm not
- 1:00:33I'm not particularly like anti- query
- 1:00:35planning right I think uh I I prefer for
- 1:00:39all Top's static query planning at least
- 1:00:42so you know what's going to happen in
- 1:00:43production right but I think um th this
- 1:00:47this this Paradigm is is is compatiable
- 1:00:50with query planning where that makes
- 1:00:52sense right like I said like we've
- 1:00:53chosen to use a very imperative Style of
- 1:00:56querying uh where you're traversing over
- 1:00:58be basic be ranges but but um I think
- 1:01:01the the key Point here is the queries is
- 1:01:03code and there and you could you could
- 1:01:04run it with with some kind of assistance
- 1:01:06by even you know llm based assistance
- 1:01:09you know you're you're in programming
- 1:01:11language territory um then so so um
- 1:01:14there's there's lots you can
- 1:01:16do my last question was more
- 1:01:18clarification like you have this
- 1:01:21middleware that sits in front of the the
- 1:01:23databas server and then the the
- 1:01:24typescript piece that's trying to
- 1:01:26determine the serial ordering of
- 1:01:28transactions is that assuming that the
- 1:01:31database server is like a like a primary
- 1:01:34replica set up so that you would then do
- 1:01:36the right to the primary and let the D
- 1:01:38server replicate it to the replica or is
- 1:01:40that middleware potentially sending it
- 1:01:42out to different nodes or you know
- 1:01:44instances and doing like a you multi-
- 1:01:46primary write up multi home write up
- 1:01:48right yeah so so there's not multi-
- 1:01:49primary right so so you so there's
- 1:01:52there's a few um points of nonscale in
- 1:01:57the system right one is you have to have
- 1:01:58a time stamp Oracle right now it turns
- 1:02:00out you can scale this pretty widely if
- 1:02:03you all all you're doing is picking time
- 1:02:04stamps you you can run a lot of
- 1:02:05transactions per second through that and
- 1:02:07the SEC and then you need um to install
- 1:02:10your rights into a primary right but but
- 1:02:14once you have a a time stamp you are
- 1:02:16able to go and read um that snapshot
- 1:02:18wherever you that snapshot's available
- 1:02:20uh you could even we don't do this but
- 1:02:22you could read that Snapshot from from
- 1:02:23S3 if you needed to you know but um but
- 1:02:25basically
- 1:02:27um when you commit a transaction at the
- 1:02:30end you're just installing right it's
- 1:02:32just taking a a bulk set of data updates
- 1:02:34and you're putting them into the
- 1:02:35database as of a time stamp got it okay
- 1:02:38awesome all right uh let's get i'll for
- 1:02:42James fantastic talk thank you for
- 1:02:43kicking this off was very um a lot of
- 1:02:46people still stuck around to the very
- 1:02:48end that's how you know it's a good talk
- 1:02:49okay this go ahead yeah sorry yeah I say
- 1:02:53I saw those I saw I think Chris had a
- 1:02:55question if people have more questions
- 1:02:56Reach Out feel free to hit me up on um
- 1:02:58on uh X Twitter it's just James a
- 1:03:01cowling um and um or or try it out we
- 1:03:05also have a blog post called how convex
- 1:03:07Works which talks in a little bit more
- 1:03:08detail about the transaction
- 1:03:09coordination protocol
- 1:03:12[Music]
About this transcript
This page contains the full transcript of Larry Ellison was Right (kinda)! TypeScript Stored Procedures for the Modern Age (James Cowling) by CMU Database Group, generated from the public captions YouTube serves with the video. The transcript has 12,155 words across 1,705 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.