Pitfall Bugs in naming or expiration logic leading to silently wrong caching

Pitfall bugs in naming or expiration logic leading to

This preview shows page 482 - 484 out of 517 pages.

pitfall, which can be avoided by periodic design and architecture reviews when smells start to creep in. Pitfall: Bugs in naming or expiration logic, leading to silently-wrong caching behavior. As we noted, the two problems you must tackle with any kind of caching are naming and expiration. If you inadvertently reuse the same name for different objects—for example, a non-RESTful action that delivers different content depending on the logged-in user, but is always named using the same URI— then a cached object will be erroneously served when it shouldn’t be. If your sweepers don’t capture all the conditions under which a set of cached objects could become invalid, users could see stale data that doesn’t reflect the results of recent changes, such as a movie list that doesn’t contain the most recently added movies. Unit tests should cover such cases (“Caching system when new movie is added should immediately reflect new movie on the home page list”). Follow the steps in the Rails Caching Guide to turn on caching in the testing and development environments, where it’s off by default to simplify debugging. Pitfall: Slow external servers in an SOA that can adversely affect your own app’s performance. If your app communicates with external servers in an SOA, you should be prepared for the possibility that those external servers are slow or unresponsive. The easy case is handling an unresponsive server, since a refused HTTP connection will result in a Ruby exception that you can catch. The hard case is a server that is functioning but very slow: by default, the call to the server will block (wait until the operation is complete or the TCP “slow timeout” expires, which can take up to three minutes), making your app slow down as well. Even worse, since most Rails front ends ( thin , webrick , mongrel ) are single-threaded, if you are running N such front-ends (“dynos” in Heroku’s terminology) it takes only N simultaneous requests to hang your application completely. The solution is to use Ruby’s timeout library to “protect” the call, as the code in Figure 12.18 shows. 1 require ’timeout’ 2 # call external service, but abort if no answer in 3 seconds: 3 Timeout::timeout(3.0) do 4 begin 5 # potentially slow operation here 6 rescue Timeout::Error
Image of page 482
7 # what to do if timeout occurs 8 end 9 end Figure 12.18: Using timeouts around calls to an external service protects your app from becoming slow if the external service is slow. Fallacy: My app is secure because it runs on a secure platform and uses firewalls and HTTPS. There’s no such thing as a “secure platform.” There are certainly insecure platforms, but no platform by itself can assure the security of your app. Security is a systemwide and ongoing concern: Every system has a weakest link, and as new exploits and software bugs are found, the weakest link may move from one part of the system to the other. The “arms race” between evildoers and legitimate developers makes it increasingly compelling to use professionally-curated PaaS infrastructure, so you can focus on securing your app code
Image of page 483
Image of page 484

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors