18.5. Scalability of Your Site
Scaling is the ability for your site to support more and more traffic by adding more resources (usually hardware) to it. Ideally, your site should be able to handle any amount of traffic just by adding enough servers.
This can be easily achieved by adding as many TurboGears application servers as necessary and throwing a load balancer in front. However, you have to be careful about one thing: A TurboGears process on one machine needs to be able to access all the data it needs. (That data can't be in a TurboGears process on another machine.)
If you avoided sessions and stored all your data in cookies or on the database, you're all set. Your application should "scale out" without any significant work at all.
18.5.1. Scaling Sessions
If you are using sessions, and you need to scale to multiple machines, fortunately all is not lost. If you're using sessions in your application, all you have to do is make sure that a user will keep getting session data across multiple requests. There are two basic ways to go about achieving that goal:
You can achieve the first option by storing the session data in a database. Then all you have to do is connect all your TurboGears processes to the same database. Alternatively, sessions can be stored in files and the filesystem could be shared by the various machines, but that is probably more complicated.
You can achieve the second option by using a "smart" load balancer that can extract the session ID from each request and direct the request to the corresponding TurboGears process. That way you could still use RAM-based sessions, because the various TurboGears processes don't need to share the session data.
18.5.2. Scaling Your Application
You don't need to do anything special in your application to make sure it scales. All you have to do is store your data in a database. That way, you can add as many machines running TurboGears as you want, and they can all easily connect to the database to access the data.
If your database grows and your traffic increases, there might come a time when you have to start spreading your database itself over multiple machines, but then you benefit from years of research and development that went into making database engines scale. At the point where you need to scale your database, it is extraordinarly helpful if your data can be partitioned into seprate databases. If your customer data is all independent, for example, there is no reason you couldn't store each customer on its own database server.
If that's not possible, these days all the major database engines, such as Oracle, SQLServer, PostgreSQL, and MySQL, can be clustered on multiple machines. But database clustering is a topic for another book.
18.5.3. Scaling at a Lower Cost
Just because you can handle more traffic easily by adding more hardware doesn't mean that doing so is feasible in practice: All that hardware costs money to buy, and it also costs money to host it. For this reason, you probably want to make sure that you make the most out of your hardware (that is, handle as much traffic as possible with one machine).
To do this, be careful about how you program your application. You should measure how much time requests take to complete and identify the bottlenecks. Also, whenever possible, use caching techniques for your whole pages, or just part of your pages or database queries.