Caching data with Memorystore

High performance scalable web applications often use a distributed in-memorydata cache in front of or in place of robust persistent storage for some tasks.We recommend using Memorystore for Redis as your caching service. NotethatMemorystore for Redis does not provide a free tier.SeeMemorystore Pricing for details.

Before getting started, make sure your app will stay within theMemorystore for Redis quotas.

When to use a memory cache

Session data, user preferences, and other data returned by queries for web pagesare good candidates for caching. In general, if a frequently run query returnsa set of results that do not need to appear in your app immediately, you cancache the results. Subsequent requests can check the cache and only query thedatabase if the results are absent or have expired.

If you store a value only in Memorystore without backing it up inpersistent storage, be sure that your application behaves acceptably if thevalue expires and is removed from the cache. For example, if the sudden absenceof a user's session data would cause the session to malfunction, that datashould probably be stored in the database in addition to Memorystore.

Understanding Memorystore permissions

Every interaction with a Google Cloud service needs to be authorized. Forexample, to interact with a Redis database hosted by Memorystore, your appneeds to supply the credentials of an account that is authorized to accessMemorystore.

By default your app supplies the credentials of the App Enginedefaultservice account, which is authorized toaccess databases in the same project as your app.

If any of the following conditions are true, you need to use an alternativeauthentication technique that explicitly provides credentials:

  • Your app and the Memorystore database are in differentGoogle Cloud projects.

  • You have changed the roles assigned to the default App Engineservice account.

For information about alternative authentication techniques, seeSetting up Authentication for Server to Server ProductionApplications.

Overview of using Memorystore

To use Memorystore in your app:

  1. Set up Memorystore for Redis, which requires youto create a Redis instance on Memorystore and create aServerless VPC Access that your app uses to communicate with theRedis instance.

  2. Install a client library for Redis and use Redis commands to cache data.

    Memorystore for Redis is compatible withany client library forRedis.

    Go

    This guide describes using theredigo client library to send Redis commands from your app.

    Java

    This guide describes using theJedis client library to send Redis commands from your app. For details about using Jedis, see theJedis wiki.

    Node.js

    This guide describes using thenode_redis client library to send Redis commands from your app.

    PHP

    This guide describes using thePHPRedis client library to send Redis commands from your app.

    Python

    This guide describes using theredis-py 3.0 client library to send Redis commands from your app.

    Ruby

    This guide describes using theredis-rb client library to send Redis commands from your app.

  3. Test your updates.

  4. Deploy your app to App Engine.

Setting up Memorystore for Redis

To set up Memorystore for Redis:

  1. Create a Redis instance in Memorystore.

    When prompted to select a region for your Redis instance, select thesame regionin which your App Engine app is located.

  2. Note the IP address and port number of the Redis instance you create.You will use this information when youcreate a Redis clientin your code.

  3. Connect your App Engine to a VPC network.Your app can only communicate with Memorystore through a VPC connector.

    Be sure to add the VPC connection information to yourapp.yaml file asdescribed inConfiguring your app use the connector.

Installing dependencies

Go

To make the redigo client library available to your app when it runs in App Engine, add the library to your app's dependencies. For example, if you use ago.mod file to declare dependencies, add the following line to yourgo.mod file:

module github.com/GoogleCloudPlatform/golang-samples/tree/master/memorystore/redis

Learn more aboutspecifying dependencies for your Go app.

Java

To make the Jedis client library available to your app when it runs in App Engine, add the library to your app's dependencies. For example, if you use Maven add the following dependency in yourpom.xml file:

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.1.0</version></dependency>

Node.js

To make the node_redis client library available to your app when it runs in App Engine, add the library to your app'spackage.json file.

For example:

{"name":"memorystore-redis","description":"An example of using Memorystore(Redis) with Node.js","version":"0.0.1","private":true,"license":"Apache Version 2.0","author":"Google Inc.","engines":{"node":">=16.0.0"},"dependencies":{"redis":"^4.0.0"}}

Learn more aboutspecifying dependencies for your Node.js app.

PHP

To make the PHPRedis client library available to your app when it runs in App Engine, add theredis.so extension to your app'sphp.ini file. For example:

; Enable the Redis extension on App Engineextension=redis.so

For more information about enabling PHP extensions in App Engine, seeDynamically loadable extensions.

Python

To make the redis-py client library available to your app when it runs in App Engine, add the following line to your app'srequirements.txt file:

redis

The App Engine Python 3 runtime will automatically upload all libraries your app'srequirements.txt file when youdeploy the app.

For local development, we recommend that you install dependencies in a virtual environment such asvenv.

Ruby

To make the redis-rb client library available to your app when it runs in App Engine, add the library to your app'sGemfile file.

source"https://cloud.google.com/memorystore"gem"redis-rb"

Creating a Redis client

To interact with a Redis database, your code needs to create a Redis client tomanage the connection to your Redis database. The following sections describecreating a Redis client using the Redis client library.

Specifying environment variables

The Redis client library uses two environment variablesto assemble the URL for your Redis database:

  • A variable to identify the IP address of the Redis databaseyoucreated in Memorystore.
  • A variable to identify the port number of the Redis databaseyou created in Memorystore.

We recommend you define these variables in your app'sapp.yaml file instead ofdefining them directly in your code. This makes it easier to run your app indifferent environments, such as a local environment and App Engine. Learn more about environment variables in theapp.yaml reference page.

Go

For example, add the following lines to yourapp.yaml file:

env_variables:REDISHOST:'10.112.12.112'REDISPORT:'6379'

Java

For example, add the following lines to yourapp.yaml file:

env_variables:redis.host:'10.112.12.112'redis.port:'6379'

Node.js

For example, add the following lines to yourapp.yaml file:

env_variables:REDISHOST:'10.112.12.112'REDISPORT:'6379'

PHP

For example, add the following lines to yourapp.yaml file:

  env_variables:       REDIS_HOST: '10.112.12.112'       REDIS_PORT: '6379'

Python

For example, add the following lines to yourapp.yaml file:

env_variables:REDISHOST:'10.112.12.112'REDISPORT:'6379'

Ruby

For example, add the following lines to yourapp.yaml file:

env_variables:REDISHOST:'10.112.12.112'REDISPORT:'6379'

Importing Redis and creating the client

Go

After you define theREDISHOST andREDISPORT environment variables, use the following lines to import the redigo library, create a connection pool, and then retrieve a Redis client from the pool:

// Command redis is a basic app that connects to a managed Redis instance.packagemainimport("fmt""log""net/http""os""github.com/gomodule/redigo/redis")varredisPool*redis.PoolfuncincrementHandler(whttp.ResponseWriter,r*http.Request){conn:=redisPool.Get()deferconn.Close()counter,err:=redis.Int(conn.Do("INCR","visits"))iferr!=nil{http.Error(w,"Error incrementing visitor counter",http.StatusInternalServerError)return}fmt.Fprintf(w,"Visitor number: %d",counter)}funcmain(){redisHost:=os.Getenv("REDISHOST")redisPort:=os.Getenv("REDISPORT")redisAddr:=fmt.Sprintf("%s:%s",redisHost,redisPort)constmaxConnections=10redisPool=&redis.Pool{MaxIdle:maxConnections,Dial:func()(redis.Conn,error){returnredis.Dial("tcp",redisAddr)},}http.HandleFunc("/",incrementHandler)port:=os.Getenv("PORT")ifport==""{port="8080"}log.Printf("Listening on port %s",port)iferr:=http.ListenAndServe(":"+port,nil);err!=nil{log.Fatal(err)}}

Java

When you use the Jedis library, we recommend that you create aJedisPool, and then use the pool to create a client. The following lines of code use theredis.host andredis.port environment variables you defined earlier to create a pool:

packagecom.example.redis;importjava.io.IOException;importjava.util.Properties;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importjavax.servlet.annotation.WebListener;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;@WebListenerpublicclassAppServletContextListenerimplementsServletContextListener{privatePropertiesconfig=newProperties();privateJedisPoolcreateJedisPool()throwsIOException{Stringhost;Integerport;config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));host=config.getProperty("redis.host");port=Integer.valueOf(config.getProperty("redis.port","6379"));JedisPoolConfigpoolConfig=newJedisPoolConfig();// Default : 8, consider how many concurrent connections into Redis you will need under loadpoolConfig.setMaxTotal(128);returnnewJedisPool(poolConfig,host,port);}@OverridepublicvoidcontextDestroyed(ServletContextEventevent){JedisPooljedisPool=(JedisPool)event.getServletContext().getAttribute("jedisPool");if(jedisPool!=null){jedisPool.destroy();event.getServletContext().setAttribute("jedisPool",null);}}// Run this before web application is started@OverridepublicvoidcontextInitialized(ServletContextEventevent){JedisPooljedisPool=(JedisPool)event.getServletContext().getAttribute("jedisPool");if(jedisPool==null){try{jedisPool=createJedisPool();event.getServletContext().setAttribute("jedisPool",jedisPool);}catch(IOExceptione){// handle exception}}}}

To create a client from the pool, use theJedisPool.getResource() method. For example:

packagecom.example.redis;importjava.io.IOException;importjava.net.SocketException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;@WebServlet(name="Track visits",value="")publicclassVisitCounterServletextendsHttpServlet{@OverridepublicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsIOException{try{JedisPooljedisPool=(JedisPool)req.getServletContext().getAttribute("jedisPool");if(jedisPool==null){thrownewSocketException("Error connecting to Jedis pool");}Longvisits;try(Jedisjedis=jedisPool.getResource()){visits=jedis.incr("visits");}resp.setStatus(HttpServletResponse.SC_OK);resp.getWriter().println("Visitor counter: "+String.valueOf(visits));}catch(Exceptione){resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());}}}

Node.js

After you've defined theREDISHOST andREDISPORT environment variables, you can use the following lines to import the node_redis library and create a Redis client:

'use strict';consthttp=require('http');constredis=require('redis');constREDISHOST=process.env.REDISHOST||'localhost';constREDISPORT=process.env.REDISPORT||6379;constclient=redis.createClient(REDISPORT,REDISHOST);client.on('error',err=>console.error('ERR:REDIS:',err));// create a serverhttp.createServer((req,res)=>{// increment the visit counterclient.incr('visits',(err,reply)=>{if(err){console.log(err);res.status(500).send(err.message);return;}res.writeHead(200,{'Content-Type':'text/plain'});res.end(`Visitor number:${reply}\n`);});}).listen(8080);

PHP

After you've defined theREDIS_HOST andREDIS_PORT environment variables, you can use the following lines to create a Redis client:

<?php/** * Copyright 2019 Google Inc. * * Licensed 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. */// Only serve traffic from "/"switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {    case '/':        break;    default:        http_response_code(404);        exit('Not Found');}// Connect to Memorystore from App Engine.if (!$host = getenv('REDIS_HOST')) {    throw new Exception('The REDIS_HOST environment variable is required');}# Memorystore Redis port defaults to 6379$port = getenv('REDIS_PORT') ?: '6379';try {    $redis = new Redis();    $redis->connect($host, $port);} catch (Exception $e) {    return print('Error: ' . $e->getMessage());}$value = $redis->incr('counter');printf('Visitor number: %s', $value);

Python

After you define theREDISHOST andREDISPORT environment variables, use the following lines to import the redis-py library and create a client:

importredisredis_host=os.environ.get('REDISHOST','localhost')redis_port=int(os.environ.get('REDISPORT',6379))redis_client=redis.Redis(host=redis_host,port=redis_port)

If you've used an older version of redis-py for other apps, you might have used theStrictClient class instead ofClient. However, redis-py nowrecommendsClient instead ofStrictClient.

Ruby

No additional information for this runtime.

Using Redis commands to store and retrieve data in the cache

While the Memorystore Redis databasesupports mostRedis commands, you only need to use a few commands tostore and retrieve data from the cache. The following table suggests Rediscommands you can use to cache data. To see how to call these commands from yourapp, view your client library's documentation.

TaskRedis command
Create an entry in the data cache and
set an expiration time for the entry
SETNX
MSETNX
Retrieve data from the cacheGET
MGET
Replace existing cache valuesSET
MSET
Increment or decrement numeric cache valuesINCR
INCRBY
DECR
DECRBY
Delete entries from the cacheDEL
UNLINK
Support concurrent interactions with the cacheSee details aboutRedis transactions.

For Python, the redis-py client library requires all transactions to occur in apipeline.

Testing your updates

When you test your app locally, consider running a local instance ofRedis to avoid interacting with production data (Memorystore doesn'tprovide an emulator). To install and run Redis locally, follow the directions in theRedis documentation.Note that it currently isn't possible to run Redis locally on Windows.

For more information about testing your apps, seeTesting and deploying your application.

Deploying your app

Once your app is running in the local development server without errors:

  1. Test the app on App Engine.

  2. If the app runs without errors,use traffic splitting to slowlyramp up traffic for your updated app. Monitor the app closely for anydatabase issues before routing more traffic to the updated app.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.