Redis Caching: Make Your API 10x Faster
What you build: an API on a slow 1-million-row database with and without a Redis cache on a free VM, load-tested (about 20x more requests per second), plus stale data and a full cache broken and fixed

✍️ Translate this blog into Hindi, Tamil or Telugu
When an API is slow because the database does the same heavy work for every request, a cache is often the biggest speed-up you can get for the least effort. Redis is the most popular one: an in-memory store that answers in well under a millisecond. In this lab you put Redis in front of a deliberately slow database on a free VM, load-test the API with and without the cache, and then hit the two problems every cache brings: stale data (the cache keeps showing old numbers) and a full cache (writes start failing). You fix both the way production teams do.
Who it is for: backend and cloud beginners who know a little Python and want to understand caching beyond "just add Redis". Time: about 1 hour. Cost: free. Everything runs on one e2-micro VM in us-east1 (Google Cloud's Always Free tier), deleted at the end.
Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output is the real result we got. Timings will differ a little on your VM.
⭐ Recommended by CareerByteCode · BytePlay courseNVIDIA GPU AI Factory: Architecture, Engineering & Operationsโน40,000Start learning →Part A: caching in plain English

- Cache: a fast copy of an answer that is slow to make. Real-life example: a shop assistant who remembers today's most asked price instead of walking to the store room every time.
- Cache hit / miss: the answer was in the cache (fast) / was not (slow, then saved).
- Cache-aside (the pattern in this lab): the app checks the cache first; on a miss it reads the database, then stores the answer in the cache.
- Key and TTL (time to live): each answer is stored under a name like
stats:p042and expires after a number of seconds (here 60), so old answers do not live forever. - Invalidation: deleting a cached answer when the data behind it changes.
- Eviction: when the cache is full, Redis can throw away keys to make room.
allkeys-lruthrows away the least recently used ones.
Our API has two versions of the same endpoint: /nocache/stats/<product> always asks the database, and /cache/stats/<product> uses Redis. The "database" is SQLite with 1 million sales rows and no index on the product column, so each question takes tens of milliseconds of real work, like a busy reporting table.
Before you start
- A Google Cloud account (the free trial works) and a project.
- Open Cloud Shell (the
>_icon at the top right of the Google Cloud console) and set your project:
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
gcloud services enable compute.googleapis.comPart B: the lab
Step 1: a free e2-micro VM
gcloud compute instances create redis-lab --zone=us-east1-b --machine-type=e2-micro \
--image-family=debian-12 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \
--no-service-account --no-scopes 2>&1 | tail -2
until gcloud compute ssh redis-lab --zone=us-east1-b --tunnel-through-iap --command="echo ssh ok" 2>/dev/null | grep -q ok; do sleep 10; done; echo "SSH works"NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
redis-lab us-east1-b e2-micro 10.142.0.8 EXTERNAL_IP RUNNING
SSH worksFrom here on: open an interactive SSH session with the VM using the command below (or Compute Engine > VM instances > SSH next to redis-lab). Once you see the VM terminal prompt (you@redis-lab:~$), run the next boxes there.
gcloud compute ssh redis-lab --zone=us-east1-b --tunnel-through-iapEverything from Step 2 runs inside the VM in ~/lab188. Make the folder and save the API below there as api.py:
mkdir -p ~/lab188 && cd ~/lab188
nano api.py # paste the code below, then Ctrl+O, Enter, Ctrl+X to save and exit# api.py: a small product API on a slow SQLite "database", with and without a Redis cache.
# GET /nocache/stats/<product> always asks the database (slow)
# GET /cache/stats/<product> asks Redis first; on a miss, asks the database and keeps the answer 60 seconds
# POST /sale/<product>/<qty> records a sale; ?invalidate=1 also deletes the cached answer
import json, sqlite3, time
from flask import Flask, request, jsonify
import redis
app = Flask(__name__)
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
DB = "shop.db"
TTL = 60
def stats_from_db(product):
con = sqlite3.connect(DB)
row = con.execute("SELECT COUNT(*), SUM(qty), ROUND(AVG(price), 2) FROM sales WHERE product = ?", (product,)).fetchone()
con.close()
return {"product": product, "orders": row[0], "units": row[1], "avg_price": row[2]}
@app.get("/nocache/stats/<product>")
def nocache(product):
t = time.perf_counter()
data = stats_from_db(product)
return jsonify(data=data, source="database", ms=round((time.perf_counter() - t) * 1000, 1))
@app.get("/cache/stats/<product>")
def cached(product):
t = time.perf_counter()
key = f"stats:{product}"
hit = r.get(key)
if hit:
return jsonify(data=json.loads(hit), source="redis", ms=round((time.perf_counter() - t) * 1000, 1))
data = stats_from_db(product)
r.setex(key, TTL, json.dumps(data))
return jsonify(data=data, source="database (now cached)", ms=round((time.perf_counter() - t) * 1000, 1))
@app.post("/sale/<product>/<int:qty>")
def sale(product, qty):
con = sqlite3.connect(DB)
con.execute("INSERT INTO sales (product, qty, price) VALUES (?, ?, 499)", (product, qty))
con.commit(); con.close()
if request.args.get("invalidate") == "1":
r.delete(f"stats:{product}")
return jsonify(saved=True, cache="deleted")
return jsonify(saved=True, cache="left as it was")Step 2: install Redis, Flask and a load tester
Why: redis-server is Redis itself (it starts at once as a service), python3-redis is the Python client, python3-flask the web framework, apache2-utils gives ab (ApacheBench) for load tests.
# Install Redis (the cache), Flask and the Redis client for Python, and ab (a load tester)
sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq redis-server python3-flask python3-redis apache2-utils sqlite3 > /dev/null 2>&1
redis-server --version | cut -d' ' -f1-3; redis-cli ping; python3 -c "import flask, redis; print('flask', flask.__version__, '| redis-py', redis.__version__)"Redis server v=7.0.15
PONG
flask 2.2.2 | redis-py 4.3.4PONG is Redis answering redis-cli ping: it is running.
Step 3: a slow database
# A "slow database": 1 million sales rows in SQLite, with no index on product (like a busy reporting table)
cd ~/lab188
python3 - <<'PY'
import sqlite3, random
random.seed(7); con = sqlite3.connect("shop.db")
con.execute("DROP TABLE IF EXISTS sales"); con.execute("CREATE TABLE sales (id INTEGER PRIMARY KEY, product TEXT, qty INT, price REAL)")
names = [f"p{i:03d}" for i in range(200)]
con.executemany("INSERT INTO sales (product, qty, price) VALUES (?, ?, ?)", ((random.choice(names), random.randint(1, 5), random.randint(99, 2999)) for _ in range(1_000_000)))
con.commit(); print("rows:", con.execute("SELECT COUNT(*) FROM sales").fetchone()[0])
PY
ls -lh shop.db | awk '{print $5, $9}'rows: 1000000
18M shop.dbStep 4: start the API and call it
Why: systemd-run starts the API as a temporary background service. We call the uncached endpoint once, then the cached one twice: the first cached call is a miss (it asks the database and stores the answer), the second is a hit.
# Start the API in the background as a temporary systemd service, then call both endpoints once
cd ~/lab188
sudo systemd-run --unit=api --working-directory=$PWD --uid=$(id -u) python3 -m flask --app api run --port 8080 > /dev/null 2>&1; sleep 3
curl -s localhost:8080/nocache/stats/p042; echo
curl -s localhost:8080/cache/stats/p042; echo
curl -s localhost:8080/cache/stats/p042; echo{"data":{"avg_price":1546.44,"orders":5048,"product":"p042","units":15041},"ms":86.1,"source":"database"}
{"data":{"avg_price":1546.44,"orders":5048,"product":"p042","units":15041},"ms":84.5,"source":"database (now cached)"}
{"data":{"avg_price":1546.44,"orders":5048,"product":"p042","units":15041},"ms":0.4,"source":"redis"}The database answer takes about 85 ms; the cached one 0.4 ms, about 200 times faster for that single call.
Step 5: look inside Redis
Why: see what the app stored. KEYS lists keys (fine on a lab, avoid it on a big production cache), GET reads one, TTL says how many seconds it has left.
# Look inside Redis: the key, its value and how long it will live (TTL)
redis-cli KEYS 'stats:*'
redis-cli GET stats:p042
redis-cli TTL stats:p042stats:p042
{"product": "p042", "orders": 5048, "units": 15041, "avg_price": 1546.44}
50
Step 6: load test with and without the cache
Why: single calls do not show what users feel under load. ab -n 300 -c 10 sends 300 requests, 10 at a time. -l tells ab that answers may differ in length (ours include a timing), so they are not counted as failures.
# Load test: 300 requests, 10 at a time, without and with the cache (-l: the answers differ in length, that is fine)
ab -q -l -n 300 -c 10 http://localhost:8080/nocache/stats/p042 | grep -E "Requests per second|Time per request.*mean\)|Failed requests"
ab -q -l -n 300 -c 10 http://localhost:8080/cache/stats/p042 | grep -E "Requests per second|Time per request.*mean\)|Failed requests"
redis-cli INFO stats | grep -E "keyspace_(hits|misses)"Failed requests: 0
Requests per second: 2.52 [#/sec] (mean)
Time per request: 3966.627 [ms] (mean)
Failed requests: 0
Requests per second: 52.16 [#/sec] (mean)
Time per request: 191.736 [ms] (mean)
keyspace_hits:302
keyspace_misses:2
On this small VM the database path handles about 2.5 requests per second (each request waits almost 4 seconds when 10 arrive together), while the cached path handles about 52 per second, roughly 20 times more. Redis counted 302 hits and just 2 misses. On a bigger machine both numbers grow, but the gap is the point: the cache takes the repeated heavy work off the database.
Break it: stale data
A new sale for p042 is saved in the database. The cached answer does not know.
# Break it: a new sale is saved, but the cached answer does not change (stale data) until the TTL runs out
curl -s localhost:8080/cache/stats/p042 | python3 -c "import json,sys; d=json.load(sys.stdin); print('before:', d['data']['orders'], 'orders from', d['source'])"
curl -s -X POST localhost:8080/sale/p042/3; echo
curl -s localhost:8080/cache/stats/p042 | python3 -c "import json,sys; d=json.load(sys.stdin); print('after: ', d['data']['orders'], 'orders from', d['source'])"
curl -s localhost:8080/nocache/stats/p042 | python3 -c "import json,sys; d=json.load(sys.stdin); print('truth: ', d['data']['orders'], 'orders from', d['source'])"before: 5048 orders from redis
{"cache":"left as it was","saved":true}
after: 5048 orders from redis
truth: 5049 orders from databaseThe cache still says 5,048 orders; the database says 5,049. Users see old numbers until the 60-second TTL runs out. For a product page that may be fine; for a stock count or a balance it is not.
Fix it: delete the cached answer on every write
Why: this is cache invalidation: when the data changes, delete its key (?invalidate=1 makes our API call DEL stats:p042 after saving). The next read is a miss, gets the fresh value and caches it.
# Fix it: delete the cached key when the data changes (cache invalidation)
curl -s -X POST "localhost:8080/sale/p042/2?invalidate=1"; echo
curl -s localhost:8080/cache/stats/p042 | python3 -c "import json,sys; d=json.load(sys.stdin); print('after: ', d['data']['orders'], 'orders from', d['source'])"
curl -s localhost:8080/nocache/stats/p042 | python3 -c "import json,sys; d=json.load(sys.stdin); print('truth: ', d['data']['orders'], 'orders from', d['source'])"{"cache":"deleted","saved":true}
after: 5050 orders from database (now cached)
truth: 5050 orders from databaseCache and database agree: 5,050. The TTL stays as a safety net for anything that changes without going through your code.
🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโน499Build the projects →Break it: the cache is full
We give Redis only 2 MB of memory with the noeviction policy (refuse writes when full), then try to write 800 values of 4 KB each.
# Break it: a small memory limit with no eviction. When Redis is full, every new write fails
redis-cli CONFIG SET maxmemory 2mb > /dev/null; redis-cli CONFIG SET maxmemory-policy noeviction > /dev/null
for i in $(seq 1 800); do redis-cli SET "junk:$i" "$(head -c 4000 /dev/zero | tr '\0' x)"; done | sort | uniq -c | sed 's/when used memory.*/.../'
redis-cli INFO memory | grep -E "^used_memory_human|^maxmemory_human|^maxmemory_policy"
curl -s localhost:8080/cache/stats/p100 | head -c 160; echo 572
228 OK
572 OOM command not allowed ...
used_memory_human:2.00M
maxmemory_human:2.00M
maxmemory_policy:noeviction
{"data":{"avg_price":1555.51,"orders":5099,"product":"p100","units":15310},"ms":972.6,"source":"database (now cached)"}
228 writes worked, then Redis was at its 2 MB limit and 572 writes failed with OOM command not allowed when used memory > 'maxmemory'. Our API's small key happened to still fit this time, but any app writing to this cache would start getting errors (in our first runs the API answered 500 Internal Server Error for exactly this reason).
Fix it: let Redis evict old keys (allkeys-lru)
Why: for a cache, losing an old entry is fine (it can always be rebuilt from the database). allkeys-lru makes Redis drop the least recently used keys to make room. (noeviction is right when Redis holds data you cannot lose, such as a queue.)
# Fix it: let Redis evict the least recently used keys (allkeys-lru), the usual setting for a cache
redis-cli CONFIG SET maxmemory-policy allkeys-lru
for i in $(seq 801 1000); do redis-cli SET "junk:$i" "$(head -c 4000 /dev/zero | tr '\0' x)"; done | sort | uniq -c
redis-cli INFO stats | grep evicted_keys
curl -s localhost:8080/cache/stats/p100 | python3 -c "import json,sys; d=json.load(sys.stdin); print('p100:', d['data']['orders'], 'orders from', d['source'])"OK
200 OK
evicted_keys:200
p100: 5099 orders from redisAll 200 new writes worked, Redis evicted 200 old keys to make room, and the API still answers from the cache.
Step 7: stop the API and reset Redis
# Stop the API and reset Redis
sudo systemctl stop api
redis-cli FLUSHALL; redis-cli CONFIG SET maxmemory 0 > /dev/null; redis-cli CONFIG SET maxmemory-policy noeviction > /dev/null
redis-cli DBSIZEOK
0POC: prove it works
- One call: about 85 ms from the database, 0.4 ms from Redis.
- Load test on a free e2-micro: about 2.5 requests per second without the cache and about 52 with it (about 20 times more), 0 failures, 302 hits and 2 misses.
- Stale data (5,048 vs 5,049) fixed by deleting the key on write.
- A full cache refused 572 of 800 writes with
noeviction;allkeys-lruaccepted every write by evicting 200 old keys.
Quiz task: check what you learned
- What does a TTL of 60 mean? (Answer: Redis deletes the key 60 seconds after it was set, so the next read is a miss.)
- A price changed but the site still shows the old one. Name two fixes. (Answer: delete the key when the price is saved, and keep a short TTL as a safety net.)
- Which eviction policy suits a cache, and which suits a job queue? *(Answer:
allkeys-lrufor a cache;noevictionfor data you must not lose.)* - Why can a cache make things worse when 1,000 users ask for the same missing key at once? (Answer: all of them miss and hit the database together (a "cache stampede"); use a lock or serve the old value while one request refreshes it.)
- Where would you run Redis on Google Cloud in production? (Answer: Memorystore for Redis, a managed service, instead of a VM.)
Clean up (delete everything)
Exit the VM (exit), then in Cloud Shell:
gcloud compute instances delete redis-lab --zone=us-east1-b --quiet 2>&1 | tail -1
echo "redis-lab VMs left: $(gcloud compute instances list --filter="name=redis-lab" --format="value(name)" | wc -l)"
echo "redis-lab disks left: $(gcloud compute disks list --filter="name~^redis-lab" --format="value(name)" | wc -l)"Deleted [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-east1-b/instances/redis-lab].
redis-lab VMs left: 0
redis-lab disks left: 0
FAQ
Is Redis only a cache? No. It is also used for sessions, rate limits, leaderboards, queues and pub/sub messaging. Those uses often need persistence and noeviction.
What should I cache? Answers that are slow to make, asked often, and allowed to be a little old: product pages, reports, search results, user profiles. Do not cache anything where a stale answer causes harm unless you invalidate reliably.
What about Memorystore? Memorystore for Redis (or Valkey) is Google Cloud's managed Redis: no VM to patch, high availability, monitoring. It is a paid service, so this free lab uses Redis on the e2-micro instead; the code is the same.
How big should the TTL be? As long as a stale answer is acceptable to the business: seconds for prices, minutes for reports, hours for static content. Add invalidation for anything changed by your own app.
Can the cache and database ever disagree after invalidation? Briefly, yes, under heavy concurrent writes. For strict needs, delete the key after the database write commits and keep a short TTL.
Why was the uncached path so slow? The free e2-micro has a small, shared CPU, and each request scans 1 million rows with no index. An index on product would also help a lot; caching and indexing solve different problems and often go together.
Troubleshooting
| You see | Why | Fix |
|---|---|---|
Could not connect to Redis at 127.0.0.1:6379 | Redis is not running | sudo systemctl start redis-server |
OOM command not allowed when used memory > 'maxmemory' | The memory limit is reached with noeviction | Raise maxmemory, or use allkeys-lru for a cache |
| Old values after an update | No invalidation, long TTL | Delete the key on write; shorten the TTL |
ab reports many Failed requests | Answers differ in length | Add -l to ab |
| Hit ratio stays low | Keys differ per request (for example, they include a timestamp) | Build keys only from what decides the answer |
| Redis memory keeps growing | Keys with no TTL | Always set a TTL (SETEX or EXPIRE) for cached values |
What next
- Put Nginx in front of the API: Nginx for Beginners: Web Server, Reverse Proxy and HTTPS.
- Watch your server under load: Monitor Linux Servers with Node Exporter and Ops Agent.
- A managed database for app data: Firestore Hands-On: A NoSQL Database for Apps.
- More hands-on posts: ByteBlog. Practice in ByteLabs and see the courses.
Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.
⭐ Keep learning with CareerByteCode
Hand-picked to go deeper on this topicGet CareerByteCode Team's next blog by email
One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.
We email you once to confirm. By subscribing you agree to the privacy policy.
Related blogs
CareerByteCode TeamData Engineering Project for Your Resume on Google Cloud
What you build: a daily batch pipeline on Cloud Storage and BigQuery with layers, data quality checks, quarantine and idempotent re-runs. Every step tested.
#bigquery
CareerByteCode Teamdbt with BigQuery: Clean Data Like an Analytics Engineer
What you build: a dbt project on BigQuery that cleans messy raw CSVs into a tested mart table; the tests really catch a duplicate order and an unknown status, then pass 11 of 11 after the fix.
#bigquery
CareerByteCode TeamSpanner Free Trial: Global SQL Database Hands-On
What you build: a free trial Cloud Spanner instance with interleaved tables, real duplicate and orphan errors, a unique index, a Python app saving an order and points in one transaction, and a stale r
#bigquery
CareerByteCode TeamBigQuery ML: Train a Model with Just SQL
What you build: a churn prediction model written in SQL with BigQuery ML, scored and used for predictions, in your own Google Cloud account. Every step tested.
💬 Comments (0)
Top · NewestSign in to comment