Scalable Internet Architectures This page intentionally left blank Scalable Internet Architectures Theo Schlossnagle DEVELOPER’S LIBRARY Sams Publishing,800 East 96th Street,Indianapolis,Indiana 46240 USA Scalable Internet Architectures Acquisitions Editor Copyright 2007 by Sams Publishing Shelley Johnston All rights reserved.No part of this book shall be reproduced,stored in a Development Editor retrieval system,or transmitted by any means,electronic,mechanical, Damon Jordan photocopying,recording,or otherwise,without written permission from the publisher.No patent liability is assumed with respect to the use of the Managing Editor information contained herein.Although every precaution has been taken in the preparation of this book,the publisher and author assume no Gina Kanouse responsibility for errors or omissions.Nor is any liability assumed for Project Editor damages resulting from the use of the information contained herein. Andy Beaster International Standard Book Number:0-672-32699-X Library of Congress Catalog Card Number:2004092644 Copy Editor Printed in the United States of America Geneil Breeze First Printing:July 2006 Indexer 09 08 07 06 4 3 2 1 Larry Sweazy Trademarks Proofreader All terms mentioned in this book that are known to be trademarks or Kathy Bidwell service marks have been appropriately capitalized.Sams Publishing cannot attest to the accuracy of this information.Use of a term in this book should Technical Editors not be regarded as affecting the validity of any trademark or service mark. Alec Peterson Kevin Murphy Warning and Disclaimer Every effort has been made to make this book as complete and as accurate Publishing Coordinator as possible,but no warranty or fitness is implied.The information provided Vanessa Evans is on an “as is”basis.The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages Book Designer arising from the information contained in this book. Gary Adair Bulk Sales Page Layout Sams Publishing offers excellent discounts on this book when ordered Gloria Schurick in quantity for bulk purchases or special sales.For more information, please contact U.S.Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the United States,please contact International Sales [email protected] The Safari® Enabled icon on the cover of your favorite technology book means the book is available through Safari Bookshelf.When you buy this book,you get free access to the online edition for 45 days.Safari Bookshelf is an electronic reference library that lets you easily search thousands of technical books,find code samples,download chapters,and access technical information whenever and wherever you need it. To gain 45-day Safari Enabled access to this book: n Go to http://www.samspublishing.com/safarienabled n Complete the brief registration form n Enter the coupon code CPA9-NUSB-2JMG-ZYCT-DM8Q If you have difficulty registering on Safari Bookshelf or accessing the online edition,please email [email protected]. ❖ This book was made possible by the endless devotion and encouragement from my wife,Lisa,and the intoxicating cheer and innocence of my children,Zoe and Gianna. ❖ Contents at a Glance Introduction 1 Scaling Up (and Down) 5 2 Principles for Avoiding Failure 11 3 Mission-Critical Environments 25 4 High Availability. HA! No Downtime?! 37 5 Load Balancing and the Utter Confusion Surrounding It 61 6 Static Content Serving for Speed and Glory 75 7 Static Meets Dynamic Adding Caches to Reduce Costs 105 8 Distributed Databases Are Easy,Just Read the Fine Print 137 9 Juggling Logs and Other Circus Tricks 169 10 The Right Tool for the Job 199 A Spread 227 Index 247 Table of Contents Introduction 1 1 Scaling Up (and Down) 5 Defining Scalability 5 Why Do We Need to Scale? 6 Scaling Up Gone Wrong 6 What Goes Up Should Go Down 7 Real-World Considerations 8 Being a Good Architect 9 2 Principles for Avoiding Failure 11 Working in Production Environments 12 The Dot Bomb and Its Effect on Building Large Systems 13 Stability and Control 14 Rapid Development 15 Unit Testing 16 Version Control 18 Version Control in Action 20 A Different Approach to Disaster Recovery 22 Good Design 23 3 Mission-Critical Environments 25 High Availability 26 Monitoring 27 Monitoring Implementations 27 Criteria for a Capable Monitoring System 29 Coping with Release Cycles 30 Internal Release Cycles 31 External Release Cycles 33 The Cost of Complexity Shackled to Large Architectures 34 Looking for Speed 34 It’s Not a One-Man Mission 36 viii Contents 4 High Availability. HA! No Downtime?! 37 Why High Availability and Load Balancing Are Different 41 Load Balancing Is Not High Availability 42 High Availability Is Not Load Balancing 42 Traditional High Availability—The Whitepaper Approach 43 Surveying the Site 45 Pouring Concrete—Foundry ServerIron 45 High Availability Rethought (Peer-Based) 49 Peer-Based High Availability in the Real World 52 Growing Seamlessly 55 Diverging from the Web:High Availability for Email, DNS,and So On 55 Pouring Concrete 56 5 Load Balancing and the Utter Confusion Surrounding It 61 Load Balancing Basics 61 IP-Friendly Services 63 Web Switches 65 IP Virtual Servers 67 Application Layer Load Balancers 67 A Real Use 69 Abusing the Term 71 The Web and Beyond 72 Session Stickiness:A Fatal Misconception 73 So,Which One Is the Best? 74 6 Static Content Serving for Speed and Glory 75 What’s So Tricky? 76 Context Switching 77 Resource Utilization 78 The Goal 81 Building the Cluster 82 Setting Goals 83 Contents ix Tackling Content Distribution 83 A Priori Placement 84 Cache-on-Demand 86 Choosing a Web Serving Platform 88 Examining Our Availability Requirements 90 Choosing an OS 90 Wackamole 91 Reasoning 91 Installation 91 Testing the High Availability 94 Serving Content 98 World Domination 99 DNS Round-Trip Times 101 Anycast—Shared IP 102 A Final Review 104 Was It Worth It? 104 7 Static Meets Dynamic Adding Caches to Reduce Costs 105 Types of Caches 107 Layered/Transparent Cache 107 Integrated (Look-Aside) Cache 109 Data Cache 112 Write-Thru and Write-Back Caches 113 Distributed Cache 114 Deploying Caches 117 Serving a News Site 117 Simple Implementation 119 Introducing Integrated Caching 122 Tackling User Data 127 Two-Tier Execution 130 Caching Is Powerful 134 8 Distributed Databases Are Easy,Just Read the Fine Print 137 What Is a Distributed Database? 137 Data Resiliency 137