Server Managed Caching Example

This example uses the client/server topology to scale an application vertically, with many clients benefitting from centralized server-side data management.

Server-managed caching is the model for independent scalability of data and application tiers. You isolate data management in cache servers and connect to them from application server clients, so many clients benefit from the management resources of the servers.

GemFire's client/server caching gives you dynamic server pool management, server load balancing and conditioning, logical server grouping, and automatic server failover for high availability.

The servers run in one distributed system. Each client runs in its own separate distributed system. The clients forward data requests to the servers that their caches cannot fulfill. The clients also forward client-side updates. Additionally, clients can register interest in server-side data, either by specifying the data keys or by registering queries that run continuously. In both cases, updates on the server side are automatically forwarded to the client.

Running the Example

This example shows a simple client/server installation with one worker client, that gets and puts data in its cache, and one consumer client, that just receives events from the server. Both clients are connected to a single cache server. The worker client requests data from the server. If the server does not have the data cached, it automatically runs a data loader to get it. The server returns the data to the worker client. The worker client then updates the data in its cache. The update is automatically distributed to the server. The server forwards all changes made by the worker client to the consumer client. Both clients have an asynchronous listener that reports local caching activity to standard out, so you can see what is happening.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment.
  1. Change to the <InstallDirectory>/SampleCode/quickstart directory.
  2. In one session, start the cache server (see Pivotal GemFire Servers for more information):

    $ gfsh start server --name=server_mc --cache-xml-file=xml/Server.xml

    The gfsh command starts a cache server in the background, displays the status of the server and some environment information, and then exits to the command prompt.

  3. In the same session, start the consumer client:

    $ java quickstart.ClientConsumer all-keys
  4. When the consumer client asks you to start the worker client, start it in another session:

    $ java quickstart.ClientWorker 
  5. Follow the instructions on the screens, noting the listener output from each client. When both clients have exited, stop the cache server:

    $ gfsh stop server --dir=server_mc

    You can also use the deprecated cacheserver command to stop the cache server:

    $ cacheserver stop

Example Source Files

Program and cache configuration files for the clients and the server, including the loader and listener declared in the Server.xml and Client.xml files. (The server is a GemFire cacheserver process and does not have a Java source file.)

Table 1. Cache configuration files, located in SampleCode/quickstart/xml
Server.xml Configures a cache to serve caching clients. The example region also is configured with a loader.
Client.xml Configures a region as a client region in a client/server cache. The region's pool connects to the cacheserver.

Table 2. Java program files, located in SampleCode/quickstart/quickstart A client program that exercises the server cache. A consumer client that registers interest in events on the server. The server sends automatic updates for the events. A very simple CacheLoader implementation. A CacheListener that reports cache events.

Related Javadocs