Table of contents
7
Table of Contents
13
Preface
14
The Tale of Twitter
16
Speed Versus Scalability
17
What to Expect in This Book
19
Topics
21
How This Book Is Organized
22
Who Is This Book For?
22
Conventions Used in This Book
23
Using Code Examples
23
Safari® Books Online
23
Comments and Questions
24
Acknowledgments
25
Chapter 1. The Big Picture
25
What Is Enterprise?
27
Growing Slowly
29
Understanding All the Pieces
29
Persistence Layer
32
Application Layer
33
A front-end and services
34
Web-services layer
35
Caching Layer
40
Messaging System
40
Web Server
40
Firewall
43
Chapter 2. Organizing with Plugins
44
Benefits
45
Writing Your Own Plugins
46
Core Enhancements
46
Initialization template
46
Core plugin template
48
Testing
49
Using a core plugin
50
Custom Extensions
50
Initialization template
50
Extension plugin template
52
Using a custom extension
53
Testing
54
Deployment
54
svn:externals
55
Chapter 3. Organizing with Modules
56
Files and Directories
57
Module Boundaries for Namespacing
58
ActiveRecord Associations Between Modules
59
Reciprocal Relationships
60
Modules Presage Services
62
Ensuring Proper Load Order
63
Exercises
63
Refactor Steps
63
High-Level Module Refactor
64
Detangling Utility Methods
65
Chapter 4. Database As a Fortress
66
Your Database Is an Application, Too
66
“One Framework to Rule Them All”
66
“No One in My Company Will Ever Write a Bug”
67
“This Rails Application Is the Only Application on Earth”
67
Sit Atop the Shoulders of Giants
68
Choosing the Right RDBMS
68
A Note on Migrations
70
Dispelling Myths
71
Operations and Reporting
75
Chapter 5. Building a Solid Data Model
75
Theatre Tickets
76
Starting Simple
78
Constraints
87
Mythbusting
88
Referential Integrity
94
Intro to Indexing
97
Chapter 6. Refactoring to Third Normal Form
97
Third Normal Form
101
Timing Is Key
102
Refactoring: Inheritance and Mixins
106
Exercises
106
Refactor Steps
109
Chapter 7. Domain Data
111
Dealing with Zip Codes
113
Strategy Pattern with Domain Tables
117
Refactor from Day One
119
Chapter 8. Composite Keys and Domain Key/Normal Form
121
Why Composite Natural Keys Matter
123
Spotting Composite Keys
125
Atop the Shoulders of…
126
Migrating to Domain Key/Normal Form
126
Single column keys
129
Using Composite Keys in Rails
129
Using the composite_primary_keys plugin
130
Using a DK/NF-Rails hybrid
132
Making assignment easier with method overrides
133
Deferrable Foreign Key Constraints
135
Understanding the Trade-offs
136
Efficient use of indexes
137
Ease of writing code
137
Exercises
137
Refactor Steps
138
Single Column Refactor
139
Multiple Column Refactor
141
Chapter 9. Guaranteeing Complex Relationships with Triggers
141
Constraint Checking with Triggers
145
Anatomy of a PL/pgSQL Function
146
It’s all strings
146
Local variables and assignment
147
Blocks
147
Trigger-specific features
147
Triggers: fail safe versus fail fast
148
Conditionals
151
Chapter 10. Multiple Table Inheritance
151
The Problem
153
What Is Polymorphism?
155
Inheritance and Persistence
156
Single Table Inheritance
157
Multiple Table Inheritance
159
XOR on Columns
161
Elegant MTI in Rails
167
Factory Classes
168
Exercises
169
Refactor Steps
169
Refactoring STI
169
Refactoring: polymorphic => true
171
Chapter 11. View-Backed Models
172
Database Views
173
Creating a View
173
Basing a Model on a View
174
Considerations
175
Insert, Update, Delete
176
References and Constraints
177
Indexing
177
Exercises
178
Refactor Steps
179
Chapter 12. Materialized Views
180
Materialized View Principles
181
A View to Materialize
182
Getting into Form
184
The Target Table
185
Refresh and Invalidation Functions
187
Time Dependency
189
Who Pays the Price?
191
1:1 updates
191
1:N updates
192
N:1 updates
192
Triggered Refreshes and Invalidations
193
Movie Showtimes
195
Movies
196
Theatres
196
Orders
197
Purchased tickets
198
Hiding the Implementation with the Reconciler View
201
Periodic Refreshes
201
Completing the circle
202
Cache Indexes
203
Results
204
Cascading Caches
204
Exercises
207
Chapter 13. SOA Primer
207
What Is SOA?
210
Why SOA?
211
Shared Resources
211
Synchronized tables
212
A shared database
213
A service-oriented architecture
214
Reduce Database Load
215
Slicing and dicing
218
The myth of database replication
220
Scalability II: Caching Is Tricky
221
Reduce Local Complexity
224
In Summary
224
Exercises
225
Chapter 14. SOA Considerations
225
Service Considerations
226
Implementation Details Are Hidden
228
API Is Accessible
229
API Design Best Practices
229
Send Everything You Need
231
Limit Round Trips
233
Look for Opportunities for Parallelization
234
Send as Little as Possible
236
REST Versus XML-RPC Versus SOAP
236
XML-RPC
238
SOAP
241
Chapter 15. An XML-RPC Service
241
ActionWebService and Rails 2.0
242
Creating an Abstraction Barrier
242
ActiveRecord As the Physical Model Layer
244
The Object Model Layer
249
Defining the API
254
More Testing
256
The Client Plugin
258
Shared Code
258
The Client Singleton
260
Integration Testing
263
Chapter 16. Refactoring to Services
263
An Orders Service
275
Integrating with the MoviesService
277
Other Considerations
278
MoviesService Object Model
289
Putting It All Together
291
Chapter 17. REST Primer
291
REST Basics
291
Resources and Verbs
293
Hardware Is Part of the Application
294
Mapping REST to SOA
294
Mapping to CRUD
296
Different Clients, One Interface
298
HTTP+POX
298
Defining a Service Contract
300
REST Clients in Ruby
301
The Way the Web Was Meant to Be Used
303
Chapter 18. A RESTful Web Service
303
Scoping the Problem
305
Tools
305
ROXML
307
Net::HTTP
308
MoviesWebService
309
Resources Server Implementation
312
Actions Server Implementation
313
A Client Implementation
315
wadl.rb
318
REST Describe
319
Chapter 19. Caching End to End
320
Data Layer Caching, Revisited
320
The Snapshot
321
The Refresh Function
321
Invalidation Triggers
322
Indexing
322
Logical Model Caching
329
Considerations
332
Avoiding Rebuilding with Stale Data
336
Cache Indexes
337
Other Caching
337
Query Plan Caching
338
Database Query Caching
339
Rails Query Caching
340
Fragment, Action, and Page Caching
343
Index