In a recent patch we improved Rails response time by >10%, our largest improvement to date. I'm going to show you how I did it, and introduce you to the tools I used, because.. who doesn’t want fast apps?
In addition to a speed increase, we see a 29% decrease in allocated objects. If you haven't already, you can read or watch more about how temporary allocated objects affect total memory use. Decreasing memory pressure on an app may allow it to be run on a smaller dyno type, or spawn more worker processes to handle more throughput. Let's back up though, how did I find these optimizations in Rails in the first place?
A year ago Heroku added metrics to the application dashboard. During the internal beta, one of the employees building the product asked if they could get access to my open source app, codetriage, because it was throwing thousands of R14-out of memory errors a day. This error occurs when you go over your allotted RAM limit on Heroku (512 mb for a hobby dyno). Before metrics, I had no clue. As the feature was made available to customers, they became acutely aware that their apps were slow, used lots of swap memory, and threw errors right and left. What should they do to get their apps back in shape?
Initially we recommended reducing the number of web worker processes. For example if you were running 3 Unicorn workers (our recommended webserver at the time) we might suggest you decrease it to 2. This solved the problem for most people. Exposing the RAM usage helped tremendously. Still, customers reported "memory leaks" and overall they weren’t happy with their memory use. When our largest customers started to ask questions, they landed on my desk.
I'm responsible for maximizing Ruby developer happiness on Heroku. This can mean writing documentation, patching the buildpack to stop pain points before they happen, or working upstream with popular open source libraries to resolve problems before they hit production. The longer I look at slow code or code with a large memory footprint, the more I see these things as reproducible and ultimately fixable bugs. As I was seeing issues in customer's reported apps and in some of my own, I went to the source to try to fix the issues.
$ derailed exec perf:objects
I ran this benchmark against my Rails app, identified a line that was allocating a large amount of memory, and refactored. In some cases we were using arrays only to join them into strings in the same method, other times we were duplicating a hash before it was merged. I slowly whittled down the allocated object count. Allocating objects takes time. If we modify an object in place without creating a duplicate, we can speed up program execution. Alternatively, we can use a pooled object like a frozen string that never needs to be re-allocated. The test app, codetriage.com, uses github style routes which requires constraints and a "catch all" glob route. Applying these optimizations resulted in a 31% speed increase in url generation for a route with a constraint:
The routing improvements combined with all the other savings gives us a speed boost of more than 10%. The best part is, we don't have to change the way we write our Rails app, you get these improvements for free by upgrading to the next version of Rails. If you're interested in where the savings came from, look at the individual commits which have the methodology and object allocation savings for my test app recorded.
Working with Rails has made me a better developer, more capable of debugging library internals, and helped our Ruby experience on the platform. Likewise having a huge number of diverse Rails apps running on the platform helps us be aware of actual pain points developers are hitting in production. It feels good when we can take these insights and contribute back to the community. Be on the lookout for the Rails 5 pre-release to give some of these changes a try. Thanks for reading, enjoy the speed.
If you like hording RAM, maximizing iterations per second, or just going plain fast follow @schneems.
It started screaming for life. Sucking in air and yelling out with a beautiful pain. I couldn't stop smiling.
I didn't know what paternity leave was until I was already there. We spent months taking classes about labour and raising a newborn. We read books, and grilled friends for tips and product recommendations. Rarely if ever, did conversation turn towards paternity leave. Of everyone I talked to, I was the only one that actually had an official "paternity leave", everyone else had "vacation". All the fathers I spoke with took at least a little time off, though most seemed to only be able to muster 1 week out of their accrued bucket. In comparison, my company offered me 4 weeks of paternity at 80% pay.
Most people can conceptualize a maternity leave. We understand that it is necessary for the Mother's body to heal, and for nursing a baby. What exactly does one do during a paternity leave? It's basically vacation for the Dad, right?
We've got a system set up where my wife is in charge of feeding the baby, and healing. I try to tackle everything else in the baby realm. This means dressing, changing, swaddling, and cleaning. Before we go out, I pack the diaper bag, and strap the baby into the carrier. I try to anticipate things our baby needs and accomplish them before they're too urgent. I started doing this for my wife, in order to take the load and worry off her, but it had an unexpected benefit - a closer bond with my son. I've always thought newborns were boring, but having one changed my mind. Every day is a small, if hardly noticeable growth, that is only visible through constant vigilance.
My schedule looks like this: I usually go to bed at around 10 depending on how much sleep I got the night before, but sometimes as early as 8. We swaddle, change, and nurse the baby, and put him in a bassinet by our bed. Every 1~2 hours he'll wake up to be fed. My wife feeds him, I change him, burp him a little, and do any necessary clothing changes. I try to see to any needs my wife has if I can. The biggest one is refilling water (I highly recommend a thermally insulated spill-proof water bottle like camelbak podium). Best case scenario: we go back to sleep for a few hours. Worst case: a few minutes. I try to combat the fatigue by napping during the day, or by going to bed earlier and not getting out of bed until later. Once I get "up" I make coffee and bring my wife food when she's ready. We are lucky enough to have family staying with us, so I don't have to prepare meals or clean.
It's hard to say what I do during the day. We still go through the same changing-feeding schedule, but it doesn't seem as bad as when you're not having to wake up. Even with naps and and early bedtime, I often find myself so drained that I can't really focus on a task for too long. I had anticipated more downtime during paternity leave. We purposefully didn't watch season 3 of "House of Cards" so we could binge it during this time, but we've not even watched the first episode. The best way I could describe life when we're awake, is things seem to take 4x as long. Before, leaving the house took 15 minutes. Now it takes 1 hour. I've taken the opportunity to do some basic handy-person tasks in my backlog, such as changing my car's cabin air filter, and doing some work on my bedroom vents so the baby won't be cold at night. Even so, I find it difficult to heavily focus on one task for very long. Even after a good night, when I'm not exhausted, my attention is always split. I always have to be ready to drop what I'm doing and switch to baby gear. While I could imagine myself already back at work for a week, I can't imagine it being healthy for me, my relationship with my wife, or my employer. In an industry chalk full of burnout, I can't imagine anything more burnout inducing than showing up at work sleep deprived, and worrying about your little one when someone is demanding you to stay late to file your TPS report. My time away has been no vacation, but the ability to focus my life's energy on a single undivided task has been greatly rewarding.
One of the hardest things about taking time off has been acknowledging that I can be replaced in my company, and taking active steps to make that replacement easier. I think this is a large reason many men don't take a long break after a child's birth even when they can. They think, "How will this place run without me? I'm really needed here". It feels great to be needed by a team, or a department, or a company. It can be hard to feel needed when you're deep in a diaper full of shit. Don't make your partner pull unnecessary double duty if they don't have to.
Since pregnancy, I've started looking at "job security" a little differently. If you're so irreplaceable, how could someone ever promote you? The most critical members of the team aren't those that single-handedly hold up the keystone, but those that lay down pillars of brick to help prop up their teammates.
A large portion of my job is supporting customers, so during the months before I left, I took deliberate time to document problems and fixes so others on my team would know how to handle them. The kind of things you don't bother writing down because you know them so well. After publishing several documents and sharing with support, I was surprised to find it helped me even before going on leave by decreasing ticket count. With a hard deadline and a clear set of handoffs it's hard to fudge things. I think my wife did a much better job preparing for the leave. She hired 2 employees and successfully delegated most of her job to 4 separate employees. She frequently says to me, "Getting pregnant was the best thing that ever happened to me professionally." Even if you're not going to have a kid, I think it's a good thought experiment the next time you request a pay raise. Talk about all the ways you've made yourself redundant. If you got promoted today, could someone take over your job? Why, or why not?
P.S. I've only ever had one person ask me if I was going to go back to work after the baby (yes), versus the countless numbers that asked my wife (yes). If you want to ask maybe try "Are both of you going back to work?" or "what's your child care plan after leave?" The best intended questions come with some of the worst pre-defined judgments.
Honestly there are too many things to share to make this a very focused post. I really wanted to talk about what it's like to have some time to spend bonding with your partner and a new child. For me it's been incredibly empowering. I can't imagine coming home from an 8 hour day, then having to wake up every hour, change a 'poopy' diaper, and not feel any resentment. My time off came with the opportunity to grow with my family instead of us falling apart. If you've taken paternity leave or know someone who has, please encourage them to share their story-both good and bad. If you're a male who is planning to have kids, ask your company if they have paternity leave or if they would consider adding the benefit. I think that men should take pride in being involved with their family. We can be incredibly involved in raising a child, even from an early age.
Every program wants to live forever. What happens when a program is forced to exit before it’s done running, and why would we want to do that?
In this post we take a look at how Unix uses signals to tell processes when and how to shut down. We will also take a look at how Ruby handles these shutdown signals and how you can ensure your program will do the right thing.
I’ve never met a developer who complained about code getting faster or taking up less RAM. In Ruby, memory is especially important, yet few developers know the ins-and-outs of why their memory use goes up or down as their code executes. This article will start you off with a basic understanding of how Ruby objects relate to memory use, and we’ll cover a few common tricks to speed up your code while using less memory.
When we moved into our house on the East Side, the interior walls were all supported by 1 inch thick long leaf pine. A builder looking at the material during a site visit casually mentioned he would make something out of it. This peaked my interests and
set me down my recent woodworking path. The only problem was that I didn't have many tools.
Through my life my dad always had a workshop, I've been building pinewood derby cars and cutting 2x4s on a table saw since I was a boy. I've got a modest collection of hand me down tools in a shed out back, but when my ambitions when from construction grade woodworking to finer things, I didn't have the space, or the tools, or the cash to get started. Techshop solves these problems.
Located about 30 minutes North of where I live, they've got a ton of woodworking machines. A $2k+ saw stop tablesaw with an amazing outfeed table, drill press, bandsaw, chop saw, and what I really needed: a jointer and planer. In addition to woodworking tools they've got metal working, quilting, laser cutting, 3d printing, and pretty much anything else you can think of as being both DIY and expensive. So far we've built a kitchen table bench, and we're working on a matching table.
The membership works like a gym. You pay a monthly fee and get access to everything. The only difference is that few things in a gym can maim you quite as quickly as the equipment found all around techshop. So to use a piece of equipment, you must first go through a training course. I took one for "basic woodshop" and one for "jointer/planer". You can see classes listed here.
A good jointer would easily cost me $700, the tech shop ran a promotion for 3 months of membership with 2 free classes for $300. Not only could I use the jointer there and save $400, I got a class out of it and a fully stocked workspace with speed squares, clamps, and more. They also have these large tables you can use in the common area, they're first come first serve, but i've never had a problem getting one.
View inside the woodshop:
It's not all rainbows and unicorn's, there's some downsides to using a community workshop.
CON: The tools break. I've gone one or two times when the machine I needed wasn't working. I often like to bring multiple things to do with me so it's not a total waste, but at home I rarely totally break a tool.
PRO: They fix the tools for you. You don't need to know how to maintain or repair any of the machines, it's great.
CON: Sweat the small stuff. The small items like drill-bits are constantly going missing or were misused to the point they're not functional. BYO-Drill-bits.
PRO: They're literally connected to a Lowes, so if you forget anything it's a 30 second walk next door.
CON: If it's crowded you need to wait to swap between tools. At home, I can keep everything setup and swap as needed. In a community shop, I need to think about my workflow a little better. Also if you only need to make one cut on a machine, often someone who is using it for a large project will let you.
PRO: Company: When you're tired, there's usually people there doing cool stuff, it's inspirational. You can ask others for technique help, but always take it with a grain of salt.
CON: It's a 30 minute drive for me, and means I must store ALL of my lumber in my car. They've got overnight storage, but it's costly at $20 a day.
PRO: It's relatively close to "fine lumber" which is a great place to buy hardwood.
When it comes to tools for a project the "I just need one more" is always present. Next time you find yourself in need, consider tech shop.
If you like building stuff or reading about building stuff follow @schneems and @rubyku.