Keynote article ...
Clustering on Tomcat
Subject: Clustering, using Apache http server (version 2.2.14 in my example) with mod_proxy_balancer as the front load splitter and Apache Tomcat 6.0.20 as the replicated application engine. [[Tip should also work for other recent 2.2.x and 6.0.x versions]]
Background
This is a follow on article from Load balancing with sticky sessions (httpd / Tomcat), where I looked at sharing out the application work between a number instances of Tomcat from an Apache http server (httpd) that did the bookkeeping. In a nutshell, the Apache http server sent new arrivals to a 'random' Tomcat, and then used sticky sessions so that - when a visitor came back for their subsequent visit in the same series of accesses - they would always talk to the same Tomcat and could continue their conversation with the server having full knowledge of the position to date.
The balancer alone is a good solution as far as it goes but:
• What happens if the Tomcat that has been stuck to goes out of service?
• What happens if you have such a lot of traffic that you need to replicate your httpd front end?
• What happens if your httpd fails?
• What is you don't actually want to use sessions, but still need what appears to be a single Tomcat?
One possible option to addressing some of these is to use the clustering capability of Tomcat, which I'll describe below. But you should first consider if you really need the extra step:
(a) can I accept that a session will be lost on the rare occasions that a Tomcat goes offline?
(b) is writing to a backend database going to preserve sufficient information anyway?
and if the answer to either is "yes", you probably do NOT need to cluster.
How does clustering work?
You run your web application on a series of identical (or rather "near identical" - the IP address will differ!) servers. With clustering turned on, each of the servers in the cluster is broadcasting (via multicast) any changes made in sessions, cookies, etc to any other listening cluster members on that same multicast address. So that when a visitor comes back for his / her next access, all the machines know what's been going on and can knowledgeably handle the request, even if the original machine isn't available.
You can turn clustering on in Apache Tomcat 6.0.20 simply by uncommenting the line in the default server.xml file that relates to it:
<Cluster className = "org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
and restating your Tomcat. Older versions of Tomcat (such as 5.5) had a long configuration section listing the ports, replication time, IP addresses to use, trigger files all of which are important but none of which actually needs to be changed from default in the current release that's the target of this article.
Once you have turned clustering on (yes, it's now that simple), your machines will be communicating ... it's rather like starting a rumor in an office - before you know it, EVERYONE who's around has heard the rumor.
Clustering with the balancer
If you have already implemented balancing with sticky sessions (as covered in the preceeding article), turning on clustering will cause the data to be shared around. Most of the time the data passed around will not be used - it will ONLY form a backup of the session, to be used if the balancer is unable to reach the sticky machine because it has done down or been taken out of service.
With sticky sessions activated, even a second front-end Apache http server won't cause a switch from one Tomcat to another unless a fail-over occurs, as the jvmroute is a part of the cookie so either (any) of the httpd front ends will correctly forward to the original Tomcat. And if you have an intelligent hardware load balancer, that too will be able to forward consistently and the the clustering will remain merely as a backup.
If you disable sticky sessions on your balancer, the metrics will change. Forwarding will now be at shared to each of the Tomcats in the balanced group / cluster group (take care that all members of the balance group are included in the cluster!) and so the visitor will get to a differnt back end box each time. But that's now perfectly fine, as they're sharing the data between them so will all know about the originator.
Testing if your cluster is working
Ironically, clustering and balancing is designed to be transparent, so how do you test whether it's working?
My first simple 'trick' is to change the background colour of the pages returned from each cluster member so that "if it's orange it must be Holt" and "if it's blue it must be Chippenham" (our servers are names after local towns and villages!). Going a little further, you can edit your servlet / JSP to return the name of the current host. In Java, the following line:
String myname = InetAddress.getLocalHost().getHostName();
will return you the local name of your computer, so that you can then echo the name.
On last Tuesday's course, I took our sample "Barman" script that remembers how many drinks you've had in a session (visit counter!) and extended it into a "Pub Watch" script, where each of the barman communicates with his colleagues in neighboring pubs to keep track of who's out on the town, and how much they have had to drink in each establishment.
If you click on the links in the previous paragraph, you can download the source code for "Barman" and "PubWatch" and try the code out for yourself. Using the balancer manage that I introduced at the end of yesterday's article, you can open and close individual pubs and see how their customers go elsewhere for their next drink, and you can turn sticky sessions off in the balancer and see how faithful customers will then hit the road and go to a different pub each time for their next drink.
Some notes on clustering
1. The machines in the cluster communicate through multicast, so must be on the same subnet.
2. It's a good idea for the subnet you use to have plenty of capacity if your environment is busy, and for it to be firmly behind a strong firewall from your own company's general user traffic, let alone the Internet
3. If you have multiple Tomcat clusters on the same subnet, you'll need to configure one of the clusters away from the default settings - otherwise they'll end up as being one big cluster (you'll find the word 'tribe' creaping in here!)
At present, we mention clustering on our public deploying apache httpd and Tomcat course. Only a small proportion of our delegate want to go 'that far', and for newcomers who hadn't done any web server work when they first came along a couple of days earlier, it would be just too much for the one session.
An extra day on the end of a Tomcat course, coverage in a private course, or a special session set up for the purpose ... all are possible to help you learn how clustering and balancing work. We'll have a network of computers set aside at our training centre for the purpose of setting up a test case, experimenting with configurations, seeing what happens when machines are switched on and off. Something you wouldn't dare so with your own production environment, and might be reluctant to do even on your development of test networks (that's even assuming that you do HAVE multiple machines at the development or test level). (this article written on 2009-10-30) |
Other articles ...
[2552] Web site traffic - real users, or just noise?
- (new - 2009-12-26)[2482] Load balancing with sticky sessions (httpd / Tomcat)
[2355] See this page in French
[2325] Apache, Tomcat, mod_proxy
[2273] Three recent questions on Tomcat Convertors
Top or
Show all for Apache HTTP and Tomcat Servers
[2086] C Programming v Learning to Program in C. Which course?
[2005] Variables and pointers and references - C and C++
[2004] Variable Scope in C++
[2002] New C Examples - pointers, realloc, structs and more
[1675] Comparing Objects in C++
Top or
Show all for C and C++ Programming
[2546] How well do you know Melksham - a quiz for Christmas
- (new - 2009-12-21)[2544] Vision for Wiltshire
- (new - 2009-12-20)[2540] Rock and hard place .. and the relaxing right one won
- (new - 2009-12-18)[2531] Melksham - new Tuesday Market
- (new - 2009-12-09)[2524] An update on legal changes from the FSB?
- (new - 2009-12-03)[2521] Melksham Market - Tuesdays, 09:00 to 14:00
- (new - 2009-12-01)[2517] Blogging accuracy - open invitation for any corrections
- (new - 2009-11-29)[2514] The road show has been - where now?
- (new - 2009-11-27)[2512] Kim of the Castle
- (new - 2009-11-25)[2508] Community Area Partnership - inaugural AGM
- (new - 2009-11-20)[2497] Brown horses in Melksham
- (new - 2009-11-08)[2495] Planning the future of Wiltshire
- (new - 2009-11-07)[2487] Melksham Forum
- (new - 2009-11-01)
Top or
Show all for Melksham
[2548] Ten years ago, we moved to Melksham Spa
- (new - 2009-12-24)[2541] VAT Changes
- (new - 2009-12-19)[2534] And now for some posts a bit more technical
- (new - 2009-12-12)[2366] At the end of the course
[2354] Training, Open Source computer languages
Top or
Show all for Training from Well House Consultants
[2530] Taking a knock over Santa
- (new - 2009-12-08)[2502] Monday night, Tuesday night
- (new - 2009-11-13)[2441] Not your cup of tea?
[2413] Sanity checking the price, and selling up to increase income
[2396] Easing off in our 50s?
Top or
Show all for Running a training and hotel company
[2538] Open Source Training Centre and Courses for 2010
- (new - 2009-12-16)[2537] Faster network, but not faster browsing until ...
- (new - 2009-12-14)[2493] Melksham buses to/from Chippenham, Trowbridge, Bath, Devizes, London
- (new - 2009-11-05)[2436] Melksham Hotel Rooms - pictures
[2425] Weekend and Christmas Promotion - Well House Manor Hotel, Melksham
Top or
Show all for Well House Manor - Hotel and Training Centre
[2527] Flying tonight
- (new - 2009-12-05)[2526] A reluctance to move from old shoes to new
- (new - 2009-12-05)[2519] Status Page / breaks of service in early December
- (new - 2009-11-30)[2470] Windows 7 and Open Source Programming
[2449] Four aspects - Chamber, Transport, Courses and Hotel
Top or
Show all for For the Webmaster, Postmaster and moderator
[2550] Do not copy and paste code - there are much better ways
- (new - 2009-12-26)[2536] All the Cs ... and Java too
- (new - 2009-12-13)[2535] When should I use Java, Perl, PHP, or Python?
- (new - 2009-12-13)[2510] The music of the stock market
- (new - 2009-11-22)[2507] Admission
- (new - 2009-11-19)[2505] I almost put the bins out this morning
- (new - 2009-11-16)[2501] Simples
- (new - 2009-11-12)
Top or
Show all for General Programming Topics
[2545] Scraping content for your own page via PHP
- (new - 2009-12-21)[2539] Changing Images
- (new - 2009-12-17)[2488] A variable number of arguments in a PHP function
- (new - 2009-11-02)[2478] How did I do THAT?
[2433] Controlling, supressing, enabling PHP error messages
Top or
Show all for PHP - the language and its application
[2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example
- (new - 2009-12-04)[2434] Abstract classes, Interfaces, PHP and Java
[2423] What is a JVM, a JRE, a JDK - components of the core Java Environment
[2422] Looking inside Java classes - javap and javadoc
[2421] Sorting Collections of Objects in Java
Top or
Show all for Java and the Java Environment
[2453] Dark to dark
[2443] Contrasting Cambridge, Bristol and Wiltshire
[2362] Thornham Farm Shop, Seend Cleeve, nr Melksham
[2305] Hot Tap at West Wilts Show
[2302] See us at the West Wilts Show
Top or
Show all for Around, about and nearby to Wiltshire
[2551] Perl and the Common Gateway Interface - out of fashion but still very useful?
- (new - 2009-12-26)[2532] Analysing Google arrivals by country of origin
- (new - 2009-12-10)[2484] Finding text and what surrounds it - contextual grep
- (new - 2009-10-30)[2427] Operator overloading - redefining addition and other Perl tricks
[2416] Automating access to a page obscured behind a holding page
Top or
Show all for The Perl Programming Language and its use
[2554] Adding retrospective ALT attributes to IMG
- (new - 2009-12-28)[2390] Dynamic / changing images on your web page
[2223] CSS Style Diagrams - working out where attributes come from
[2135] What features does this visitors browser support? (PHP)
[1998] Text on a background image
Top or
Show all for Client Side Languages (HTML, CSS, Javascript)
[2296] Variable scope - what is it, and how does it Ruby?
[2293] Regular Expressions in Ruby
[2292] Object Orientation in Ruby - intermediate examples
[2291] Collection objects (array and hash) in Ruby
[2290] Opening and reading files - the ruby fundamentals
Top or
Show all for Ruby and Ruby on Rails
[2458] Cant connect to local MySQL server through socket /tmp/mysql.sock
[2448] MySQL - efficiency and other topics
[2447] MySQL stored procedures / their use on the web from PHP
[2446] Learn about MySQL stored procedures
[2445] Securing MySQL on a production server
[2444] Potted MySQL installation
[2426] Which version of MySQL am I running?
[2381] Checking the database connection manually
[2263] Mysqldump fails as a cron job - a work around
[2259] Grouping rows for a summary report - MySQL and PHP
[2240] How do I query a database (MySQL)?
[2210] MySQL server replication
[2209] Monitoring and Tuning your MySQL installation
[2204] Images in a database? How big is a database? (MySQL)
[2172] PHP4 v PHP5 - Object Model Difference
[2134] Oracle take over Sun who had taken over MySQL
[2110] MySQL - looking for records in one table that do NOT correspond to records in another table
[2085] MySQL - licensing issues, even with using the name
[2053] What a difference a MySQL Index made
[1935] Summary of MySQL installation on a Linux system
[1885] Hiding a MySQL database behind a web page
[1771] More HowTo diagrams - MySQL, Tomcat and Java
[1766] Diagrams to show you how - Tomcat, Java, PHP
[1754] Upgrade from PHP 4 to PHP 5 - the TRY issue
[1735] Finding words and work boundaries (MySQL, Perl, PHP)
[1731] Apache httpd, MySQL, PHP - installation procedure
[1724] addslashes v mysql_real_escape_string in PHP
[1689] Some sideways thoughts on the news
[1575] Database design for a shopping application (MySQL)
[1574] Joining MySQL tables revisited - finding nonmatching records, etc
[1561] Uploading to a MySQL database through PHP - examples and common questions
[1518] Downloading data for use in Excel (from PHP / MySQL)
[1455] Connecting to MySQL 5 from PHP on Mac OSX Leopard
[1450] Easy selection of multiple SQL conditions from PHP
[1423] MySQL - table design and initial testing example
[1417] What software version do we teach?
[1381] Using a MySQL database to control mod_rewrite via PHP
[1331] MySQL joins revisited
[1235] Outputting numbers as words - MySQL with Perl or PHP
[1213] MySQL - the order of clauses and the order of actions
[1131] MySQL - Password security (authentication protocol)
[1123] mysqldump and mysqlrestore
[1095] Apache httpd , browser, MySQL and MySQL client downloads
[947] What is an SQL injection attack?
[937] Display an image from a MySQL database in a web page via PHP
[924] The LAMP Cookbook - Linux, Apache, MySQL, PHP / Perl
[907] Browser -> httpd -> Tomcat -> MySQL. Restarting.
[723] Viewing images held in a MySQL database via PHP
[673] Helicopter views and tartans
[666] Database design - get it right from first principles
[647] Checking for MySQL errors
[644] Using a MySQL database from Perl
[591] Key facts - SQL and MySQL
[581] Saving a MySQL query results to your local disc for Excel
[572] Giving the researcher power over database analysis
[567] Combining similar rows from a MySQL database select
[535] MySQL permissions and privileges
[517] An occasional chance, and reducing data to manageable levels
[515] MySQL - an FAQ
[513] MySQL - JOIN or WHERE to link tables correctly?
[502] SELECT in MySQL - choosing the rows you want
[494] MySQL - a score of things to remember
[489] Which MySQL server am I using?
[449] Matching in MySQL
[401] What is an SQL injection attack?
[375] Oops - I got my initial database design wrong
[361] Binary Large Objects or bars
[334] Symbolic links and hard links
[279] Getting a list of unique values from a MySQL column
[270] NULL in MySQL
[193] The wrong MySQL
[192] Current MySQL and PHP paths and upgrades
[175] Worthwhile
[159] MySQL - Optimising Selects
[158] MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN
[104] mysql_connect or mysql_pconnect in PHP?
[85] Present and future MySQL
[84] MySQL - nuggets
[59] MySQL - Pivot tables
Top or
Show all for SQL and MySQL
[2476] Tcl - uplevel to run code at calling level
[2475] Quick easy and dangerous - automated logins via Tcl / Expect
[2474] Using Tcl and Expect to automate repetitive jobs
[2472] split and join in tcl and expect
[2471] A short form of if ... then ... else
Top or
Show all for Tcl, Tcl/Tk and Expect
[2509] A life lesson from the accuracy of numbers in Excel and Lua
- (new - 2009-11-21)[2500] Dynamically formatting your results (Lua)
- (new - 2009-11-10)[2492] Lua Classes - open enrolement
- (new - 2009-11-04)[2461] Luac - getting lua to start fast by precompiling
[2455] Lua examples - coroutines, error handling, objects, etc
Top or
Show all for Programming in Lua
[2523] Plan your application before you start
- (new - 2009-12-02)[2520] Global and Enable - two misused words!
- (new - 2009-11-30)[2506] Good example of recursion in Python - analyse an RSS feed
- (new - 2009-11-18)[2504] Learning to program in ...
- (new - 2009-11-15)[2489] Parallel Pinging, using Python Threads or Expect spawn lists
- (new - 2009-11-02)[2485] How do I set up a constant in Python?
- (new - 2009-10-31)
Top or
Show all for Python Programming
[2555] Bookkeeping
- (new - 2009-12-29)[2553] On a short walk from home
- (new - 2009-12-27)[2549] Christmas Day ...
- (new - 2009-12-25)[2547] The great thing about snow ....
- (new - 2009-12-23)[2543] Day and night at Christmas
- (new - 2009-12-19)[2528] Melksham Christmas Lights
- (new - 2009-12-05)[2518] Through the arches
- (new - 2009-11-29)[2503] Melksham manslaughter case concludes - sort of
- (new - 2009-11-14)[2499] ourdog is Greyhound, Staffie and Ginger Cake
- (new - 2009-11-09)[2498] Remembrance Sunday 2009, Melksham
- (new - 2009-11-08)
Top or
Show all for And also ...
[2557] Rail Campaign - response to core consultation
- (new - 2009-12-31)[2556] Railway Station Survey - please complete today or tomorrow
- (new - 2009-12-30)[2533] Five guests, three groups, one car
- (new - 2009-12-11)[2529] Santa Special, 2009
- (new - 2009-12-06)[2522] Integrated public Transport - what could be done for Melksham
- (new - 2009-12-01)[2515] Why we have two buses an hour - but not a half hourly service
- (new - 2009-11-28)[2513] Wiltshire Council purchases Melksham Station Land
- (new - 2009-11-26)[2511] The story of the railway land at Melksham Station
- (new - 2009-11-24)[2490] Friendly Hotel in Melksham, near Bath
- (new - 2009-11-02)[2486] Santa Special - Trowbridge and Melksham to Swindon
- (new - 2009-10-31)
Top or
Show all for Public Transport
[2542] My armpit was like a zebra crossing
- (new - 2009-12-19)[2496] A better design of mouth
- (new - 2009-11-07)[2397] Signwriting is dead. Long live the sign.
[2256] Is it Python past cheetah already?
[2251] Over a third of numbers start with the digit 1
Top or
Show all for Fun and Flames
[2483] Clustering on Tomcat
- (new - 2009-10-30)[2384] Looking ahead to the Autumn season of training and accommodation
[2144] Looking for a career change - Physician to Web Site Designer
[1955] How to avoid duplicating web page maintainance
[1857] November and December Public Course Schedule
Top or
Show all for Keynote Articles
[2494] Making Linux Politically correct
- (new - 2009-11-06)[2491] Root is root for a reason!
- (new - 2009-11-03)[2479] Accidentally typed ci rather than vi?
[2301] Mistaken identity?
[2300] What does x on a linux directory mean?
Top or
Show all for Linux and Shell Programming
Public Transport • Training from Well House Consultants • And also ... • Tcl, Tcl/Tk and Expect • Programming in Lua • Python Programming • Well House Manor - Hotel and Training Centre • Apache HTTP and Tomcat Servers • The Perl Programming Language and its use • Ruby and Ruby on Rails • PHP - the language and its application • C and C++ Programming • Linux and Shell Programming • Melksham • SQL and MySQL • For the Webmaster, Postmaster and moderator • Java and the Java Environment • Fun and Flames • Running a training and hotel company • Around, about and nearby to Wiltshire • Client Side Languages (HTML, CSS, Javascript) • Keynote Articles • General Programming Topics
A little more about this newsletter ...
At Well House Consultants, we run niche IT training courses ... and we run a hotel for delegates on those courses and other visitors to Melksham too. And we make a lot of friends - have a lot of ambassadors with whom we want to keep in touch. So every day Graham (that's me, writing this piece) puts together an article or two which might include the latest sample programs that I've written during the current course, new information about
Well House Manor - our business hotel, tips on search engine optimisation,
announcements of upcoming public courses, pictures of local places, and even (on occasions) rants and whimsical pieces to keep those friends up to date and in touch. The feeds are available directlt via the Blog -
"The Horse's Mouth", they're on our
Twitter Feed and you can find me at my
LinkedIn profile.
But most people just want to look us up occasionally - every month or two, and then to catch up on the latest news just for their particular subjects of interest ... and that's what this newsletter is about
You'll find above the titles of ALL the new articles written in the last two months, listed by major subject area, and showing as
(new) with their date of publication. You'll find additional articles in each category too - topping each category up to a minimum of five articles. And you'll find a link at the end of each section which lets you expand that section to show the titles of every article that's been published in that section. After all, "the old ones are often the best ones", aren't they?