Do I have to restart Zope periodically?
For a production website that needs to be available 24/7, are there any issues with leaving Zope (or a ZEO cluster) running indefinitely?
You don't have to restart Zope periodically, but it's considered best practice to do so, and depending on the size and activity of your site, you'll very likely eventually run into some problems if you don't. The reason is that Zope runs as a python process, and under certain circumstances a python process can consume all of the available memory on the host system. This isn't a 'bug' in python, or necessarily even the code in the script that python is running. Rather, it's a consequence of the fact that python uses a reference-counting garbage collection scheme. This scheme is how python manages memory usage; specifically it's how python knows when to return memory to the OS by deleting objects that are no longer being used. However, under some circumstances, it isn't able to know that an object is no longer being used, or more correctly it knows that there are a group of objects that are referencing each other and there is no way to determine a safe order in which to delete them, so it has to just leave them be. When that happens the objects aren't destroyed (and thus the memory they were occupying isn't freed) until the entire process terminates, allowing python to indiscriminately clean up everything.
The point is that a long-running Zope process can eventually accumulate a very large memory footprint, much larger than it's actually using for the activities that it's doing. And the only way to reduce that footprint is to terminate the process and restart it, allowing python to free all of the extraneous memory and get back to a clean slate for the process.
How often should the process be restarted?
There is no hard and fast rule. It depends (not surprisingly) on both the capacity of the system on which it's running (i.e. how much RAM it has), and on the nature of the activity and scale of traffic on the website that the Zope process is providing. If this is a production website, you should already be thinking about how to monitor the system and keep tabs on the resources used and the average load and response behavior of Zope. It should be straightforward to identify when/if the Zope process begins to bloat to the point of jeopardizing performance.
If you're not being proactive about it, you might even find out the hard way, when Zope starts throwing mysterious exceptions (MemoryError, ClientDisconnected, etc..). Of course mysterious exceptions could be caused by all kinds of problems, but if you're left scratching your head because you can't identify the cause, it may simply be that Zope soaked up too much memory and started making a mess of things.
How do I restart the process?
Several administrative scripts are available with a Plone install. You probably used one of them to start Zope. And you can probably just run the same script and pass it 'restart' as an argument. But here are a few specific examples:
- For Zope running as a single instance:
- For Zope running as a ZEO cluster:
- For Zope running as a ZEO cluster (if you only want to restart the clients and not the zeoserver):
How do I get this to happen automatically?
On GNU/Linux and OSX: use cron. Create an entry in your crontab that executes the step(s) you identified above as suitable for your install.
On Windows: sorry, I don't know.
Anything else I should know/do?
Note that you should schedule the restart to occur at a time that will have the lowest likelihood of interfering with users accessing your site/services. If your site serves a single region, then sometime in the very early AM is probably a good choice. If your site is international, and experiences sustained traffic at all times, then you probably want to be a bit more sophisticated in how you manage any potential downtime, and should consider using a load-balancer, which is beyond the scope of this discussion.
If you're using a ZEO cluster, which has multiple Zope client processes, it's a good idea to stagger the restarts of the clients, so that they're not all down simultaneously (even briefly). Again, a load-balancer can assist here.
Supervisor has also been recommended as a way to help manage this (and many other) administrative tasks for a Zope deployment.
If you're using a ZEO cluster, typically the zeoserver process does not need to participate in a restart schedule. It's been described as a "dumb python pickle reader/writer", and doesn't exhibit the same kind of long-running memory bloat that can be witnessed with the Zope client processes. However, it's been noted that the zeoserver can incur large increases in memory usage during/after a 'pack' of large ZODB storages. You should observe the behavior of your zeoserver processes during the course of any regular maintenance and if you think it warrants a restart then go ahead, it won't hurt.
Does this advice still apply to the latest and greatest versions of Python, Zope and Plone?
Maybe. Maybe not. Rumor is that the memory bloat often witnessed with long-running Plone instances had less to do with conventional garbage collection (and cyclic references) and more to do with a specific memory management problem in Python 2.4.* (which was used by Plone until v4). This same rumor asserts that Python 2.6+ does not suffer the problem to the same degree. (Thanks to Steve McMahon for the tip.)
So it's possible that in the future the need to restart Plone processes will be much less frequent. The general advice about carefully monitoring the resources used by your site will always be applicable, and you can judge for yourself whether memory bloat is a problem, and if it is then the guidelines provided here will remain applicable.