Design a Google Sheets Sync Service
Accept row writes for many Google Sheets through an API, keep every sheet under its 60-writes-per-minute limit, survive long Google outages without losing or duplicating rows, and let callers track each request.
Problem statement
Design a backend service that accepts a Google Sheet ID and a payload through an API and writes that data into the given Google Sheet.
For this exercise, assume the callers are background automations in a business software product, such as a workflow that adds a row to a customer's sheet every time a form is submitted. Each call names one connected sheet and asks to append a batch of rows to one tab of it.
The service already holds a working Google credential for every connected sheet, so connecting a sheet and signing in to Google are not part of this design. Rows sent to the same sheet must appear in that sheet in the order the service accepted the requests.
The service must never send more than 60 write requests per minute to any one Google Sheet. For this exercise, assume this is the Google Sheets API's write quota for one sheet, and Google answers HTTP 429 when it is exceeded.
Traffic is uneven: most sheets get a few rows an hour, but a busy sheet can receive far more than 60 requests in a minute. Requests for different sheets must not slow each other down.
The Google Sheets API can also be slow, return errors, or be unavailable for a long time. An accepted request must never be lost, and its rows must never be written twice.
Callers can wait minutes for their rows to be written, but they must not hold a connection open while the sync runs, and they must be able to find out what happened to every request they sent. For this exercise, assume the Sheets API can apply several writes to one sheet in a single call, all or nothing.
Reading data back from sheets, two-way sync, editing or deleting existing rows, connecting sheets, and the callers' own workflow engine are out of scope.
What to build
- A caller submits a sheet ID, a tab, and a batch of rows, and quickly gets back a request ID without waiting for the rows to be written; a retried submission never creates a second request.
- The service writes each accepted request's rows into its sheet, in acceptance order for that sheet, without ever sending more than 60 write requests per minute to one sheet.
- A sheet that is over its limit, backed up, or failing does not delay requests for other sheets.
- When the Google Sheets API is slow, rate limited, or down, accepted requests wait and are retried until they succeed or fail for good; none is lost and no row is written twice.
- A caller can look up any request and see whether it is PENDING, PROCESSING, SUCCEEDED, or FAILED, with the reason for a failure.
At scale
- For this exercise, assume about 500,000 connected sheets, of which about 20,000 receive at least one request in a busy minute.
- For this exercise, assume a peak of about 3,000 incoming sync requests per second across all sheets, each with at most 500 rows and 256 KB of data.
- For this exercise, assume one busy sheet can receive up to 2,000 requests in a single minute, while its limit stays 60 writes per minute.
- For this exercise, assume the Google Sheets API can be fully unavailable for up to 2 hours, and 99 percent of requests for a sheet that is under its limit should be written within 10 seconds when Google is healthy.