Rubyist, writer, former teacher. Human who loves creating things for other humans.View the profile
I am a collaborative team player experienced with systems thinking and design on fast-paced, complex projects. I enjoy reconciling the nitty-gritty with the big picture.In my former career as an architectural designer, I was known for creating reusable software-based tools, typically using Excel and Revit plug-ins. I started learning to program in order to improve these tools. This initial tinkering developed into a passion; I was spending evenings and weekends coding. Eventually, I attended Epicodus in order to turn my programming hobby into a career.View the profile
About the talk
RailsConf 2019 - Inside Rails: The Lifecycle of a Request by Yehuda Katz, Vaidehi Joshi, Godfrey Chan, & Krystan HuffMenne
This is a sponsored talk by Skylight.
This breathtaking documentary series combines rare action, unimaginable scale, impossible locations and intimate moments captured from the depths of Rails' deepest internals. Together we will follow the lives of Rails' best loved, wildest and most elusive components. From the towering peaks of Rack to the lush green of Action Dispatch and the dry-sculpted crescents of Action Controller, our world is truly spectacular. Join the Skylight team on this incredible Journey to unearth the lifecycle of a Rails request.
Well, I guess that didn't still weird stuff a little bit more people in there. So I'm just checking if everyone in the right room because in case you're not sure this is this is a sponsor session to me and slapped like we we work for a company that sponsor to conference. That's why we didn't call for papers process. So I'll talk might not be very good thing. I've like it's always very flattering and fly really appreciate it when people will come out and support us Lee it work for a company called Skylight we make Rails performance monitoring.
Well call Skylight the invite at Escalade. IO and if you can remember that you are all there. There is a promo code that you can use that give you fifty bucks off, which is pretty good deal. And if you can't remember that your own no problem. We have a booth at the exhibit hall tomorrow and the day after tomorrow so you can just come talk to us at the booth and we'll hook you up with the discount in everything. In case you haven't used Skylight before this water looks like but again with a booth, so I'm not going to waste your time. We just going to dive
right into I'll talk. Okay, so who here has written a real zap before? Just checking cool. So most of the room here are Ruby programmers rails developer, right? So like and probably within some similar to this at some point where you have the controller and you have like a index method or you show a method or whatever and inside the method is just Ruby right? Like we know how we works. We know like maybe you'll put post-op fine there. But like we can understand all that pulls is a class fine as a class method soap. Find calls by method.
Have you ever wondered? How did we get here in the first place? Like when you type Skylight. IO. / hello in the browser. How did it how did it make all the way to your index method to call step and how old does a web request get turned into something inside Ruby? That's what this talk is going to be about and to tell you more about that we have sex. So let's say you are a meeting somebody for lunch. Meet at pastini probably works for your co-workers who go there a lot but it probably
doesn't work for your out-of-town friend who's not familiar with the area. Instead you can give them the street address. That way they can give it to their cab driver or just ask anyone for directions. Computers work much the same way Skylight. IO is easier for us to remember but it doesn't help your computer find the server. When you type skylight on your browser, the first order of business is to connect to the server. And to figure out how to get there. We need to translate that name into an address for computer networks. This is the IP address and it looks
like this you probably come across it at some point. With this kind of address computers can navigate the interconnected network of the internet and find their way to their destinations. DNS which stands for domain name system is what helps our computers translate the domain name into the IP address that they used to find the correct server. You can find out for yourself with a command line tool called dig on your computer. The output looks a little bit intimidating. But the main thing to look at here, is that a translated in Skylight. IO in
234-194-8473. The DNS is a registry of domain names maps to IP addresses when you buy your own a domain you are in charge of setting up and maintaining this mapping. Otherwise, your customers can't find you. Okay. Once we have the IP address of the server or browser can connect to it the way the browser connect to the servers actually pretty interesting. You can think of opening a connection between the two like picking up a phone and dialing someone's number. In fact, why don't we driving? There's another program on your
computer called telnet that lets you open a raw connection to any server. Here we are connecting to the server. We found earlier at Port 80, which is the default HTTP port. Once we have connected we have to say something but what we say. Hi. Unfortunately, that doesn't get you very far. What do you do when you pick up the phone and it sounds like a Spam call? You hang up? So let's try this again, but this time doing a properly. The browsers and the servers have to agree on a language for speaking to each other so that they can understand what one another is asking
for this is where HTTP comes in stands for hypertext transfer protocol, which is the language of both browsers and web servers can understand. Make the request for Skylight. Hello, here is the simplest request that we can make. It specifies that doesn't get request for the past. / hello using HTTP protocol version 1.1. And as for the host Skylight that I know. a simple response from the server looks like that's it specifies that the request it was successful is a bunch of Header information and finally hello world the text. We rendered from the controller.
HTTP is a plain text protocol as opposed to a binary protocol which makes it easy for humans to learn understand and debug provide the structure for the browser to ask for web pages and assets cement forms handle cashing compression another mundane details. And by the way, here's a side note just like your phone line the connection between the browser in the server is unencrypted request go through a lot of places to get to the other side and the conference Wi-Fi. The convention center is router is our internet provider the servers hosting company and many other intermediate networks in
between that help for the request along to the right place. This means a lot of parties along the way have the opportunity to eavesdrop in on the conversation, but maybe you don't want others to know what you're what conversation you're having. No problem. You can just encrypt the contents of the conversation. It will still pass through all of the same parties and they would still be able to see that you were sending each other messages, but those messages won't make sense to them because only your browser and the server have the keys to decrypt them. This is known as HTTP
to ask makes it secure. But it's not a different protocol from HTTP. You're still speaking the same thing text protocol that we saw earlier. But before the browser sends the message out and Crips it and before the server interprets the message it decrypted. The encryption decryption is done for using a secret key that both the browser on the server have agreed upon and no one else knows about but how did the browser and server pick what keys to use for encryption and decryption without giving them away while all the other parties are listening in Well, that's a topic for another
shock. Okay, let me know your browser has successfully connected to the server and asked for a specific webpage. How did it generate the response to tell you about that? Let's Push by Tatiana stack. Thank you back. All right. So that is a good question. How did it generate this response will first of all we have to figure out what kind of server this is. Well, it's a web server, which really just means that it speaks HTTP as we saw earlier. Super quite a few
different examples of web servers here if we have Apache and genetics passenger light D unicorn puma and webrick. Some of them are written in Ruby what others are written in feet and their job is really just to parse which just means understand what the request is and they have to make a decision about how to service that request request like serving static assets. You can easily configure a web server to do that for you. So for example, let's say that we wanted to tell the web server that whenever a browser request anything and assets then the web server should try
to find that file in my apps public assets folder and if it exists, it should serve that with compression. Otherwise, it should return a 404 not found page. Depending on which web server you're using there are specific configuration in Texas and languages that you might want to rely upon. For example, if you're using nginx, you probably would write something that looks like this config file. For more complicated things. However, it gets trickier. For example, we might want to tell her web server Hey whenever a browser goes
communicate with rail? Well in Ruby there a lot of different ways to communicate this kind of information. So rails could potentially register a block with a server or the server could call a method on Rails and the server could pass that request information along as request arguments environment variables and maybe even a global variable and if we did end up going down this path what kind of object would these look like? And then of course, there's another question answer on the flip side, which is how rails would communicate back to the web
server. Ultimately, all of these options actually work and at the end of the day what's more important is not which option you pick but rather that everyone is agreeing on the same option. It's important that we all agree to the same conventions. And that is why Rock was born to present a unified API for web servers to communicate with Ruby web Frameworks and vice versa by implementing the rack protocol all web servers only. All they have to do really is just Implement a single adapter for Ruby and
anyone Ruby framework that conforms to this convention will just work seamlessly with these web servers. The rack is just a simple Ruby protocol and all that means it's a convention. And rest of the few different things the first the web server needs to tell the web framework. Hey to handle and by the way, you know, and then the framework needs to tell the server. It's cool. I handled it. Oh, and also here's your result your status code your headers the body. In order to remain lightweight lightweight and framework agnostic
Rock pick the simplest possible way to do this in Ruby. It does a few different things like it notifies the web framework using a method call and it communicates the details in the form of method arguments the web framework communicates back by responding with some sort of return value from that method call. Encode it looks a little bit like this. We see that the web server prepares a half which conveniently is called the ends Hash Hash contains all the headers and a path. So for example will see that request method contains the HTTP verb for example, get or post
half info as it's named contains the path request and HTTP underscore star has the corresponding header value. An important thing here is that your app has to implement a call method. And again we see convention at work here because the server is going to expect that call method to exist. Why because the server invokes the apps call method with the end cash has the argument that's passed in the app will process the request based on the information in the end
hash and it'll return an array with exactly three things in it. Another way to refer to this is a temple of so, what are the three things in this Temple of three? First you have the HTTP status code, which is just a number II you have the headers hash and finally you have the body. Intuitively, we might think that the body should just be a string but it's actually not for some technical reasons. Technical reason the body is actually just an e chibal object as
in it just needs to implement the each method and it healed strings. In a very simple case though, you can just expect it to be returned as an array with a single string in it. So let's see this in action to guide us through the rack API. I'm going to call and Godfrey. Right. So I'm like the goal off the talk is to understand rails and then forget that. Before we get into rails, let's just try to use this reggae. I Will Learn by itself. Right? So let's say we write a simple rack up. This is probably one of the simplest rack app that you can
write you can see that is an object. It has a comment on it and taken and passionate checks. What is in the end facts? Right? So specifically looks at the path of the request if the path is / hello, then it's going to render a 200 response. That's the status code 200 stands for a successful request as we said we need to return an array with free things in that the first thing is 200. The second thing is a hash of headers in this case want to tell the browser Odysseus plane packs. So don't bother trying to interpret at 6 p.m. Our anything like that. And finally the first thing is an
array of the body in which case we're returning in plain text. Hello world. So else if the path is anything else other than hello we're going to do if all four I'm sure you have been counted out in the pass for $0.04 for not found it. So we're just going to be a very simple plain text response off not found if you go to anywhere other than / hello. How do we call it? How do we make it do things? We said earlier rack is really just a convention just a procolor. I like you notice that this didn't have to
stop classroom anything specific you just has like they can just be any object that respond to call. I'm so to hope that up to you if she requires you need a web server again as we talked about earlier so we can hope this was nginx or Apache but unfortunately for this kind of simple demonstration rack came with A prebuilt scripts that's kind of like a web server. Natively under like a ruby web server than a totally understand recpro call call Rekha to configure the web server. You can use a come pick that are you vile. It's
basically a ruby file with some extra DSL. So you can see that the first thing we're doing and just come pick Daria found as to require that file that we have up there. And the only other thing in there is to run hello world. New so run as the key word in the DIA cell. Tells rack up. Hey, this is the thing on the right hand side in this case Hello World. New which is an object with a. Call on it as we set up a couple of times by now. So if I don't need to really put all these together to run that you can just run the command rack up from the same directory where come
pick. Are you is so you can see under the hood rack up just wraps whatever Ruby web server you have sometimes that use thin sometimes used. What brake setup implementation details? Right? Like as far as we can, as far as we're concerned a web server that understands config don't argue which understands rack apps. So now if we open the browser go to your local holes, 1992 or 92 92. / hello. We are going to get the hello world responds and if we go to anything else likes, oh my God or / web. We're going to get out and I found
great. We have written the rack up. So we want it so that when you go to locals 9092 / right now you get enough time because Slash's not / hello. So why don't we make / redirect to / hello. So to do that, you'll probably modify your rack up to it be something like that, right? The parts of focus on is if we first checked if Flash Return a free one response code which is her response code for a permanent redirect. And in the Heather's were you return the location of the Republic which is / yellow. So this works it works great. If you open a browser now go to slash a little retarded. It's
like hello. Everything is awesome. However, as you can imagine if you keep getting things to hear this if statement is going to get pretty Big R. I like as you have more pages. This is like you're going to have like a hundred line if statement and also redirect is a pretty common thing that you might want to use in different parts of the Abyssal would be great if we can extract this into a reusable composable piece of code, and we can do that. So magic didn't work out on the screen now. I'm on the bottom. It's exactly the same Hello World app that we had on the.
Screen without any concerns about the redirect Ray like we can see that hello world is back to you to state that it wasn't need to know anything about redirects and we made a new class on top call redirect that is only responsible for handling. The reason right aspect of the app. So and you can see that in the call method. We first check if the path is matches what we want to redirect from then we'll intercept a request and return like right away and just tell the browser redirect and that
would be it. However, if it doesn't match we want to redirect from then we just going to delegate to the app that's passed them to us to do more processing. And two wire everything up it's going to look something like this. Right like we this time instead of passing hello world. On you to run. We're going to pass redirect out new but passing flow world done you too. We like app that we made earlier. So this is great and it works. And in fact, we just made out first rack Middle where you might have heard of that word before it is exactly what it is
recommended. Where is not necessarily like the unlike the breakout call protocol the Middle where is not like a concrete Concepts in the racks back is just a very useful convention like basically as far as the web server is concerned. There's no matter where I like. There's just one app with a. Call on it what you choose to do inside your. Cause it's up to you and in the case of a middle where it just happened to choose to sometimes forwarded to a different app, right? That's really it but because of how useful this
middleware convention is the rack up some fake. R u d s l has a special send text for that. I'm so basically the only problem with this year is that The nesting gets a little bit uncanny and you can imagine if we want a lot of middleware that right hand side is going to look like a very deeply enough to think and so the rack up this all has a way for you to flatten it basically instead of run you can use to use keyword to specify a list of middleware before you specified the app.
So this is pretty powerful. I'm the rec jamm itself ships with a bunch of the useful middleware that you can add to your app. So for example, something like that basically was out riding a lot of like without running any code really you have added compression to your app. You have added H-E-B catching to your app and you have handled had responses. So that's the power of this power off the middleware convention. So to recap this is like the whole rack Concept basically, we have learned at all. And so it's
now time to go back to the rails. So the whole point of this detour interact is to say that web servers uses direct protocol to communicate with Ruby web Frameworks. So therefore it must mean that rails implements retrograde like otherwise, what's the point of this detour and it does indeed if you look at your house app, you'll see that rails actually generated that convict up. Are you file for you? So you can check it out and your work out later, but it probably looks something like this one require as one single
line homerun Realty application so we know that The convention for the way that the rack up DSL works is the thing you pass to run. It's a wreck app. So therefore must mean whatever rails. Application is it must be a wreck out right? So let's try that so you can try it out guy running the rails console and we can just do rails. Application. Call right after the promise in order to call a rack up. We have to pass at the empath. Obviously we can just build in cash by hand.
But this is pretty a lot of work then pretty aeropro and I'll give the really carefully read the racks back to make sure you're passing all the things that are expected to be there. But fortunately wreck House of utility for exactly this purpose call Rack Mach request. N 4th and you just passing of your elbow and it will build the corresponding and cash for you, which is very useful for testing and stuff. So now Try calling rails application. Call again. So if we do that, you'll see that it kind of does what you expect it. Like it
might be a little bit surprising but it actually worked right like when do you use ovulation to call it actually run it actually ran entire rails app including giving you the Locking outfit that you're probably familiar with and it returned as we mentioned earlier return an array of three things. If you're not family with. Syntax, that's how you can conveniently the structure that Ray from the return value in a sign names that I'm so if you look at what the status is. Well, it's 200 if you look at the headers, it's a monitor things as common type in there
and other a bunch of other headers that reels added for you and if you look at the bodies again, 40 years in the raso takes a little bit more work to look at it, but I'm if you print it out, you will see that it's the HTML response that you expected. So that's pretty cool. We found the rails app like the relative rack up integration point but if you look at configure, are you you remember that earlier? We said there's two things you can put in a compact car. You can put run and given a nap, but you can also specify your middle. Where is there right like
that's what they use keyword is far and you see you can see there's no use keyboard. So this rails not use any middle. Where's Well turns out Realtors handle middle words differently but if you want to look at the rest middleware so you can run bin rails middleware and I would give you all the Middle where in the send text that you're familiar with you can see that rails implemented some of its internal features and middle, where's this is pretty cool because if you don't need some of these features you can just take it out. So for example, if you are writing an
API server, you might not be using cookies right? So that doesn't mean that you're stuck with it, even though you don't need it. You can just remove it by in your configuration application. RB. You can just do contact me to wear to delete and they will remove it from the stock at now. If you have to remove the part of rails that cares about cookies, so it seems great of course also, add your own. Middle where are there to I'm so this is pretty useful for some used case that you might have that we might want to wrap it around the whole request.
So finally we have looked at all the middle. Where's but WhatsApp? How does it get to the control action for that? We have Kristen about that. We know that uses for middleware and run is for the app. I see run on that last line that must be the app, right? Well, what is it? Application. Route we know that it should be a wrap rack app. So let's try it. So it's the rack up as you can see we called Borg application routes instead of the Rails application
call. But otherwise, it's the exact same example as before. What is this track app do it looks at the URL and matches it against a bunch of routing rules to find the right controller action to call. It's generated from your config / routes to RB file. Hope that looks pretty familiar. The resources DSL as shorthand for defining a bunch of route that once ultimately it expands into these 7 routes. For example, when you make a get request to post it will call the Post controller index method. If you make a put request to post / ID will go to the Post
controller update method instead. So what is this post's hash index. Well, we know that it stands for the index action on the post controller. If you follow that code you will see that it eventually expand into the post controller. Action called with index. What is that? Here's a much simplified version of the action controller code. First of all, we have the action method. You can see it returns a Lambda to Lambda takes an argument called end. What's that? Surprise is a hash and what did the land to return an array?
And by the way surprise, when does respond to call? Yep? It's a wreck at? everything is Iraq at finally putting everything together. You can imagine that the routes app. Is there a cat that looks something like this. It matches the given request pass and HGTV verb against the rules to find a new route to config and then delegate it to the appropriate rack app on the controllers. Good thing you don't have to write this by hand. Thanks, or else. You might be wondering how does rail generate this from your route to config in order to do the
mapping efficiently? There's a talk for that check out of it stock from last year Israel cam for more information. Okay. Now we know that hashtag everything is Iraq up. And we can mix and match things. Here's some Pro tips. Did you know that you can route a part of your rails up to a rack up just like that? In fact now that we learned about land has you can even write that in line. You may think it's a terrible idea, but in fact you probably use this functionality before How do you think redirect Works surprise?
It's a rack app. You can even mount a Sinatra app inside a real zat You may not know this but the sidekick web UI is written in Sinatra. You may already have a Sinatra after running inside your LSAT. Of course, you can also go the other way around Mount rails up inside your Sinatra rap fully that to your imagination. This is also another thing you can do remember that controller. Action method. I wouldn't recommend actually doing this in your rails at because it bypasses autoload and some performance optimization. It's really cool to know how everything fits together. Okay, after all
that, we've finally made it back to that controller action that we started with the sunrise. Everything is a rack at oh, wait. How does that render plane thing work? Well, maybe counter-attack next year if they're kind of running out of time. She controls Hears a Who to talk about how Skylight fits into the story. So as we learned so far Frameworks aren't magic. They are just a layer of conventions that live on top of a nice primitive under the hood that works really well conventions can help you learn
to help you collaborate with your team and we all love conventions or we wouldn't be here. But more than that conventions give the community the ability to write tool that everyone can share for example Skylight needs to wrap around our entire request so we know how long it took. Well if we've been paying attention we know how to do that. We use a middle or what better way than a middleware. Convention over configuration is more than how you build rails apps. It also allows Skylight to give you a lot of detailed information without having to configure anything. So a lot of ecosystems to
look like Skylight would require you to do a bunch of work to get things running. But because rails provides such conventions, we can leverage those in order to make your life easier to make the the scanning started store easier for you earlier Trails dispatches actions, but we skipped over this part at the bottom. You may or may not know this but real to the built-in instrumentation system and we can get detailed information about what's going on inside of our request without hacking into private apis. For example this section over here is how we get the name of your points about you
having to tell us anything about what they're called. Now it Skylight does way more than just give you the average of the response time for your request. We give you detailed aggregate traces of your entire request. Will you leverage active sport notifications and rails conventions to provide conventional descriptions for rails and four common gems without any configuration by default. We also show you important parts of the request so you can focus on speeding up what matters so if you look at this example over here that I don't know if you can see the color, but I think it's purple or
something. You should probably focus on the after Eliezer cuz that's taking up so much of the time if you want to speed up at any point, but we still collect way more information that we hide by default. So if you uncheck the condensed race track box at the top you can see that each middleware that we saw before is here in the expanded trace. And as you can see from the screen, everything is the rack up. Now there's way too much stuff to tell you about in this talk like the fact that we do a ton of my working Ross. And by the way, we have have official back on job support this year, but come to
our booth to hear more about that and chat. Let's see is Leo. I don't think so. Okay. So that's that's the life cycle of a request that was interesting for you and like Christmas that maybe next year. We'll do the life cycle of a response or something. But yes, we are hiring if that kind of stuff digging deep into rails and integrating with rails measure People's app, like having a thing that running that's running and everyone's rails out interest you we are hiring
We have great perks. Yes, you can come out sets of the booth. We are a pretty small company. We have eight people 7 with seven people in total and the downside of that. Yes. We want us. There you go. We want a people. Come be the person we we we start doing this last year, but we're like having the entire company come to do this presentation. I really appreciate meeting all customers or future potential customers and in this talk and at a booth Peter
who is another engineer or not. We have couldn't make it last minute to do some travel mishap and Liz here, but currently Handling a baby situation somewhere. It's like 20 40% of company right now, but but that's the fun part you get to know each other really well and work to like work very closely with each other. So looking forward to seeing you at the booth tomorrow and sign up for Skylight. Thank you very much.
Buy this talk
Access to all the recordings of the event
Buy this video
With ConferenceCast.tv, you get access to our library of the world's best conference talks.