DEV Community

Vishwa
Vishwa

Posted on Originally published at Medium

Designing a Flash Sale on GCP: CDN, Load Balancer, Cache, Queue and Pre-Scaling

How I would keep a Flipkart-style midnight sale standing when everyone taps Buy at once

It is 11:59 PM. A shopping app has announced a phone at a big discount, starting at midnight. Imagine a few lakh people with the product page open, thumbs over the Buy button.

At 12:00 they all tap. This post is how I would design a Flipkart-style app so that moment is boring. It is an illustrative design, not a description of any company's real system.

The problem

A normal day and a sale day differ in one way: the traffic arrives at the same second. Without a plan, this is what breaks:

  • Images and scripts pull bandwidth from the origin servers that should be serving checkout.
  • Bots hammer /api/checkout and take capacity from real buyers.
  • Product reads all go to the database, which is also trying to take orders.
  • Autoscaling reacts, but adding pods and then nodes takes time. At max replicas, requests queue, latency rises, and the load balancer returns 503.
  • Side work like confirmation messages and analytics runs inside the order request and makes every order slower.

The solution: one layer for each problem

The rule I follow is simple: each layer answers if it can, and only a miss goes one step deeper.

1. CDN for everything static. Product images and JS are served from the nearest edge with a one-year TTL and versioned file names, so a new image gets a new name. Cloud CDN is a switch on a backend inside the load balancer: on for the image bucket, off for cart and checkout.

2. Load balancer and Cloud Armor at the front. The load balancer is the one public entry and sends each path to a healthy backend. Cloud Armor runs on it, at the edge, and rate-limits per IP and challenges suspicious clients, so the checkout pods only see real traffic.

3. API Gateway for identity and limits. The gateway checks the token and applies a stricter rate limit on the order route. A client over the limit gets 429 and waits.

4. Redis for hot reads. The product price and details come from Redis with a 5-minute TTL plus random jitter, so thousands of keys do not expire in the same second. A "not found" result is cached for 30-60 seconds, so bots asking for missing IDs never reach the database. The payment amount and live stock at checkout are never cached; they come from Cloud SQL.

5. A gate before the database for stock. When a few units meet a huge crowd, Redis can act as a gate: DECR a stock counter, and anyone who gets a value below zero is told "sold out" immediately. Only the few who pass reach Cloud SQL, where one transaction inserts the order and reduces stock, and rolls back if anything fails. An idempotency key on the order request stops a double charge when the user retries.

6. A queue for everything that can wait. After the order commits, the service publishes order_placed to Pub/Sub and returns. Confirmation messages, invoices and the sales dashboard are subscribers. If one is slow, its backlog grows and nothing is dropped; if one fails, it retries and then goes to its dead-letter topic, without slowing the others.


7. Pre-scale before the sale, not during it. Checkout runs on GKE so the team controls pod counts and node pools. HPA adds pods when CPU passes a threshold, and Cluster Autoscaler adds nodes when pods cannot be placed, but both take time. So I would raise the minimums before midnight. Redis memory is chosen, not dynamic, so I would resize or add shards before the sale too.

Trade-offs and limits

  • Pre-scaling costs money. Warm pods and bigger Redis instances are paid for even if the sale is quieter than expected. I would size from past sale data where it exists and scale down right after.
  • A 503 "try again" is a valid answer. If capacity runs out, a fast, clear failure is better than a page that hangs.
  • The Redis gate is a hint, not the truth. If Redis fails, the database transaction still prevents overselling, just more slowly. The counter can be rebuilt from the database.
  • Cloud SQL fits one region. If stock had to be one consistent count across regions, I would look at Spanner.
  • Test it before the real day. I would run a load test at the expected peak and watch the 5xx rate, p95 latency and Pub/Sub backlog.

Takeaway

A flash sale is a normal day compressed into one second.

CDN, Armor, gateway, Redis and Pub/Sub each take load off the database.

Scale up before the sale; autoscaling alone reacts too late.

Top comments (0)