with Kafka this is a I think a counterintuitive idea it's a thing that we all think is just not possible but it is possible this is an argument originally made by this guy it's Martin klepon most of the interesting ideas in event streaming turn out at one point are allowed to have their source at least approximately in Martin I see if you build the graph of where these ideas come from there's a lot of edges that connect to him so I was like to recommend that you follow him if you're interested in more of this
kind of thing but it would be worthwhile to step back real quick and remind ourselves I think we all probably know what those letters stand for for these these properties of a database but I still would like to talk through them real quickly atomicity consistency isolation and durability what are these and in particular why are they a problem in the garden-variety distributed systems that we build these days so durability let's just talk about each one now what's interesting about durability I don't think it's particularly challenging it's hard to realize but like anything that source data
these days probably has a decent durability story but what's interesting is if you look at like over my lifetime the databases that were that that large businesses were using you know computers are very expensive in the early 70s and you had to be an airline or a giant bank or you know something of that scale to even want one you know made to some degree but the big computers that were running businesses like that their ability meant that the database transaction law got written the tape and then and I guess to be fair that's that's
maybe a few years before I was born and you if something went down you just bring that transaction log back into memory and you'd have your state back well most of us when we came of age most people who are working in technology and you know who's whose memories are influenced by recent decades when you think of their ability you think of this right and discs don't even look like this anymore most most of your databases are probably stored on SSDs but you think if their ability you think it gets written to disk a disk
will forever be this picture in your mind really as you look at the history of durability these days if you were if you're a piece of infrastructure and you don't have a replication story it's almost like you're not really durable you know if you're just orient data on one disk what does they even mean so their ability isn't especially hard to think about but I just like pointing out it's interesting that it's it's been a moving target over time and our expectations for what constitutes durability have gotten I think strimm more stringent over time alright
let's talk about Adam isset II now the kind of textbook thing that comes to mind when we're talking about Adam in a database is something like this it's a database transaction and the really funny thing is that again the textbook example is often an ATM you know here's an automated teller machine and you know say we're transferring money we're adding $100 to Tim's account and we're taking it from Gwen's account well that has to take place in as a single atomic unit both of those have to work or not work otherwise we'll be creating money
out of nothing and only central bank's can do that so that's bad now what's funny about that is if you if you talk to anybody who's worked in fin tech I never have but I've certainly known people who have they will like actually laugh when you suggest that every every database mutation in FinTech is somehow atomic they've been building distributed systems for a long time and often sacrifice atomicity anyway that's an aside this is what we think of what's on the screen here when we think of atomicity there's another problem though that's emerging in systems
that a lot of us are building that are that are not especially large scale you know you don't have to be Amazon to want this but you've got an application and an events impinge on that application and you perform some computation on that event and you want to remember what you did so what do you do well you write it to a relational database because you're civilized person and that's what you do with the results of your computation but you also look you have a graph model and so you write it to some graph database
because you've got like product recommendations or fraud detection or something that you want to do and you need to be able search those and the database is very gonna search so you you write it to elastic also this is not weird right you have these three different ways of reading data and you need to get the data your results into each one but doing those multiple writes it's kind of bad because one of these things is gonna go away some number of them is gonna go away and now that poor application of yours has to
deal with the fact that it's done multiple rights and what does it do right does it does it roll back the other two rights does it have to squirrel away that right somewhere in a log somewhere and remember to write it back to elastic later on it's like that's a lot of responsibility for the application and that responsibility can be managed by some sort of framework or layer or something like that but again do you want application code doing that ideally you don't that's that's an infrastructure responsibility and so this atom is any problem I
think is more interesting in terms of the systems that we are building these days and by the way I'm just gonna make problems for like five minutes and then I'll solve them with Kafka so everything should be should be very bad and getting worse all right isolation again I in like relational databases is is actually super complicated the again textbook account of isolation we speak of serializable isolation right like we want multiple writers on the database we want it to look like all of those writes happened in one thread in one particular order we could
always play them back in that order not everybody even uses serializable isolation level the details of how databases actually behave in terms of transactional isolation or vendor specific and complex but I want to think again of the garden-variety distributed systems that a lot of us find ourselves building where we'll have a single update and place data store in the middle which could be a relational database right and multiple nodes of our application might be trying to issue writes do reads and issue writes on a database like imagine the application is trying to create a new
user account and there could be somebody coming in from the mobile interface there could be somebody coming in through a browser many many users coming in whatever they're people just trying to do things so process one will ask at some point in time if there's an account for Kim and the answer is no but process two could ask essentially at the same time those will get serialized of course but I'll ask if there's a Tim and process to doesn't think there's attempt so the strawman version of this problem is that now both of those processes
think it's okay to do that right and create that Tim account of course that's bad because like the Highlander there can be only one so that and nobody would actually build this like everybody is aware enough of this problem that you you wouldn't probably stumble into this bug but you need an architectural way around this when you've got multiple uncoordinated instances of your process talking to a single update in place datastore so we need to do something there now consistency again just working through the letters of acid here consistency I'm going to define as when
we mutate the system that there are certain invariants that the system that will be enforced for example we can only have one of each username usernames are unique or account balances have to be greater than zero so when we're issuing a right not I don't want you to think about the database when you think about the system as a whole when you issue that right the the system carries that out in such a way that it enforces the invariance so we never leave the system in a quote/unquote inconsistent state meaning there are two Tim's or
you know ten has negative money all right now because this is the 20 minute version I'm gonna mostly assume that you know about Kafka there's a there's a good hour-long in a 90 minute version of this talk where there's a whole Kafka intro in it but we're not gonna talk about that now I want to talk about how you can use Kafka to realize these properties in a system at scale first of all durability is to get trivial Kafka has a mechanism what Kafka is is a distributed log right some people think of it as
a as a message queue where the data is ephemeral in some way but writing to a Kafka topic writes to a log on disk and that data persists on disk until it ages out and that that retention period is completely configurable even to infinity so topics can be infinite storage of data in a system can be the system of record not only is each one of those partitions is it a log on disk but they're also replicated like I said before you know you need a replication story if you're gonna store data and Kafka does
that it writes data to disk and it doesn't erase the data when you read it it erases the data when you said that the data is too old and if you're extremely patient and you want that to be infinity that can be infinity and if you want that to be replicated to ten different servers because you have nothing but money or you're in quarantine and you're bored and you want to make operations difficult you know you can make that replication factor ten most people use three but the point is Kafka accomplishes durability all right Adam
is City let's talk about Panama City now remember but even if you don't know anything about Kafka you probably know that has something do with messaging and the core data structure in Kafka is a log Kafka calls them topics just like enterprise message queues called their things topics usually but a topic is a log so when I write to a topic that message which is a key value pair it gets written to the log and stays there and they stay in order as well so I have this ordered log of messages there's lots more nuance
to how all that works Tower replication works to the details of ordering and partitioning and how they interact see me after class so for now all you need to know is that a Kafka topic is a lock recall this problem this application that we had put together we're getting events and we want to write that data to multiple places now it's not bad to want that it's bad to do it if you're if you're doing multiple rites with the results of a computation you probably like Airy just if you do that a lot like put
up rubber band around your wrist or something like that snap it every time you find yourself thinking it's a good idea to to write more than once you don't want to it's fine for there to be multiple ways to read your data like you see here that is 100% normal and I think getting more normal as time goes on but having that application do those rights is very bad so what we're gonna do is we're gonna put a Kafka topic in the way so remember I said a Kafka topic is a log so those events
arrive at the application the application does its computation over them and instead of writing to elastic or graph database or relational database or any of those things steadily at the application having any idea what those sinks are what those readers are the data all it does is produce the results to a Kafka topic that's what it does that operation is atomic so gets an event does its work writes the result to its its output topic those little arrows down there that go from that topic to elastic to the graph database into the relational database those
are part of a framework called Kafka connect so those are basically looking for new messages in that topic and writing them out to those sinks and that's that's boilerplate framework code right like you don't you don't want to find yourself writing the code that consumes from a Kafka topic and then talks to the elastic API to write it to an elastic index that's that's you know it's probably been written and like I know everybody thinks their code is awesome but your version of that is probably not materially better than some of the version of it
that works so Connect is this framework where there are these little pluggable jars floating around the ecosystem that you could drop into the connect server and post a piece of a little blob of JSON at connect and it says oh okay you want me to read from this topic and write to elastic that's here you want me to read from this topic and write to this relational database that's there and it does that so we keep getting those results and connect keeps up until something dies right when one of those goes down remember our application
is still atomically producing its results to that topic and it keeps going and and the connectors that are up stay up now from an observer ability standpoint like I hope somebody's phone is wiggling to tell them that elastic is down customers can't search you know it's bad that this is happening but from an application perspective the application doesn't have to think about atomicity because you know it's bad elastic goes down oops okay we brought the cluster back up and now connect knows oh the last one I read was that fourth message in I'll just get
caught up and get search up-to-date now again every operational consideration that could possibly make that harder than the slides make it sound those are still there right I mean there could be a lot of traffic that could have been some sort of traffic spike that caused elastic to go down what elastic comes back up it has to catch up and that can be hard so you know it doesn't make distributed systems a good life choice it just makes this particular transactional property atomicity pretty easy to realize from the standpoint of the application all right let's
talk about isolation remember this was our problem when we have multiple processes reading and writing a single update in place mutable data store and there's nothing again you are not a strange person building bizarrely large-scale systems if this problem presents itself but you also don't want multiple processes trying to mutate the same data at once so what are we going to do don't act surprised we're gonna put the Kafka topic in there so what we do now instead of giving the responsibility to those processes of creating new accounts I'm gonna go look to see if
something exists and I'll remember what I saw and then I'll create it you know that's terrible that's too much to ask of those processes so now what we say is we can look processes you're out there you want to make users let me know when you want a user that's all you do okay the process submit the request or makes a claim on a username I want there to be a Tim and some metadata comes along with it right it's important that that process ID comes along their process to the same thing I am process
to I would like there to be a Tim well that's great and now those have been submitted and notice the kind of asynchronous feel that that starts to emerge here from the users perspective I'm sitting here in my phone and I've typed in my user name and wrangled with my password manager to get it to create a password on mobile which Oh sucks and I tap on create account and I'm quite synchronous like my attention span is low but I'm it's it's it's high enough that I will continue looking at my phone until the spinny
thing goes away and the Box turns green or box turns red or whatever happens right I'm trying to create an account but on the back end here this is asynchronous I tap that button it produced a message and in one sense the application is done right now with with that that end of the deal so let's look at what's going on in the back end and let's look at how on my phone I could turn that box red or green because that still needs to happen okay that database may be a relational database it may
not be that may be part of Kafka's stream processing framework a Kafka streams application that could be what's called a K table in Kafka streams so if you know anything about streams or if you want to look some stuff up about that that's a worthwhile topic for for quarantine research if you do find yourself with extra time on your hands it's very good thing to know about but you know what just think of it as a database so there is one process that's consuming from this one topic of usernames if you know about Kafka then
this topic absolutely can be partitioned it just has to be partitioned by username and if you don't know about Kafka and you don't know what partitions are in the Kafka Scents don't worry just think of this now is this log of of username requests that's in order and that database over there holds the current users who's currently who are currently there and so now this application that's reading think of this little database and table over here this is this is a stream processor reads from that topic and for simplicity's sake you can imagine this being
just one thread reading one log and it gets a user out and that's Tim and Tim exists that we create Tim there's a Tim well let's read the next request cuz that's what we do it's Tim ah well there's already a Tim I'm sorry I'm just gonna drop you on the floor there is no Tim and so it just just drops it I can't create Tim twice so it's enforcing that invariant that we discussed earlier now the process is still need to know what to do so this stream processing program over on the right the
drum with the little list of users there also is going to expose a rest endpoint so those processes can ask hey I'm processed to does my Tim exist did you make the process - Tim likewise process one will say is there a Tim and I do mean my Tim my process one Tim so that metadata it's not I don't show it on the slide here but that metadata came along with the users we remembered we actually created process ones Tim not process twos Tim and so process two we'll see there's a Tim but it's not
your Tim process one we'll see there's a Tim and it's your Tim so they basically do that polling while the little spinny thing is happening on the phone and I'm waiting to get I'm synchronously waiting to get that result this asynchronous process has happened on the back end and has enforced isolation because remember what we wanted the best kind of isolation to look like is like our requests happened in a what single defined order by a single thread processed by a single threads they always happen in the same order and that's exactly what we got
so we never actually let the processes write to the database they submitted requests we had a way of serializing those requests and we managed that writing in one thread in that stream processor now again this is this is a lunchtime thing we're doing so there are questions that I won't explore like you know what if you have 60 million users and you can't do that in one thread it is okay there are ways to partition them and so that each stream of user IDs is happening being interpreted by one thread in one node in your
cluster of username stream processing machines this all scales up horizontally maintaining the same semantics but it's just it's easier to think about as a small scale when you're first getting into your head and consistency again just kind of falls out from the invariants enforced by our street processors like we saw the unique unique username constraint was it forced there all right is that is the quick version of how to get acid transactional semantics with kafka durability that's easy consistency kind of falls out of our isolation solution isolation and Adam Missa tee you get those by
moving rights through topics one possible there are a number of other really neat properties that emerge when you have a bunch of services interacting by exchanging immutable events if you look for the topic the database inside out if you google that phrase there's some great blog posts and I think the small book could probably some talks by me online that you'll find and if you want to know more about Kafka there is one place that you ought to go and that is developer confluent that IO excellent thank you Jim last time we attempted our applause
very hard to do it everyone muted but here we can we can try all right that was neat thank you and even if you unmute zoom is designed to pick one voice and play it so like if you ever try to like sing happy birthday to somebody else it's terrible anyway great thank you Tim yes I highly recommend those videos like they've been important for my architectural thinking like even if you're not using Kafka gives you just a much better understanding of distributed systems and how things should go together how you should be building things
out cool good and do you have a podcast right Jim I do have a podcast that's also linked from developer confluent thought I oh that's called it's called well alright I was so impressed with myself when I named the podcast it's called streaming audio right cuz it's about events dreaming and Kafka and stuff like that haha go ahead google that if you google confluent podcast or just go to developer about consulate that IO and set that as your browser home so that opens up every time we give scrolled down for the podcast whoo yes the
James or Kurt episode just came out today that was freaking fascinating so fun to talk to him he's a very smart guy and there was if you scroll down on the web Gabe well I shamelessly plug things yeah I can't back on a few weeks ago and that was even better it just I don't know if you know Kent but I mean you could just talk to him about stuff and smart stuff comes out of his mouth and like you're better off awesome I saw his explore expand extract talk at some some startup in New
York that was really good so but I bet that was a great conversation nice thank you very much I think I think we still don't have glitchy on that correct so okay so we'll dive into our just general business here and first order of business is we have intelligent licenses to give away so