---
title:  Configuring Regions
---

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

The region is the core building block of the <%=vars.product_name%> distributed system. All cached
data is organized into data regions and you do all of your data puts, gets, and querying activities
against them.

In order to connect to a <%=vars.product_name%> server, a client application must define a region that corresponds to
a region on the server, at least in name.
See [Data Regions](<%=vars.serverman%>/basic_config/data_regions/chapter_overview.html) in the *<%=vars.product_name%> User Guide*
for details regarding server regions, and [Region Attributes](region-attributes.html) in this guide for
client region configuration parameters.

You can create regions either programmatically or through declarative statements in a `cache.xml` file. 
Programmatic configuration is recommended, as it keeps the configuration close at hand and eliminates an external dependency.
Region creation is subject to attribute consistency checks.

## <a id="programmatic-region-creation"></a>Programmatic Region Creation

To create a region:

1. Create a cache using `CacheFactory` to set its characteristics.
1. The cache includes an instance of `PoolManager`&mdash;use it to create a connection pool.
1. Use the cache to instantiate a `RegionFactory` and use it to create a region, specifying any desired attributes
and an association with the connection pool.

### .NET Framework C# Region Creation Example

This example illustrates how to create two regions with different characteristics using C#:

``` csharp
var cache = new CacheFactory()
    .Set("log-level", "none")
    .Create();

var examplePool = cache.GetPoolManager()
    .CreateFactory()
    .AddLocator("localhost", 10334)
    .SetSubscriptionEnabled(true)
    .Create("examplePool");

var clientRegion1 = cache.CreateRegionFactory(RegionShortcut.PROXY)
  .SetPoolName("examplePool")
  .Create("clientRegion1");

var clientRegion2 = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY)
  .SetPoolName("examplePool")
  .SetEntryTimeToLive(ExpirationAction.Invalidate, new TimeSpan(0, 0, 120))
  .Create("clientRegion2");
```

## <a id="declarative-region-creation"></a>Declarative Region Creation

Declarative region creation involves placing the region's XML declaration, with the appropriate
attribute settings, in a `cache.xml` file that is loaded at cache creation.

Like the programmatic examples above, the following example creates two regions with attributes and a connection pool:

``` xml
<?xml version="1.0" encoding="UTF-8"?>
<client-cache
    xmlns="http://geode.apache.org/schema/cpp-cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://geode.apache.org/schema/cpp-cache
      http://geode.apache.org/schema/cpp-cache/cpp-cache-1.0.xsd"
    version="1.0">
    <pool name="examplePool" subscription-enabled="true">
        <server host="localhost" port="10334" />
    </pool>
    <region name="clientRegion1" refid="PROXY">
        <region-attributes pool-name="examplePool"/>
    </region>
    <region name="clientRegion2" refid="CACHING_PROXY">
        <region-attributes pool-name="examplePool">
            <region-time-to-live>
                <expiration-attributes timeout="120s" action="invalidate"/>
            </region-time-to-live>
        </region-attributes>
    </region>
</client-cache>
    
```

The `cache.xml` file contents must conform to the XML described in the `cpp-cache-1.0.xsd` file provided in your distribution's `xsds` subdirectory
and available online at [https://geode.apache.org/schema/cpp-cache/cpp-cache-1.0.xsd](https://geode.apache.org/schema/cpp-cache/cpp-cache-1.0.xsd).

## <a id="invalidating-regions"></a>Invalidating and Destroying Regions

Invalidation marks all entries contained in the region as invalid (with null values). Destruction
removes the region and all of its contents from the cache.

<a id="invalidating-region__section_6F7E304D1D5743F1B53FCBD4F82651D0"></a>
You can execute these operations explicitly in the local cache in the following ways:

-   Through direct API calls from the client using `Apache::Geode::Client::IRegion<TKey, TValue >::InvalidateRegion()`.
-   Through expiration activities based on the region's statistics and attribute settings.

In either case, you can perform invalidation and destruction as a local or a distributed operation.

-   A local operation affects the region only in the local cache.
-   A distributed operation works first on the region in the local cache and then distributes the operation to all other caches where the region is defined. This is the proper choice when the region is no longer needed, or valid, for any application in the distributed system.
-   If the region on the server is configured as a partitioned region, it cannot be cleared using API calls from the client.

A user-defined cache writer can abort a region destroy operation. Cache writers are synchronous
listeners with the ability to abort operations. If a cache writer is defined for the region anywhere
in the distributed system, it is invoked before the region is explicitly destroyed.

Whether carried out explicitly or through expiration activities, invalidation and destruction cause event notification.


## <a id="region-access"></a>Region Access

You can use `Cache::getRegion` to retrieve a reference to a specified region.

`Cache::getRegion` returns `nullptr` if the region is not already present in the application's cache. A server region must already exist.

A region name *cannot* contain these characters:

| Ineligible Character description | Ineligible Character |
|----------------------------------|----------------------|
| whitespace                       |  _space_ or _tab_    |
| angle brackets                   |  &lt; &nbsp; &gt;    |
| colon                            |   :                  |
| quote                            |   `"`                |
| forward slash and back slash     |   / &nbsp;   \\      |
| pipe (vertical bar)              |   &#124;             |
| question mark                    |   ?                  |
| asterisk                         |   \*                 |


## <a id="getting-region-size"></a>Getting the Region Size

The `Region` API provides a `Size` property that gets the size of a region.
For client regions, this gives the number of entries in the local cache, not on the servers.
See the `Region` API documentation for details.
