You are previewing Enterprise Rails.
O'Reilly logo
Enterprise Rails

Book Description

What does it take to develop an enterprise application with Rails? Enterprise Rails introduces several time-tested software engineering principles to prepare you for the challenge of building a high-performance, scalable website with global reach. You'll learn how to design a solid architecture that ties the many parts of an enterprise website together, including the database, your servers and clients, and other services as well.

Many Rails developers think that planning for scale is unnecessary. But there's nothing worse than an application that fails because it can't handle sudden success. Throughout this book, you'll work on an example enterprise project to learn first-hand what's involved in architecting serious web applications.

With this book, you will:

  • Tour an ideal enterprise systems layout: how Rails fits in, and which elements don't rely on Rails

  • Learn to structure a Rails 2.0 application for complex websites

  • Discover how plugins can support reusable code and improve application clarity

  • Build a solid data model -- a fortress -- that protects your data from corruption

  • Base an ActiveRecord model on a database view, and build support for multiple table inheritance

  • Explore service-oriented architecture and web services with XML-RPC and REST

  • See how caching can be a dependable way to improve performance

Building for scale requires more work up front, but you'll have a flexible website that can be extended easily when your needs change. Enterprise Rails teaches you how to architect scalable Rails applications from the ground up.

"Enterprise Rails is indispensable for anyone planning to build enterprise web services. It's one thing to get your service off the ground with a framework like Rails, but quite another to construct a system that will hold up at enterprise scale. The secret is to make good architectural choices from the beginning. Chak shows you how to make those choices. Ignore his advice at your peril." -- Hal Abelson, Prof. of Computer Science and Engineering, MIT

Table of Contents

  1. Special Upgrade Offer
  2. Preface
    1. The Tale of Twitter
    2. Speed Versus Scalability
    3. What to Expect in This Book
      1. Topics
      2. How This Book Is Organized
      3. Who Is This Book For?
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. Comments and Questions
    8. Acknowledgments
  3. 1. The Big Picture
    1. What Is Enterprise?
    2. Growing Slowly
    3. Understanding All the Pieces
      1. Persistence Layer
      2. Application Layer
        1. A front-end and services
        2. Web-services layer
      3. Caching Layer
      4. Messaging System
      5. Web Server
      6. Firewall
  4. 2. Organizing with Plugins
    1. Benefits
    2. Writing Your Own Plugins
      1. Core Enhancements
        1. Initialization template
        2. Core plugin template
        3. Testing
        4. Using a core plugin
      2. Custom Extensions
        1. Initialization template
        2. Extension plugin template
        3. Using a custom extension
        4. Testing
    3. Deployment
      1. svn:externals
  5. 3. Organizing with Modules
    1. Files and Directories
    2. Module Boundaries for Namespacing
      1. ActiveRecord Associations Between Modules
      2. Reciprocal Relationships
      3. Modules Presage Services
      4. Ensuring Proper Load Order
    3. Exercises
    4. Refactor Steps
      1. High-Level Module Refactor
      2. Detangling Utility Methods
  6. 4. Database As a Fortress
    1. Your Database Is an Application, Too
      1. “One Framework to Rule Them All”
      2. “No One in My Company Will Ever Write a Bug”
      3. “This Rails Application Is the Only Application on Earth”
    2. Sit Atop the Shoulders of Giants
      1. Choosing the Right RDBMS
      2. A Note on Migrations
      3. Dispelling Myths
    3. Operations and Reporting
  7. 5. Building a Solid Data Model
    1. Theatre Tickets
      1. Starting Simple
      2. Constraints
      3. Mythbusting
      4. Referential Integrity
      5. Intro to Indexing
  8. 6. Refactoring to Third Normal Form
    1. Third Normal Form
      1. Timing Is Key
    2. Refactoring: Inheritance and Mixins
    3. Exercises
    4. Refactor Steps
  9. 7. Domain Data
    1. Dealing with Zip Codes
    2. Strategy Pattern with Domain Tables
    3. Refactor from Day One
  10. 8. Composite Keys and Domain Key/Normal Form
    1. Why Composite Natural Keys Matter
      1. Spotting Composite Keys
    2. Atop the Shoulders of…
      1. Migrating to Domain Key/Normal Form
        1. Single column keys
      2. Using Composite Keys in Rails
        1. Using the composite_primary_keys plugin
        2. Using a DK/NF-Rails hybrid
        3. Making assignment easier with method overrides
      3. Deferrable Foreign Key Constraints
      4. Understanding the Trade-offs
        1. Efficient use of indexes
        2. Ease of writing code
    3. Exercises
    4. Refactor Steps
      1. Single Column Refactor
      2. Multiple Column Refactor
  11. 9. Guaranteeing Complex Relationships with Triggers
    1. Constraint Checking with Triggers
      1. Anatomy of a PL/pgSQL Function
        1. It’s all strings
        2. Local variables and assignment
        3. Blocks
        4. Trigger-specific features
        5. Triggers: fail safe versus fail fast
        6. Conditionals
  12. 10. Multiple Table Inheritance
    1. The Problem
    2. What Is Polymorphism?
    3. Inheritance and Persistence
      1. Single Table Inheritance
      2. Multiple Table Inheritance
      3. XOR on Columns
      4. Elegant MTI in Rails
    4. Factory Classes
    5. Exercises
    6. Refactor Steps
      1. Refactoring STI
      2. Refactoring: polymorphic => true
  13. 11. View-Backed Models
    1. Database Views
      1. Creating a View
    2. Basing a Model on a View
    3. Considerations
      1. Insert, Update, Delete
      2. References and Constraints
      3. Indexing
    4. Exercises
    5. Refactor Steps
  14. 12. Materialized Views
    1. Materialized View Principles
    2. A View to Materialize
      1. Getting into Form
    3. The Target Table
    4. Refresh and Invalidation Functions
      1. Time Dependency
      2. Who Pays the Price?
        1. 1:1 updates
        2. 1:N updates
        3. N:1 updates
    5. Triggered Refreshes and Invalidations
      1. Movie Showtimes
        1. Movies
        2. Theatres
        3. Orders
        4. Purchased tickets
    6. Hiding the Implementation with the Reconciler View
      1. Periodic Refreshes
        1. Completing the circle
    7. Cache Indexes
    8. Results
    9. Cascading Caches
    10. Exercises
  15. 13. SOA Primer
    1. What Is SOA?
    2. Why SOA?
      1. Shared Resources
        1. Synchronized tables
        2. A shared database
        3. A service-oriented architecture
      2. Reduce Database Load
        1. Slicing and dicing
        2. The myth of database replication
      3. Scalability II: Caching Is Tricky
      4. Reduce Local Complexity
      5. In Summary
    3. Exercises
  16. 14. SOA Considerations
    1. Service Considerations
      1. Implementation Details Are Hidden
      2. API Is Accessible
    2. API Design Best Practices
      1. Send Everything You Need
      2. Limit Round Trips
      3. Look for Opportunities for Parallelization
      4. Send as Little as Possible
    3. REST Versus XML-RPC Versus SOAP
      1. XML-RPC
      2. SOAP
  17. 15. An XML-RPC Service
    1. ActionWebService and Rails 2.0
    2. Creating an Abstraction Barrier
      1. ActiveRecord As the Physical Model Layer
      2. The Object Model Layer
      3. Defining the API
    3. More Testing
    4. The Client Plugin
      1. Shared Code
      2. The Client Singleton
      3. Integration Testing
  18. 16. Refactoring to Services
    1. An Orders Service
    2. Integrating with the MoviesService
      1. Other Considerations
    3. MoviesService Object Model
    4. Putting It All Together
  19. 17. REST Primer
    1. REST Basics
      1. Resources and Verbs
      2. Hardware Is Part of the Application
    2. Mapping REST to SOA
      1. Mapping to CRUD
      2. Different Clients, One Interface
      3. HTTP+POX
      4. Defining a Service Contract
      5. REST Clients in Ruby
      6. The Way the Web Was Meant to Be Used
  20. 18. A RESTful Web Service
    1. Scoping the Problem
      1. Tools
        1. ROXML
        2. Net::HTTP
    2. MoviesWebService
      1. Resources Server Implementation
      2. Actions Server Implementation
      3. A Client Implementation
        1. wadl.rb
        2. REST Describe
  21. 19. Caching End to End
    1. Data Layer Caching, Revisited
      1. The Snapshot
      2. The Refresh Function
      3. Invalidation Triggers
      4. Indexing
    2. Logical Model Caching
      1. Considerations
      2. Avoiding Rebuilding with Stale Data
      3. Cache Indexes
    3. Other Caching
      1. Query Plan Caching
      2. Database Query Caching
      3. Rails Query Caching
      4. Fragment, Action, and Page Caching
  22. Index
  23. About the Author
  24. Colophon
  25. Special Upgrade Offer
  26. Copyright