Google App Engine : App Caching
-
App Server loads an app's code and resource files ---> keeps them in memory to serve multiple requests.
-
"app cache" differs slightly between different languages (example below in NodeJS)
-
the application instance is "running" on the server and responding to requests as events.
-
app can store code and data in memory (expects it to be available for subsequent requests handled by the app instance)
-
App caching is a mechanisms for speeding up requests
-
NO guarantee that the requests from the same client reach the same server
-
NO guarantee that an app instance will stay in memory for any particular amount of time.
-
LAUNCHING NEW INSTANCES: If the app is receiving many requests, App Engine may start new instances on additional servers to spread the load.
-
REMOVING APP INSTANCE: If an app instance does not receive a request in a while, the server may purge it from memory to free resources for other apps. Each server frees the least recently used app when it needs more resources.
How to do App Caching
-
All of the application's memory is retained in the cache, so using the app cache is a simple matter of storing values in global variables.
-
No guarantees for how apps are cached or how requests are distributed, the app cache is not a good choice for persisting data that isn't local to the app instance.
NodeJS w/Express and App Caching on GAE
Alternatives to Implement Application Caching:
-
Cache Control Headers: You can control how your application handles caching by setting appropriate HTTP headers in your Node.js Express app. The most common caching headers are Cache-Control, ETag, and Expires.
-
Google App Engine app.yaml Configuration: Google App Engine also allows you to configure static content caching through the app.yaml file for static files like images, CSS, and JavaScript. These files can be cached at the client-side or in the CDN.
1. Setting Cache Headers in Node.js Express App:
You can set cache headers in your Express routes to control how caching behaves for dynamic content.
Example Code:
const express = require('express'); const app = express(); // Middleware to set Cache-Control headers app.use((req, res, next) => { res.set('Cache-Control', 'public, max-age=3600'); // Cache for 1 hour next(); }); // Example route app.get('/', (req, res) => { res.send('This is a cacheable response!'); }); app.get('/no-cache', (req, res) => { res.set('Cache-Control', 'no-store'); // No caching for this route res.send('This route is not cacheable.'); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); |
Explanation:
-
res.set('Cache-Control', 'public, max-age=3600'): This sets the Cache-Control header on every response, instructing clients and intermediate caches to store the response for 3600 seconds (1 hour).
-
public: Indicates that the response may be cached by any cache (including shared caches like CDNs).
-
no-store: Disables caching for the /no-cache route.
You can also customize caching behavior per route depending on the content you want to cache.
2. Setting Static Content Caching in app.yaml:
For static files like images, CSS, and JavaScript, you can configure caching directly in your app.yaml file to instruct Google App Engine to handle the caching for you.
Example app.yaml Configuration:
runtime: nodejs14 handlers: # Serve static files with caching - url: /static static_dir: static http_headers: Cache-Control: "public, max-age=86400" # Cache static files for 1 day (86400 seconds) # Serve dynamic routes through Node.js - url: /.* script: auto |
Explanation:
-
http_headers: Adds HTTP caching headers to responses served from the /static directory. In this case, it caches all static files for 1 hour (max-age=3600).