Prompt
How do I troubleshoot duplicate records from a dataset API?
Latest observation
Here’s a practical way to troubleshoot duplicate records coming from a dataset API.
1) Confirm where the duplicates are happening
First determine whether the duplicates are:
- Already in the source data
- Added by the API response
- Introduced by your client code when fetching, merging, or storing results
A quick test:
- Call the API directly with a tool like
curl, Postman, or browser. - Compare the raw response with what your app/database receives.
2) Identify the unique key
Figure out what should make a record unique:
iduuid- combination of fields like
(date, user_id, event_type)
Then check duplicates using that key.
Example logic:
- If two rows have the same
id, the API may be repeating records. - If
idis missing or unstable, the source may not guarantee uniqueness.
3) Check pagination behavior
Duplicate records often come from paging issues:
- Overlapping pages
- Incorrect offset/limit logic
- Records changing between requests while paginating
Things to verify:
- Are you using
page,offset,cursor, ornext_tokencorrectly? - Does the API recommend cursor-based pagination instead of offset pagination?
- Are you reusing the same page token accidentally?
If records are inserted/updated while you’re paging, offset-based pagination can repeat or skip rows.
4) Inspect filtering and sorting
Duplicates can appear if:
- Sorting is not deterministic
- Filters are inconsistent between requests
- Results are grouped or joined in a way that repeats rows
Best practice:
- Always use a stable sort order, ideally by a unique field.
- Avoid changing query parameters across paged requests.
5) Look for retry logic that reprocesses the same page
Sometimes duplicates are caused by your client:
- Retrying a request after a timeout
- Re-fetching the last successful page
- Appending results instead of replacing/upserting them
Check whether your code:
- Appends each response blindly
- Retries without deduplication
- Processes the same response multiple times
6) Compare timestamps and sync windows
If the API is incremental:
- Make sure your “since” or “updated_after” timestamp is correct
- Account for late-arriving updates
- Use a small overlap window only if you dedupe afterward
Example:
- Query data from
last_sync_time - 5 minutes - Deduplicate by record ID during ingestion
7) Check joins or expanded nested data
If the API returns related objects, duplicates may come from:
- One-to-many relationships flattened into repeated rows
- Expand/include parameters
- Backend joins without proper aggregation
In that case, repeated records may be expected and need grouping on your side.
8) Validate with a dedupe query
If you store the data in SQL, run a quick check:
SELECT id, COUNT(*)
FROM your_table
GROUP BY id
HAVING COUNT(*) > 1;
If you don’t have a single ID:
SELECT field1, field2, COUNT(*)
FROM your_table
GROUP BY field1, field2
HAVING COUNT(*) > 1;
9) Add logging
Log:
- Request URL and parameters
- Page token / offset / cursor
- Record IDs returned per request
- Retry attempts
- Record count per batch
This makes it much easier to see where duplicates first appear.
10) Implement deduplication as a safeguard
Even if you fix the root cause, add a safety net:
- Use a set/map keyed by unique ID in memory
- Use
UPSERTin the database - Deduplicate before downstream processing
Example:
- Insert only if
idnot already seen - Or update existing rows on conflict
Common root causes
- Offset pagination on changing data
- Missing stable sort order
- Client retrying pages
- API returning expanded/joined rows
- No unique identifier in the dataset
- ETL job reprocessing the same window
If you want, I can also help you debug this with:
- your API pagination style,
- a sample response, or
- the code you use to fetch/store the data.
Brands