Transcript for:
From Functional to Reactive Programming

the session on function from functional to reactive programming so just to give you a context of what this presentation is about it's more of a journey of me learning about reactive programming so when I first got into getting introduced to reactive programming I was as confused as anybody else could be trying to understand what reactive programming really is and as it was struggling with it what made it even more difficult for me is a lot of documents that talked about what it is but quite did not at the time show me how to actually make use of it I'm a programmer at heart I if I cannot see something in code it just doesn't click into my mind so I was struggling quite a bit trying to understand what reactive programming was and what I'm gonna share with you today here in this presentation is more of my journey in terms of learning about reactive programming so I'm not gonna get too technically deep into anything here but more share about my realization of some of these ideas now one of the things the couple of things I've realized over the years in my life and and one of them is it's relatively easy to learn a syntax it's relatively easy to learn a new API or a new library but what is really hard often time is understanding a new paradigm paradigm shifts are really hard for us because it requires us to rethink and change our thought change our way that we approach solving problems so paradigms are very critical and important but in my life I've probably seen about six different paradigm changes over time and when I started programming that was the very first paradigm change for me even to realize that you could actually make machines do something by writing code and writing instruction and of course I was a very young person at the point trying to really write code to make things work and my very first exposure to programming was in procedural languages so that was my first paradigm is writing code in procedural code and then of course along the way learnt about you know procedural and structured programming that the second paradigm shift for me was to learn about object-oriented programming and trying to wrap my head around polymorphism and inheritance and encapsulation and how to really structure the code to use effectively object-oriented programming my third programming paradigm is something I would not wish on an enemy today I had to spend years programming in calm and CORBA this is one thing I'm fearful today if my children will ever find out that I once programmed with calm and and the point really is it was not that it didn't work it actually did and that's what was frustration because it worked but I didn't understand how it worked out why it worked and I remember waking up every single day with this fog in my mind trying to understand how these objects communicate across the wire across to each other and if anybody wants to use the word object Factory I'll be really angry again because I've seen enough of them but for me to actually understand how the remoting actually worked and I had to do this by writing code in C all the way through and when it worked finally I'm like oh I get this and that was a huge paradigm shift for me learning about a distributed computing and common core but my next paradigm shift honestly was even more difficult and and that was trying to program in node yeah I programmed in JavaScript for awhile I I fully understand JavaScript but when I started programming in node that was really hard because most of my experience until that point was writing code in languages like C++ in Java and c-sharp but the write code in node using JavaScript was mind-bending for me because I had to understand asynchrony rather than parallelism it's not that asynchrony was really hard but my trouble again was learning how to write code that is asynchronous not just calling asynchronous code and that was really hard for me and and my next paradigm shift were probably the fifth one was trying to learn functional programming and what really made this worse is all the people doing functional programming kept telling me oh that is so easy and yet I was finding it so difficult to do and every time he sat down to programming functional style it was hurting my head because my mind was wired by them to think about imperative style of programming what has to try to think about in terms of straight transition rather than state mutation was incredibly hard and I have to say that today I probably can solve problems in functional style about 50% of the time the other 50% of the time I still have to struggle to bend my mind to think in functional style a case in point on a flight not long ago I sat in a flight was a cross Atlantic flight and I had about nine hours on my hand I took a problem when I sat down on the flight and I wrote the code in about 15 minutes using imperative style it took me about eight hours more to write the code in functional style I would be struggling with there trying to understand how to write in functional style and at the end of eight hours a sudden light bulb goes in my head and within minutes I had the code working in functional style trust me trying to solve a problem like this is a lot more entertaining and exciting than watching in-flight entertainment so the point really is in this case it can be really hard to understand and conceptualize problems or unfunctional style and my and my most recent a paradigm shift was actually reactive programming trying to understand what reactive programming really means in that regard when I started looking at reactive programming I was asking the question the first question I asked was why in the world do we need yet another programming model and I it took me a while to realize that in our field we come up with new names for what we do every few years for what we already do and get really excited about it and this time around it is called reactive programming and and so as I was struggling with this I came to a realization about what reactive programming was and that's what I want to talk about now in terms of functional programming and reactive programming so to lay the ground for it here is my assessment of reactive programming so to me reactive programming is really what I want to call as functional programming and I'm gonna say plus plus so in other words to me reactive programming is really an extension of functional programming now clearly people may argue and say well can I not write reactive code in a procedural language or even an object-oriented language and the answer to that question is like asking the question can I not write object-oriented code using a procedural language like C and the answer is yes you can but nobody survives that so the the point really is that when you think about reactive programming and I'm gonna make the case why I think reactive programming is really a functional programming plus plus because all the signs of it kind of build up to it and once I could I clicked in my mind it became a lot easier to understand this moving forward because I was able to really write on the knowledge of functional programming I had had by by the time and build on top of that so let's talk about functional programming really quickly before we talk about reactive programming as I'm gonna draw this relationship so what is functional programming now when you talk about functional programming you often hear people talk about a few things together so what is one thing we all ever heard beaten over our head over and over when it comes to functional programming and that is they tell us we should really honor immutability and we are told that we should not mutate variables we should not use shared state so we get used to this idea of immutability quite a bit from early on the second thing that we learn about in functional programming really is about the so called higher-order functions so what are higher-order functions higher-order functions are really typically this is where it leads to I'm gonna call it as a normal function our regular function are just a function and what can you typically do we may pass our object to a function and we do this quite often and we may create object in a function and then finally we may return object from a function so this is what a normal function is that we normally used to write over code and we are used to building applications with these things quite a bit well that takes us to what we want to call as really higher-order functions so what is the higher-order function and higher-order function is where we may pass a function to a function we may create a function within a function or we may return a function from a function and that's what makes a function a higher-order function so higher-order functions have the ability to receive objects our create objects internally or return objects as well so much like how we can pass objects around we can pass functions around as well so as a result in addition to doing object decomposition we can also do functional decomposition also when it comes to building these kinds of applications but unfortunately though if we look at functional programming as using immutability and building with higher-order functions that is not that exciting to me because what it tells us is how we do things but it doesn't quite give us the gist of what we are trying to achieve so to me really functional programming is equal to a deck level it's declarative style so decorative style plus the use of a higher-order functions but the question is why do we care to do this and to get to the bottom of this the real gist in in a sense is its functional composition plus the use of lazy evaluation and to me these are the most important characteristics of functional programming so you are using functional composition and you're using lazy evaluation so what is functional composition functional composition is really a way to do pipelining a modern founder talks about it if you google for this you will find it so if you google for Martin Fowler and then look for the words a collection pipe a collection pipeline a pattern so if you look for a collection pipeline pattern what you're going to look at is a way to conceptualize your problem into the series of pipeline of transformations where the transforms our flows through a pipeline of transformation once you conceptualize this particular pattern you express your problem as a series of transformations rather than trying to think about this as a state mutation so in that regard we can look at the functional composition like so so we can take for example list of integer let's say we'll call it numbers if you will is equal to in this case let's say arrays dart as lists and I'm gonna take a bunch of values let's say these particular values so but once I start with these values what I want to really do is to perform a you know a transformation so what I can do is we can say numbers dart and and then we can say stream where a stream is an internal iterator and we can say filter given an element element mod 2 is equal to 0 so we get all the even numbers from the collection we perform a map operation given an element get me the double of the element in this case we could even say map to integer and then we could eventually say in this case perform a sum operation to get the total of double of all the even numbers so if you look at the code over here this becomes essentially a functional composition and it uses a result of 24 but in this case the collection of numbers has been transformed to a collection of filters of even numbers and a collection of double of even numbers and then we totaled it now when you look at a code like this it makes the question what are we really dealing with in this case so I want to spend a minute on lists if you will were says if you think about stream and this realization helps me quite a bit to rethink about what this pipeline means so what is the list I want you to think of the word bucket for a minute so what is a bucket a bucket is what you used to store liquid maybe you have a very delicate delicate a cloth to wash and you don't want to put into a machine to wash it so you have a bucket of water and you think though the clots in the when the bucket of water let it soak for maybe several minutes and then you take it out on wash it or you could have a bucket of water where you say water for maybe gardening this afternoon you want to take it to the garden so a bucket is like a list is like a bucket you store things into it on the other hand a stream is not a bucket it is a pipeline so you don't use pipeline to store liquid use pipeline to flow liquid through it so there's a very different conceptualization and abstraction a list is a data structure a stream is an abstraction you are dealing with so a bucket of data if you will but this is a pipeline of functions and that is one clear distinction between a list and a stream a stream is a collection of functions a pipeline of functions rather so what is the pipeline of function right here if you take the stream that you have here this pipe line contains the function which is going to apply filter on it and it contains a function which is going to be applying the transformation or doubling of the values so a stream is a collection of a pipeline of functions where a list or a set is a bucket of data or a collection of data so in other words we are conceptualizing this as a series of transformations where we apply the data but through a collection of functions and as a result we are conceptualizing the problem as a series of transformations where we have these functions but more important characteristics in here is this idea of laziness or lazy evaluation so to understand this let's take a slightly different example of this to understand the capability in here so imagine you have a 1 2 3 let's say 5 4 and then I'm going to say over here 6 & 7 & 8 and who knows this could be going for a long a range of data maybe even a million piece of data over here who knows but imagine that's what you have and what you want to do in here is for let's say find the let's say a double find the double of the first number that is greater than three and is even so if you want to find the double of the first number that's greater than three and is even how would you write this code in imperative style of programming well the first thing you would do is result is equal to zero that's what you would start with then what would you do you want to output the result when you finally get it then you would say far in this case element Yi coming from the numbers collection and then you would say if element is greater than 3 and mr2 is equal to 0 then you want to say result is equal to element times 2 but you better do a break if you don't put a break you may get a wrong result or a poor performance or both so you want to make sure that it works properly now this is an imperative style code when we run the code of course it gives a result of 8 but what is the problem with this code well we may not see the problem in the beginning but this often happens when we write code I've got some wonderful testers my tester would walk in at this point and tell me hey venket your code sucks and I always tell my testers don't tell me the obvious don't tell me my cord sucks tell me how it sucks today because every day my code sucks differently and my my testers would tell me they tested my code but with an empty collection and guess what it gave a result of 0 which is wrong because of a collection has no data how could there be the valid result or they tried it with a collection with no value greater than 3 or a collection with no even numbers and it's broken for all of those how do you fix this code now to fix the code obviously you will change this to integer and then what would you do here and you would assign this to null and we know what null is null is a smell and immediately your code begins to smell really bad and bugs creepin then you have to come in here unfortunately and you have to write another piece of code right in here and and what do you do now you would have to come down to the bottom of this code and say if result is not equal to null then you print that result otherwise you may print out something along the lines of you know no value and of course you could run the code this way to write the code it still gives you a eight as a result but if the value doesn't exist you are said no value but you can see the code becoming smelly as time goes on but instead of this instead of writing code like this what you could have done is you could have used the functional style you could have said out for it for example and then you could have said number start stream right now and once you get this dream you could sue not that one so what you could do here is you could say stream and then you could say dart filter and you can say given an element element is greater than three and you can say dart filter one more time give an element element mark two is equal to zero but then you could have said a map and give an element you could have said element up you know times two but then you can do a dart find first over here and when you do a find first you are going to get the value then you can say are else if the value doesn't exist or what do I want to do well if I don't have the value I could simply return a no value and I can return that particular response from here but obviously it's complaining a little bit that it's not able to return a string but you can maybe change that to a given element you could return the element dart to string to return a string of that value and you could execute that particular code to get a result of eight in the end but if you look at this code this becomes a series of transformations as you can see but obviously the question is if the code is beautiful like this what about the performance what if this code this this particular thing had 10,000 elements or a million elements are we going to waste time computing this well this is one of the beautiful things about the functional style of programming where implemented correctly is notice that stream is a stream filter returns a stream and the next filter returns a stream the map returns a stream but the find first returns optional now how does this code really work and that is one of the beauties of the functional style streams are have a very special semantics and so what our streams do a streams have a characteristics I've only seen among my children both streams and my children are fundamentally lazy now I'll share with you a story of what I've noticed in my house my wonderful wife and I've got a bunch of boys at home you know how lazy they are and my wife wonderful wife will tell my son turn off the TV it's like no voice was spoken because a TV is still on and he's still watching it and then she tells him after that she says go put the trash out and no muscles were moved he is still sitting and watching TV then she tells him do your homework no pencils were picked and it is still there and nothing happened and then she says I am calling daddy and immediately everything works so this they could have called it and it depends on the you know they should rename this Methodist called one of the parent and and it depends on the family I always say the parents for you know play the role of a good cop and a bad cop you know what I'm going with this my wife is the good cop I'm the bad cop so the ministry says I'm calling daddy everything gets done right away and this is exactly how streams work as well you go to the stream and say filter it says maybe another time and then you say filter it says maybe later you say map it says maybe after after a while and then you say fine first and it says oh dear let's get it done right now and immediately it acts on it so in other words a stream contains two kinds of methods intermediate operations and terminal operations and here's the easy way to find it look at the return type of all these functions every single one of these lines the return type is a stream those are intermediate but look at the return type of the fine first it's not a stream that's a clue it's a terminal operation so visually you can immediately know what is intermediate and so as a result it's lazy versus what is eternal operation as a result it triggers computation so going back to this code if you will for a minute let's remove all this for just a second and going back to this code let's remove all of these as well just for a minute and in this case I'm gonna take this filter and I'm gonna simply say sample check where the check method I'm gonna write will simply call this as static boolean oh well actually will call this as ease even for a minute and I'm gonna say ease even takes a number and all it's going to do is return over here the return let's say number is mod 2 is equal to 0 so when I execute this code after all well it doesn't have a response right now don't worry about it but I'm gonna put this word done over here and fire up this code and it says done in the very end but if I go back to the ease even function and I output called far and I'm gonna print the value of n so you can see that when I execute this code right now it says call for 5 cold call for 4 and it said done at the very end of this however if I go back to this code and if I were to put a semicolon right there and comment out that code what's gonna happen well now notice the terminal operation is not there but these are all intermediate operations how does the score work I'll tell you how this could work imagine on January the 2nd the boss calls you into the office and the boss tells you you're gonna work on a very important project and it is due on April the 15th and tells you about the project what do you do on January 2nd when you get out of the boss's office you go to your desk and you immediately start working on it right yeah I like that laughter right because you know the rest of the story you will start it promptly on April the 14th isn't it because it's deal with the next morning what is that called efficiency isn't that and this is what really efficient people do most efficient people in this world are fundamentally lazy and of course you know something the boss doesn't know you know you have already decided to quit on March 1st why in the world would you waste your time doing something you're not going to be part of eventually that's exactly what this code does when I execute this code notice it says done but it never evaluated the ease even and the reason for that of course is that it's a lazy evaluation so the laziness is extremely important when it comes to functional style of programming so efficiency and lazy evaluation go hand in hand and I'm gonna say efficiency is attained not by doing things faster but by avoiding things that shouldn't be done in the first place so if you want to really make something faster don't try to speed up your computation but instead try to remove stuff that shouldn't be done in the first place now this is a another way to think about it I live by the mountains and and one of the things I'm always afraid about is what happens when you're hiking and you run into a bear now this happens unfortunately you know hopefully not too frequently but it does happen somebody was hiking and a bear you know comes along the way so and over the time I've learned a trick that is very comforting because somebody wise once told me when you see a bear you don't have to run really fast you just have to run faster than the person behind you and this I think is a really great advice and this is exactly what I look for in case of efficiency is that you don't want to do work really fast you just have to do fast enough and that is something you can do to make this a more efficient well that brings us to reactive programming what in the world is reactive programming why do we really care about it and the answer to that question is reactive programming is not new but the name was given relatively new compared to it has been around for a very long time there's been applications that have been reactive for a quite a number of decades now I'll mention a few as we go along but why care about reactive programming today well one of the reasons is the world we live in is quite different today compared to only 10 to 15 years ago I'm almost nostalgic when I say it I remember waking up in the morning and turning on my coffee part and I can hear the but and then start dialing my modem and I can hear my modem connect and reconnect and the whoosh noise of a modem I can still hear it and of course then I'll call my friends and ask them what baud rate they're getting that morning well those days are gone long gone everyone of us is connected to our internet all the time I've got services on my my mobile devices I'm connected all the time and and every one of us carry multiple devices I'm not talking about smartwatches I'm talking about earrings and no strings and tongue rings all internet enabled and very soon we can have doctors monitor our health and they could call you right now and say hey we notice your blood pressure is really high your heart rate is weird just sit down and be careful you can tell them no I mean listen to this weird guy speaking too fast that's why my blood pressure is increasing but the point is that we are not far from the days when we could be continuously monitored in that case of course what we're dealing with this big data which is high volume data and also high frequency data now of course with all this internet enabled devices we are also living a very different world today 10 to 15 years ago companies develop products for their employees to use and made those employees available to the customers when employees use your product there's a name for them they're called captive users meaning nobody cares what they think about on the other hand today we build applications for real world users to use if a user is not responded the applications not responding well enough you're gonna lose the users so from that point of view reactive programming has a few essential things and mainly it is about responding to the stimuli now why do we need a different model we talked about it there are different problems we live today the world full of electronic devices a big data and demand on the applications performance and of course different user base as well compared to where we are but if you think about it this these applications have been around for a very long time I'll give you two examples of or reactive applications one is very old one of the best examples of a reactive application hands down is Microsoft Excel think about Microsoft Excel for a minute everybody uses that offices use it homes use it and anywhere in between we use it but you modify a cell and before you could blink your eyes all the cells that depend on it change right away and in turn recursively all the cells that depend on those cells change change immediately that's a beautiful example of a very highly reactive application but I'll give you another example and this almost slipped my me without me thinking about it I I travel up almost hundred percent I'm in a random part of the world almost every week and which really brings the question you know how do I communicate with my family if I'm traveling so much well we have electronic family we constantly communicate with each other through electronic documents and that we exchange but I didn't realize this until one morning I was in a remote part of the world and I logged on to Google Docs and I found that my wife was editing the document exactly at the same time and at a back at home and and and it occurred to me immediately I scroll down to the document and I can actually see the exact location where she was typing at that moment so immediately from tens of thousands of miles away I put my cursor exactly where her cursor was and I started changing the text that she was changing and with this I found out the real benefit of reactive programming because you could be thousands of miles away and still find a way to annoy your spouse and this is amazing because for her to tell me get out of my document now was absolutely priceless and then I sat back and talked about it and said how wonderful this is not the annoying part but how wonderful this is that I could be anywhere in the world and collaborate with people who are anywhere in the world you know I often I'm on Google Docs where I am talking to my customers or I'm talking to my conference organizers or I'm talking to my program committee and I'm constantly talking to them and we have 10 people 20 people 30 people jump on a document and we are able to interactively update at exactly the same time but on the other side think about Google Docs itself literally they have millions of people editing documents and getting response right away that's a great example of a reactive application in terms of the stimuli and how we can respond so the question is what does it really mean to create reactive applications if you look at the past you can ask people what kind of applications they were building and and a lot of us still build applications like it and we can summarize that into one acronym called crud we create data we read data we abed data and occasionally we delete data so what does this really mean you have a data in the database and you pass it to a process and then what do you do you take that process the data and you put it back into a database now if you really think about it try explaining this to somebody they ask you what are you do for a living and you say I take data from a database and I put it back where I took it from that's a very boring life isn't it and they look at you and say what are you go to work for doing that and that's kind of what we've been doing for a long time we've been just taking those data and putting it back into the database and then suddenly you look at this and say maybe everything doesn't fit into it what if I'm interested in monitoring tweets what if I'm interested in noticing what people are saying about a particular product or maybe I want to know what people are saying about a particular team while the game is on or whatever that could be you don't read this from a database there's a data flowing through and you're processing it what if a credit card company calls you and says we want you to write or help us to do fraud detection and you say yeah I'm gonna do ml machine learning and I'm gonna implement the process that will tell you if there is a possibility of fraud in the transactions that flow through but what database is the data going to come through well a credit card company is gonna process millions of credit card transactions every instant there is no tomorrow in this case you don't store all this data into a database only to come back and process it as the data is flowing through you want to detect any fraud in that and immediately respond to it and this happens to me quite effectively I traveled so much I would perform a transaction and within minutes my wife would work we could get a call at home saying we don't understand this you're tagged a credit card has been compromised we see a transaction yesterday in in Barcelona we see one the night in Riga the third day we see one in in Lisbon and a fourth day we see in Amsterdam or Australia for that matter and then she usually says yes that's called being married to a crazy person and so don't call again if you see this but this is a way to detect fraudulent probably to say this pattern of movement is not normal what's going on over here maybe we should trigger this so how do you really process that in that regard we are moving towards taking that data from the database the data as it flows through rather and set it to a function and then paint that and send the process through and so we go to a series of transformations a data that arrives at your door is processed and you get a new data which then is processed to get a new data and that flows through if you have been around in the 1980s you could not have survived 1980s without hearing this word called called data flow computing so data flow computing was big deal back in the 80s and even people started building data flow computers one of them was called Manchester Machine and then of course in the late 90s to early 2000 it quieted out nobody talked about it and I used to wonder why it don't anybody talk about it of low computing and then suddenly in the past few years I started hearing people talk about it again now what if I tell you I got a great news for you you can do data flow computing you're probably gonna look at me and say grandpa sit down that's not exciting but I'm gonna sell it to you in a way you will tell me this is the most amazing thing ever what if I tell you you can use Amazon lambdas and you can do serverless programming you're like oh my gosh this is awesome in so many words I told you you can do data flow computing and that is the fun of the field we live in you suddenly hear people talk about service computing and Amazon lambdas and you're scratching your head why are you renaming therefore computing all the way again because the new kids are like this is exciting so service computing is rebranding of what we did in the 80s it's really data flow computing so in that regard I'm gonna say reactive programming is data flow computing and that's what it really is it's a series of transformations where we take the data and transform it using a series of functions so if you ask me what is reactive programming I'm gonna say reactive programming is applying a series of data transformation it is a streaming of data as we transform through it that's what we really are trying to do in in reactive programming so in that regard what is reactive programming really you insist on having it's a streaming of data so think about data flow computing and it's a series of transformation of the data that you're going to provide but obviously the question is if that's what you're doing what does it really mean to create applications with this so in other words there are a few criteria we're gonna apply for this particular purpose now before we talk about it let's talk about object-oriented programming for a minute Opie has four pillars of the paradigm the first is of course abstraction the second of course is encapsulation and then of course we also have inheritance which is the weakest link if you will the most useless of all the four and then finally we have polymorphism which is probably the most important next to encapsulation so given the of course we have these four things that contribute to Opie similarly reactive programming also has four things as its paradigm so what are the four things that it really represents the very first thing is when you build reactive programs you're really aiming for big data a very high volume high frequency of data so that means you are really focused on scale I was talking to a customer of mine and I asked them how many people typically use your application and they said Oh on a given day I asked and say on a given day about hundred people use and I was surprised I said hundred people you're a multi billion dollar company only hundred people use your product and they said yeah you asked on a different day but over a 48-hour period every month over seventy million people use their products how do you go from nobody cares what we do for most of the month and seventy million people use our system in in 48 hours well you cannot possibly do this by keeping a staff locally in your building to manage all these servers and we have learned the lesson really good or the past decade is being elastic so elastic computing simply as the word elastic says we grow when the demand grows we shrink when the demand goes down so we can bring in processors when the demand goes up either based on the demand or the time of anticipation and we can bring it down so you only pay for what you use not other things and that can be really powerful way of using the systems and to scale based on that so elasticity is one important part of it the second thing of course is how do you exchange data between all these different services and the answer to that question is we can't have the data that flows through but how do I get make the data flow through let's talk about a way we should not approach this this is true for reactive programming this is also true for micro services I was in a meeting at a client site we were talking about micro services everything was going well and remember one of the characteristics of micro services are that they are animus you want to be able to build microservices start them and manage them in isolation and independent of other services so this conversation with the client was going really well until suddenly one of the people in the room asked the question how do these micro services talk to the central database and immediately the entire room went quiet like somebody died in the room because that's a such a disastrous way of thinking because if you have a central database you modify the schema on the database and all the micro services come crashing down they're not going to be able to be isolated and independent so I have a slogan I have a philosophy of life and I hope you agree with this philosophy and the philosophy of life is a boat in life and programming we should never share two things and this is something I teach my children from the very beginning both in life and programming we should never share two things the first is a toothbrush and and the second one is databases and we should never share and very rarely I may even share a toothbrush but never a database and the point really is that the minute we shared these things things become really unhygenic and becomes really problematic over time so this is something to think about so how do you really exchange data between these services and the answer to that question is really you want to focus on message driven and you want to be able to drive these through message driven and as a result you could use things like kafka stream or other ways to communicate your messages and these services and and and all these processes or functions are going to do message exchange through message driven architecture the next thing about reactive programming is really to be able to be responsive so what is responsiveness the application we quit using today or the applications that are going to take a very long time to respond we live in the age of instant gratification we want things to work really really fast and we're not going to wait forever for things to spin now a really good sample of responsiveness is infinite scrolling remember the first day you came across infinite scrolling this application was slow and sluggish but that morning you went to the application and snap you got an output and you're like wow that's interesting that work really fast I wonder why and you scroll and your brain is telling you there's no bottom when you scrolled and before you could think about it the bottom gets filled with more data and as you scroll the data comes in that's a really good example of being responsive the goal is not to work really hard and produce slow results but do the minimum to get something to the user so they can be productive with it and then you Americus the cost of doing it over time here's a way to think about it you walk into a store on a Saturday morning the clerk is busy at the store helping somebody what are you gonna do you're gonna walk around for a few minutes look at the ceiling look at the wall look at the floor and you walk away on the other hand you walk into the store and the minute you walk in the clerk makes eye contact with you and says I'll be with you shortly and goes back to help the customer that was there you probably waited for a good 15 minutes at that point why because you felt important even though you are not and that is one of the ways to really think about it is you build loyalty by being responsive and that is something you can really do well the fourth part of this component really is that you want the applications not only to be responsive you want them to be resilient as well so resilience is a very important characteristics the applications we quit using are the applications that are going to fail in a very ungraceful manner so what if we treat failure as a first-class citizen what if we say it's okay to fail and we will promote failure in a graceful way and we can recover from it now I want you to ask the question can there be an organization today that creates a terrible application and users will still use it and the sad answer to that question is yes and that is called the Russian government and I'll tell you why I have I have this one to fall I'm thankful for this luxury I can travel most of this world without having to get a visa I just fly in and they say thanks for coming and they let me in but of course I gotta apply for visa to go to Russia which is great but I applied for the visa and the first page was pretty easy I entered my name address phone number great the second page not too bad the third page is the worst of the page it said list all the countries you have travelled the past ten years are you kidding me and it took me forever to enter all the 200 countries have been to in the past ten years but this is called the curse of a programmer when you're a programmer you think of every other programmer suspiciously so as I was entering the data I am like how well is this gonna end so everything I type in the form I'm literally copying and pasting into notepad I'm saving it I come to the fifth page click the button and it's gone I'm like are you kidding me after spending an hour and a half to two hours entering all this data it's gone what do you do start over and now I'm copying and pasting the notepad into this application and coming all the way to the end and there was the submit button clicking on it is called hope and I did and it failed a second time in the u.s. we have a saying the third time is a charm I wanted to give it a try and yes it did work the third time but I still used it why because I'm the cat abuser to the government at this point and people have applied for visa to the US government have said that is true for the US government - I'm so happy we actually sucked the same but the point really is that any business in the private industry will not survive this you would go to another product and use it so unless you're a captive user you're not gonna listen to this this reminds me of an experience I had I was speaking in a conference and and at the end of my talk a gentleman in the conference came to me and said hey venket you probably don't remember this but I was your student he said back in 1998 I took your distributed object computing course I've been teaching to the University for a good thirty years now and I told him oh my goodness I remember back in the 90s teaching distributed object computing remember I mentioned about Common Core BA so and he said well I have a story to tell you I said oh I love stories tell me the story you have and he said well I was in your course to speed up the computing course and the course had a final project and I was demoing the final project and in the middle of my demo you got it from your seat you walked over quietly to my machine disconnected my internet cable went back and sat in your chair and you said continue and my program crashed because there was no internet connection and I failed the course miserably I said oh my gosh this is not a story this is a tragedy why are you telling me this after all these years and he said well I'm telling you this because there's a happy ending to this as how could there be a happy ending if you have if it starts with the failure of my course he said you sing taught me a very important lesson that day with this experience you taught me not to focus on the happy path but I should program for resilience he went on to say today I work in the industry but I'm a lot better than what I was before thanks to you he said because in my office in my cubicle on the wall I have a picture of evil venket and I look at this picture every day and I never make the mistake again honestly I don't know how this picture looks like but the point is I don't think we need a picture like this we have to program for resilience that's absolutely important so the question is how can we build resilience into the application and the answer to that question is we want to deal with exceptions as a first-class citizen the worst thing we did in our industry is call exceptions as exceptions we should call them normal because stuff happens and you have to deal with that so if it was normal we will deal with it properly exceptions unfortunately or an imperative programming idea and doesn't fit really well into functional programming would that said we talked about all these parts over here about reactive applications but moving a little further how does this really relate to well this is where let's talk about writing a functional interface to do what we really want to do so to get a feel for this let's take this example and write this here as really a functional API that is being reactive for reasons I don't know but let's take a look at this right here so what I'm gonna do here is that we're going to use a generation of a number just to give you an idea about how this works so let's go ahead and start with a flowable in this case and a flowable off let's say integer I'm gonna call it as create and I'm gonna take an ammeter and I'm gonna pass it to an emic function at this point and I'm going to don't worry about the back pressure right now we'll talk about this a little later then I'm gonna create this emit function so what am I going to do in the emit function in the emit function I'm gonna simply say over here starting that's all I'm gonna do now that I've said starting over there I'm gonna come back to this code and say you know let's move forward but when I run the code notice it did not call the starting why well that's because it's lazy it's not gonna do the work until you really use the result you can tempt it you can go to it and say a dart filter and given a piece of data data Mart do is equal to zero and the reactive library says I know what you're trying to do but I'm still lazy so you could even tempt it even further and say a dot map and you can say given data data times two and of course it doesn't budge yet as you can see but then you say dot subscribe this is like the called a dr called mommy function and then you say system dot out print line and now it says we got a deal and you can see that it's a starting because you have connected to it with the terminal operation so you can see the functional composition and you can see the lazy evaluation right in front of you so this is one of the reasons why I say that reactive programming is functional programming plus plus because we start with the function composition and the lazy evaluation and then we build on it so what does that really mean in that case so I'm gonna say reactive programming is equal to functional let's say functional programming because we are talking about composition plus lazy and then plus we are going to say that in this case we're gonna add something more to it and we'll see what we are gonna add in just a few minutes so we are creating this and we're going through this lazy evaluation now what we can do in here is we can then say well in count is equal to zero let's say and at this point what I want to do is say wild count is less than let's say plus plus count is less than ten but I want to now say over here something along the lines of a metro dot on next and I'm gonna send the count over here and then of course I'm gonna do a sleep off let's say about half a second so I'm going to sleep for half a second right in here and and and once I sleep for half a second what am I gonna do at this point well actually let's go ahead and write a sleep function right here and then be done with it all right so that's gonna be sleeping for a half a second and move on this is gonna emit the data well that's great is gonna emit the data from that point of view if it is going to be much like a stream as a functional composition then what is the difference between those two and I want to quickly draw some similarities and distinctions just to understand this so Java stream versus what I want to call as a reactive stream well both are really pipeline and I and and functional composition right so both of them do a functional composition and pipelining so that is common to both of those both of those have lazy evaluation and this one has lazy evaluation also but the similarity ends unfortunately right there so what happens further down in the case of a stream you a data channel a channel only so all you transmit in a Java 8 stream is data and in the case of reactive stream a data channel also will understand what that means in just a minute but the most important question to ask is what about X errors and exceptions what I can answer that question in two English words for Java 8 stream and that is called good luck because if you are caught blows up it causes havoc there is no real good way to deal with that why because exceptions are exceptions are are are really imperative programming ideas so exceptions are imperative programming ideas now what do you do when an exception happens you blow up the stack and you go back to the call where the try block is what does that mean in functional programming you remember the functional pipeline imagine a friend of yours is driving on the freeway and poof they get a flat tire and they call you and say hey dude I've got a flat tire what do I do what's the worst thing you can tell them you tell them you got a flat tire now drive in Reverse it makes no sense isn't it but a good as a good friend unless you're trying to kill them what you would tell is safely exit the freeway and get out so the idea really is how do you deal with this and the way you deal with in reactive stream is a deal with that downstream so you want to deal with the downstream I'll give you a quick example of this I was in Boston to speak at the Java user group had a great time the next morning I had to go to Montreal and and I said to myself I'm international traveler it's just across the border it is not a problem it's a different country I can take this well my flight was at 6:30 in the morning I decide to go to the airport at 4:00 from the hotel well it had really snored the last night though the door was covered with snow I started driving at 4:00 in the morning there's nobody around me and but ten minutes I hears this boom and the next thing you know the car is not moving well I stopped the car take a look at the tire it's completely busted and I'm thinking what do I do and since there's nobody around I pick up to the picked the phone and call the towing service and they said you're a valuable customer to us thanks for calling we'll be with you in two and a half hours that's not gonna help and I said they're thinking what do I do honestly a reactive programming came to my mind and with good courage I turn on the ignition put my foot on the gas pedal and I drove the car all the way to the airport and as I pulled into the parking lot of the rental car company you'd have seen the people over there they were like what in the world is this huge noise and the guy came over and said are you okay what's going on I said oh just a flat tire here's the key by the way and I walked over and this is when you're so happy it's a rental car well a few weeks went by my wife called me and said remember that story you told me I said yes well you got the bill in the mail it's like oh let me sit down what is that she said oh they charge you $85 I can handle that well the point really is you deal with the downstream how do you do this well the way you do this is in the reactive stream the flow vellore the observable have three channels that they provide the first is the data channel through the data channel you get the data so what you saw up here is that you're subscribing to the data channel so when I run this code you can see that it is giving us the four eight and twelve and so on that is data coming through the data Channel well that's great but what's going to happen if things were to go wrong you have an error channel and through the error channel you get the error and there's a complete channel through which you will get a complete message to signal that there's no more data coming in but the way this works is if there is an error you get an error through the error channel but the data Channel will close up so you're not going to get any message through the error channel but what happens in this case is that it reads so what do you do in here instead what you do is treat error as data so you're treating error as data so you're not reading it any differently if something were to go wrong I politely give you a error rather than throwing things are to how uncivilized that would be so how does this work let's understand this in this example so I'm gonna go back to this code as you can see it produces the data for 8 and so on but I'm gonna subscribe to the error channel so enter and I put an arrow and I output error over here so let's say error and then I'm going to then simply display right here the error message and then I put the complete channel and in this case I'm gonna simply say done to display it now when I run the code obviously it just goes through it but what is something were to go wrong so notice what I'll do here I'll come into this code and say if and I'm gonna say count is equal to let's say 7 then what am I gonna do throw new runtime exception so I'm gonna blow up right in the middle let's send a message like we often see in our production something went wrong so this is gonna blow up right in the middle with such a useful message isn't it and as a result what's gonna happen when you run this code so when I fired this up you notice it starts processing this one at a time but when the value it's a seven it immediately says something went wrong but notice the word error over here so your exception was captured the data stream was closed and the error message is delivered to you by hand at this point it threw the error channel similarly of course if you don't have an error then what's going to happen really is it is going to go all the way through and then you can say emitter dark aren't complete and you can send a complete signal and notice at the very end of this you will see the message done being printed and that is coming from the complete channel as you can see right in there but there's one small problem with this code still and the problem really in this case is what's going to happen if when I receive this I'm gonna take this one and say a sample let's say in this case sample and I call a process function so what in the world is the process function well I'm gonna write the process function receives the data will call this data but I'm going to unfortunately here output that data but I'm gonna say sleep off one second now notice there's a half a second delay in sending the message here but a one-second delay in receiving it which is really unfortunate because it doesn't go really that fast as you can see it's much slower so what you can do to get around this problem is to make it asynchronous so what you do here is you can say in this case observe on and I'm going to say there's going to be schedulers Dart let's call it IO not going into the detail right now and I'm going to specify that is going to be on the schedulers and I fire this up this time but notice in this case it is all actually to see this a little bit better let's do one more thing I go to the Emmett function and I'm going to simply say output would say emitting over here and then display the count over here so when I run this code you can see it's anything much faster than I'm processing it but how is this working because I applied this idea of back pressure so what is a back pressure a back pressure is where you let the producer run in a very different pace as a consumer or a subscriber and one doesn't control the other and of course in this case you can use different strategies you can say drop if it makes sense and when you run the code this time notice that one and two but you process you know what to keep this a little easier to see if you don't mind let me just do data times one that way and I'll remove the filter so we can see the processing in a lot better way so notice one and two but three and four was not processed we have five and six was processed but seven and eight were skipped and you see nine and the reason for that is we did a drop strategy compared to if we say buffer you can see that it's gonna buffer it up and all the values one two and three and four all of them are processed and air until the very end of course in this case I want to keep it a little alive so let's go ahead and do that for a second to see that working and you will see the buffering come through and it's not going to lose any data at this point and it's buffering through them and displaying the results right after so to summarize what we talked about here reactive programming is functional programming plus the use of back pressure so as a result and and and plus the use of three channels of of communication so you are building up around this so so essentially what you're doing is you're taking this idea the central idea of functional programming and you're building abstractions on top of it and you're bringing in the back pressure into it and the three channels of communication into it and on top of that you could be adding some flavors to this for example you can say either synchronous potentially or you could be using asynchronous as well so you can decide which way to go based on your problem needs so you could be doing synchronous evaluations or potentially an asynchronous evaluation so the reactive programming ideas start with the ideas of functional programming and then you build on it with back pressure the three channels of communication and the synchronous versus asynchronous communication as well and that's basically what we saw here is as a bridge between these in terms of how we evolved so to summarize what we talked about the ideas of reactive programming are very fundamental to creating a highly scalable and efficient and performing systems but these ideas are not just isolated they nicely built on the ideas we're already familiar with by introducing a few concepts and merging them together and and that's basically the my journey in learning this in trying to figure out what this means and putting it in that context or helped me really understand how I