Powered By GitBook
bigtable-memcached
Improve your application's performance by using Memcached for frequently queried data.
Billy Jacobson | Developer Relations Engineer | Google
Contributed by Google employees.
This tutorial explains how to improve your application's performance by using Memcached for frequently queried data like this:
1
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(memcachedIP, 11211));
2
Object value = mcc.get(rowId);
3
if (value != null) {
4
System.out.println("Value fetched from cache: " + value);
5
} else {
6
// Read row from database (Pseudocode)
7
Row row = dataClient.readRow(rowId);
8
mcc.set(rowId, 30 * 60, row.toString()); // Cache for 30 minutes.
9
System.out.println("Value fetched from db and cached: " + row);
10
}
Copied!
Databases are designed for specific schemas, queries, and throughput, but if you have data that gets queried more frequently for a period of time, you may want to reduce the load on your database by introducing a cache layer.
This tutorial looks at the horizontally scalable Cloud Bigtable, which is great for high-throughput reads and writes. You can optimize performance by ensuring that rows are queried somewhat uniformly across the database. If you introduce a cache for more frequently queried rows, you speed up your application in two ways: reducing the load on frequently accessed rows and speeding up responses by colocating the cache and computing.
Memcached is an in-memory key-value store for small chunks of arbitrary data. This tutorial uses the scalable, fully managed Memorystore for Memcached, because it is well integrated with the Google Cloud ecosystem.
The examples in this tutorial use Cloud Bigtable, but Spanner or Firestore would be good options too.
This tutorial uses gcloud commands for most of the steps, but you can accomplish the same tasks in the Cloud Console.

Before you begin

    1.
    ​Create a new Google Cloud project or select an existing project.
    2.
    Create a Cloud Bigtable instance and a table with one row:
    1
    cbt createinstance bt-cache "Bigtable with cache" bt-cache-c1 us-central1-b 1 SSD && \
    2
    cbt -instance=bt-cache createtable mobile-time-series "families=stats_summary" && \
    3
    cbt -instance=bt-cache set mobile-time-series phone#4c410523#20190501 stats_summary:os_build=PQ2A.190405.003 stats_summary:os_name=android && \
    4
    cbt -instance=bt-cache read mobile-time-series
    Copied!

The code

The general logic for a cache can be described as follows:
    1.
    Pick a row key to query.
    2.
    If the row key is in cache
      1.
      Return the value.
    3.
    Otherwise
      1.
      Look up the row in Cloud Bigtable.
      2.
      Add the value to the cache with an expiration.
      3.
      Return the value.
For Cloud Bigtable, your code might look like this (full code on GitHub):
1
try {
2
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(discoveryEndpoint, 11211));
3
System.out.println("Connected to Memcached successfully");
4
​
5
// Get value from cache
6
String rowkey = "phone#4c410523#20190501";
7
String columnFamily = "stats_summary";
8
String column = "os_build";
9
String cacheKey = String.format("%s:%s:%s", rowkey, columnFamily, column);
10
​
11
Object value = mcc.get(cacheKey);
12
​
13
if (value != null) {
14
System.out.println("Value fetched from cache: " + value);
15
} else {
16
System.out.println("didn't get value from cache");
17
// Get data from Bigtable source and add to cache for 30 minutes.
18
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
19
Row row = dataClient.readRow(tableId, rowkey);
20
String cellValue = row.getCells(columnFamily, column).get(0).getValue().toStringUtf8();
21
System.out.println("got data from bt " + cellValue);
22
// Set data into memcached server.
23
mcc.set(cacheKey, 30 * 60, cellValue);
24
System.out.println("Value fetched from Bigtable: " + cellValue);
25
} catch (Exception e) {
26
System.out.println("Could not set cache value.");
27
e.printStackTrace();
28
}
29
}
30
mcc.shutdown();
31
} catch (Exception e) {
32
System.out.println("Could not get cache value.");
33
e.printStackTrace();
34
}
Copied!
In this case, the cache key is row_key:column_family:column_qualifier to make it easy to access column values.
Here are some potential cache key-value pairs you could use:
    rowkey: encoded row
    start_row_key-end_row_key: array of encoded rows
    SQL queries: results
    row prefix: array of encoded rows
When creating your cache, determine the setup based on your use case. Note that Bigtable rowkeys have a size limit of 4KB, whereas Memcached keys have a size limit of 250 bytes, so your rowkey could potentially be too large.

Create a Memcached instance

This tutorial creates a Memorystore for Memcached instance, but you can install and run a local Memcached instance to try this out or for testing.
    1.
    Enable the Memorystore for Memcached API:
    1
    gcloud services enable memcache.googleapis.com
    Copied!
    2.
    Create a Memcached instance with the smallest size on the default network, in a region that is appropriate for your application:
    1
    gcloud beta memcache instances create bigtable-cache --node-count=1 --node-cpu=1 --node-memory=1GB --region=us-central1
    Copied!
    You may have to wait a few minutes for the instance to finish creation.
    3.
    Get the Memcached instance details and get the discoveryEndpoint IP address:
    1
    gcloud beta memcache instances describe bigtable-cache --region=us-central1
    Copied!

Set up a machine within the network

You need to create a place to run code on the same network as your Memcached instance. You can use a serverless option such as Cloud Functions, but a Compute Engine VM requires less configuration.
    1.
    Create a Compute Engine instance on the default network with enabled API scopes for Cloud Bigtable data:
    1
    gcloud beta compute instances create bigtable-memcached-vm \
    2
    --zone=us-central1-a \
    3
    --machine-type=e2-micro \
    4
    --image=debian-10-buster-v20200910 \
    5
    --image-project=debian-cloud \
    6
    --boot-disk-size=10GB \
    7
    --boot-disk-type=pd-standard \
    8
    --boot-disk-device-name=bigtable-memcached-vm \
    9
    --scopes=https://www.googleapis.com/auth/bigtable.data,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/trace.append,https://www.googleapis.com/auth/devstorage.read_only
    Copied!
    The zone must be in the same region as your Memcached instance.
    2.
    Connect to your new VM with SSH:
    1
    gcloud beta compute ssh --zone "us-central1-a" bigtable-memcached-vm
    Copied!

Optionally connect to Memcached via Telnet

The Memorystore for Memcached documentation contains more information about this process, but you can just run the commands below to set and get a value in the cache:
1
sudo apt-get install telnet
2
telnet $DISCOVERY_ENDPOINT_ID 11211
3
set greeting 1 0 11
4
hello world
5
get greeting
Copied!

Run the code

Now you're ready to put your code on the machine.
The instructions in this section explain how to clone the repository directly onto the VM and run it from there. If you want to customize the code, check out the article on rsyncing code to Compute Engine or use the gcloud scp command to copy your code from your local machine to your VM.
    1.
    Install Git on the VM:
    1
    sudo apt-get install git
    Copied!
    2.
    Clone the repository containing the code:
    1
    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    Copied!
    3.
    Go to the directory containing the code:
    1
    cd java-docs-samples/bigtable/memorystore
    Copied!
    4.
    Install Maven:
    1
    sudo apt-get install maven
    Copied!
    5.
    Set environment variables for your configuration:
    1
    PROJECT_ID=your-project-id
    2
    MEMCACHED_DISCOVERY_ENDPOINT="0.0.0.0"
    Copied!
    You get the MEMCACHED_DISCOVERY_ENDPOINT value from the gcloud beta memcache instances describe command that you ran in a previous step. Exclude the :11211 suffix
    6.
    Run the program once to get the value from the database, and then run it again to see that the value is fetched from the cache:
    1
    mvn compile exec:java -Dexec.mainClass=Memcached \
    2
    -DbigtableProjectId=$PROJECT_ID \
    3
    -DbigtableInstanceId=bt-cache \
    4
    -DbigtableTableId=mobile-time-series \
    5
    -DmemcachedDiscoveryEndpoint=$MEMCACHED_DISCOVERY_ENDPOINT
    Copied!
Now you have used the core concepts for putting a cache layer in front of your database, and you can integrate these concepts into your existing application.

Cleaning up

If you followed along with this tutorial, delete your VM, Cloud Bigtable instance, and Memcached instance with these commands to avoid being billed for the continued use of these resources:
1
cbt deleteinstance bt-cache
2
gcloud beta memcache instances delete bigtable-cache --region=us-central1
3
gcloud compute instances delete bigtable-memcached-vm --zone=us-central1-a
Copied!

What's next

Last modified 7mo ago