Designing a batched GPS ingestion API

A driver app on the fleet platform I built records GPS points throughout a shift. Sending each one as its own request wastes battery and data, and falls apart the moment the phone drives into a dead zone. So the app buffers points locally and uploads them in batches — up to 500 per call. Getting that endpoint right took more thought than I expected. These are the decisions that mattered.

Give every point an identity

Mobile networks are unreliable, so the app will resend a batch the server already stored, because the response was lost. The fix is for each point to carry an ID the device generates once, when the point is recorded:

POST /api/v1/tracking/points
{
  "deviceId": "a3f6…",
  "points": [
    { "clientPointId": "8c1e…", "recordedAt": "2026-11-18T07:42:11Z",
      "lat": 51.4545, "lng": -0.9781, "accuracy": 6.5, "speed": 12.4, "heading": 270 }
  ]
}

A unique index on (DeviceId, ClientPointId) makes the insert idempotent: send the same batch ten times and you store each point once.

Cap the batch, and say so

A fixed limit keeps request size and database transaction time predictable. Over the limit, return 413 or 400 with a message that states the maximum, so the client splits the batch rather than retrying forever.

Insert in one round trip

Five hundred single-row INSERTs is slow. One approach that works well on SQL Server is to pass the batch as a table-valued parameter and let a stored procedure insert only the rows that aren't already there:

CREATE TYPE dbo.GpsPointList AS TABLE (
    ClientPointId UNIQUEIDENTIFIER NOT NULL,
    RecordedAt    DATETIME2        NOT NULL,
    Lat           DECIMAL(9,6)     NOT NULL,
    Lng           DECIMAL(9,6)     NOT NULL,
    Accuracy      REAL             NULL,
    Speed         REAL             NULL,
    Heading       REAL             NULL
);
GO
CREATE PROCEDURE dbo.GpsPoints_InsertBatch
    @CompanyId INT, @DeviceId NVARCHAR(64), @Points dbo.GpsPointList READONLY
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO dbo.GpsPoints (CompanyId, DeviceId, ClientPointId, RecordedAt, Lat, Lng, Accuracy, Speed, Heading)
    SELECT @CompanyId, @DeviceId, p.ClientPointId, p.RecordedAt, p.Lat, p.Lng, p.Accuracy, p.Speed, p.Heading
    FROM @Points p
    WHERE NOT EXISTS (SELECT 1 FROM dbo.GpsPoints g
                      WHERE g.DeviceId = @DeviceId AND g.ClientPointId = p.ClientPointId);

    SELECT @@ROWCOUNT AS Inserted;
END

From C#, Dapper's AsTableValuedParameter on a DataTable does the job. The unique index stays as the final guard in case two copies of the same batch race each other.

Validate without rejecting the whole batch

One bad point shouldn't cost the driver the other 499. Drop points that are clearly wrong — coordinates out of range, timestamps in the future, very poor accuracy — and count them in a response along these lines rather than failing the request:

{ "received": 500, "inserted": 488, "duplicates": 9, "rejected": 3 }

The app can then clear its buffer up to the last batch the server acknowledged. Duplicates count as acknowledged — the server has them.

Order by when it happened, not when it arrived

After a dead zone, a phone uploads ten minutes of old points after the office has already seen newer ones from a hardware tracker. Two rules keep this sane:

  • Store and query by RecordedAt, never by arrival time. Route playback sorts on it.
  • Update the vehicle's "current position" only if the incoming point is newer than the stored one. On the fleet platform, a position service compares phones, Traccar and GPSLive trackers this way and shows the freshest.

Scope and authenticate

The endpoint uses the same JWT and refresh-token flow as the rest of the driver API. The company and device come from the token, never from the request body, so one company's phone can't write points into another company's fleet.

Summary

  • Device-generated point IDs plus a unique index make retries harmless.
  • A fixed batch size keeps requests and transactions predictable.
  • A table-valued parameter turns a batch into one database round trip.
  • Partial acceptance with counts beats all-or-nothing.
  • "Latest position" moves forward only.
Share This Post
GM
Ghulam Murtaza

Ghulam Murtaza

Senior Full Stack .NET developer in Lahore with 6+ years building web platforms, REST APIs and mobile back-ends on ASP.NET Core, C# and SQL Server.

Related Posts

Idempotent Stripe webhooks in ASP.NET Core
Idempotent Stripe webhooks in ASP.NET Core
Read More
Multi-tenant isolation with EF Core global query filters
Multi-tenant isolation with EF Core global query filters
Read More
Newsletter

Get the latest posts delivered to your inbox