GPS, cập nhật vị trí, tối ưu dữ liệuGPS, location updates, data volume
Giờ tan tầm Sài GònSaigon rush hour.17 giờ 30, 200.000 tài xế của Xe Nhanh cùng online, mỗi máy gửi một điểm GPS sau 4 giây. Hàng chục nghìn khách đang nhìn chấm xe nhích dần trên bản đồ.At 5:30 p.m., 200,000 Xe Nhanh drivers are online, each phone sending a GPS point every 4 seconds. Tens of thousands of riders watch a car dot creep across their map.
Hãy theo một điểm GPS của tài xế d-881 lúc 17 giờ 30: nó nặng 28 byte, đi qua broker MQTT, xếp hàng trong Kafka theo tài xế rồi được processor tách ra ba nơi. Redis GEO giữ vị trí mới nhất để tìm xe, TimescaleDB giữ lộ trình để tính cước, còn pub/sub chỉ báo cho đúng khách đang xem chuyến. Chạy luồng một điểm trước, rồi bật “Điểm đến muộn” và “Tìm xe gần” để xem cách hệ thống chống xe giật lùi và lọc tài xế ma.Follow one GPS point from driver d-881 at 5:30 p.m.: 28 bytes that pass an MQTT broker, queue in Kafka by driver, and get split by the processor into three places. Redis GEO keeps the latest position for search, TimescaleDB keeps the route for fares, and pub/sub notifies only the rider watching that trip. Play the single point first, then turn on “Late points” and “Nearby search” to see how the system stops cars jumping backwards and filters ghost drivers.
1Tài xế d-881 đang chở khách trong chuyến T-5521. App gửi một điểm protobuf 28 byte với timestamp từ GPS và số thứ tự.Driver d-881 is on trip T-5521. The app sends a 28-byte protobuf point with the GPS timestamp and a sequence number.
Không ghi từng điểm vào Postgres. Kafka làm bộ đệm, partition theo driver_id; Redis chỉ giữ một dòng cho mỗi tài xế; lịch sử ghi theo lô bằng COPY. Một instance Redis xử lý khoảng 100k lệnh/giây, nên tách cluster theo thành phố.Never write each point to Postgres. Kafka buffers, partitioned by driver_id; Redis keeps one row per driver; history is written in batches with COPY. One Redis instance handles about 100k commands a second, so shard the cluster by city.
Mạng di động giao muộn và giao lại. So ts (và seq) với điểm mới nhất trong cùng script Lua để đọc-so-ghi là nguyên tử; điểm cũ bị loại khỏi luồng live nhưng vẫn vào lịch sử. Dùng ts của GPS, không dùng giờ server nhận.Mobile networks deliver late and deliver twice. Compare ts (and seq) with the latest point inside one Lua script so read-compare-write is atomic; stale points leave the live path but still go to history. Use the GPS ts, not the server's receive time.
GEOADD lưu vào một sorted set, từng thành viên không có TTL. Giữ thêm seen:{city} (ZSET theo last_seen), job mỗi 10 giây ZRANGEBYSCORE rồi ZREM những tài xế im lặng quá 60 giây; truy vấn cũng lọc theo last_seen.GEOADD stores into a sorted set, and members have no TTL. Keep seen:{city} as a ZSET by last_seen; a job every 10 seconds runs ZRANGEBYSCORE and ZREMs drivers silent for over 60 seconds, and queries filter on last_seen too.
Không broadcast mọi điểm. Mỗi chuyến một kênh trip:{id}; gateway chỉ subscribe kênh của các WebSocket nó đang giữ. Giới hạn 1 cập nhật mỗi giây cho mỗi người xem, client tự nội suy chuyển động giữa hai điểm.Never broadcast every point. Each trip has a channel trip:{id}; a gateway subscribes only to channels of WebSockets it holds. Cap updates at one a second per watcher and let the client interpolate between points.
4.3 tỷ điểm một ngày. TimescaleDB chia chunk theo giờ, nén chunk cũ hơn 1 ngày (giảm khoảng 90%), continuous aggregate 1 phút cho báo cáo, retention xoá dữ liệu thô sau 7 ngày. Lộ trình chuyến được đơn giản hoá (Douglas-Peucker) và lưu riêng.4.3 billion points a day. TimescaleDB chunks by hour, compresses chunks older than a day (about 90% smaller), keeps a 1-minute continuous aggregate for reporting, and drops raw data after 7 days. Trip routes are simplified (Douglas-Peucker) and stored separately.
Tần suất thích ứng: 4 giây khi đang chạy, 15–30 giây khi đứng yên, bỏ điểm dịch dưới 10 m. Gửi protobuf, delta encoding so với điểm trước, gom 3–5 điểm một lần khi không trong chuyến. MQTT giữ một kết nối nhẹ, keepalive 60 giây, thay vì mở HTTPS mỗi lần.Adaptive interval: every 4 seconds while moving, 15–30 seconds when stopped, skip moves under 10 m. Send protobuf with delta encoding against the previous point, and batch 3–5 points when not on a trip. MQTT keeps one light connection with a 60-second keepalive instead of a new HTTPS call each time.
Biển số, loại xe, ảnh tài xế không đi kèm từng điểm. Điểm chỉ mang driver_id và toạ độ; thông tin xe là đối tượng dùng chung, client tải một lần khi bắt đầu chuyến và cache lại.Plate, car model and driver photo never travel with each point. A point carries only driver_id and coordinates; vehicle details are a shared object the client loads once at trip start and caches.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
drv:{city}:{driver_id} (Redis hash) | ts, lat, lng, heading, speed, status, trip_id | Vị trí mới nhất, EXPIRE 120 s; hash tag {city} để cùng slot với geo:{city} trong script LuaLatest position, EXPIRE 120 s; the {city} hash tag keeps it in the same slot as geo:{city} for the Lua script |
geo:{city} · seen:{city} (Redis ZSET) | member = driver_id, score = geohash | last_seen_ms | GEOSEARCH cho tìm xe gần; seen dùng để quét tài xế im lặngGEOSEARCH for nearby drivers; seen is used to sweep silent drivers |
driver_location (hypertable) | driver_id, ts, lat, lng, speed, heading, accuracy_m, trip_id | Chunk 1 giờ, nén sau 1 ngày, giữ thô 7 ngày; index (driver_id, ts DESC)1-hour chunks, compressed after a day, raw kept 7 days; index (driver_id, ts DESC) |
driver_location_1m (continuous aggregate) | driver_id, bucket, first(lat,lng), last(lat,lng), avg_speed, points | Downsample 1 phút, giữ 1 năm cho báo cáo và tranh chấp1-minute downsample kept for a year for reporting and disputes |
LocationProcessor (Java) và Processor (Go) là hộp Location Processor ở sơ đồ: HistoryWriter ghi mọi điểm vào hộp TimescaleDB, còn luồng live chỉ publish lên hộp Redis Pub/Sub khi điểm mới hơn điểm đã lưu. update_latest.lua chạy bên trong hộp Redis GEO, so sánh và ghi trong một lần gọi để nguyên tử.LocationProcessor (Java) and Processor (Go) are the Location Processor box in the diagram: HistoryWriter writes every point to the TimescaleDB box, while the live path publishes to the Redis Pub/Sub box only when the point is newer than the stored one. update_latest.lua runs inside the Redis GEO box, comparing and writing in one call so it is atomic.
package com.example.tracking;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class LocationProcessor {
private static final Duration MAX_LIVE_AGE = Duration.ofSeconds(30);
private final StringRedisTemplate redis;
private final RedisScript<Long> updateLatest; // update_latest.lua
private final HistoryWriter history; // COPY into TimescaleDB
private final Clock clock;
@KafkaListener(topics = "location.raw", batch = "true", concurrency = "8")
public void onBatch(List<LocationPoint> points) {
history.copy(points); // every point, original ts
Instant now = clock.instant();
for (LocationPoint p : points) {
if (Duration.between(p.ts(), now).compareTo(MAX_LIVE_AGE) > 0) continue;
String tag = "{" + p.city() + "}";
Long applied = redis.execute(updateLatest,
List.of("drv:" + tag + ":" + p.driverId(), "geo:" + tag, "seen:" + tag),
p.driverId(), String.valueOf(p.lng()), String.valueOf(p.lat()),
String.valueOf(p.ts().toEpochMilli()));
if (applied == null || applied == 0) continue; // older than the latest
p.tripId().ifPresent(trip ->
redis.convertAndSend("trip:" + trip, p.toWireJson()));
}
}
}package main
import (
"context"
"time"
"github.com/redis/go-redis/v9"
"github.com/segmentio/kafka-go"
)
type Processor struct {
rdb *redis.ClusterClient
latest *redis.Script // update_latest.lua
history *HistoryWriter
maxAge time.Duration
}
func (p *Processor) Run(ctx context.Context, r *kafka.Reader) error {
for {
batch, err := fetchBatch(ctx, r, 500, 200*time.Millisecond)
if err != nil {
return err
}
points := decode(batch) // JSON here (protobuf on the wire in production); malformed points are counted and skipped
if err := p.history.Copy(ctx, points); err != nil {
return err // offsets not committed: the batch is read again
}
for i := range points {
if time.Since(points[i].Time()) <= p.maxAge {
p.applyLive(ctx, &points[i])
}
}
if err := r.CommitMessages(ctx, batch...); err != nil {
return err
}
}
}
func (p *Processor) applyLive(ctx context.Context, pt *Point) {
tag := "{" + pt.City + "}"
keys := []string{"drv:" + tag + ":" + pt.DriverID, "geo:" + tag, "seen:" + tag}
ok, err := p.latest.Run(ctx, p.rdb, keys, pt.DriverID, pt.Lng, pt.Lat, pt.TsMillis).Int()
if err != nil || ok == 0 {
return // stale point, or a Redis hiccup the next point repairs
}
if pt.TripID != "" {
p.rdb.Publish(ctx, "trip:"+pt.TripID, pt.WireJSON())
}
}| MQTT (hoặc WebSocket)MQTT (or WebSocket) | HTTP batchBatched HTTP | |
|---|---|---|
| Độ trễLatency | Dưới 1 giây, kết nối luôn mởUnder a second over a connection that stays open | Bằng chu kỳ gom lô, thường 10–30 giâyAs long as the batching interval, usually 10–30 seconds |
| Pin, dataBattery, data | Header nhỏ, keepalive tốn ít; cần xử lý reconnectSmall headers, cheap keepalives; reconnects must be handled | Mỗi request tốn TLS và header, nhưng radio được nghỉ giữa các lôEach request pays for TLS and headers, but the radio rests between batches |
| Phía serverServer side | Cần broker (EMQX, HiveMQ, AWS IoT) giữ hàng trăm nghìn kết nốiNeeds a broker (EMQX, HiveMQ, AWS IoT) holding hundreds of thousands of connections | API stateless sau load balancer, dễ vận hànhA stateless API behind a load balancer, easy to run |
| Chọn khiPick it when | Đang trong chuyến, khách nhìn bản đồOn a trip, with a rider watching the map | Tài xế rảnh, chỉ cần vị trí gần đúng để ghép chuyếnThe driver is idle and matching needs only a rough position |
Thành viên Redis GEO không tự hết hạn, nên tài xế mất mạng vẫn hiện là rảnh và bị ghép chuyến. Lưu last_seen trong một sorted set, lọc điểm cũ hơn 30 giây khi tìm kiếm và chạy job ZREMRANGEBYSCORE dọn mỗi phút. Theo dõi tỉ lệ ghép chuyến bị tài xế không phản hồi để phát hiện sớm.Redis GEO members never expire, so a driver who lost signal still shows as free and gets matched. Keep last_seen in a sorted set, filter anything older than 30 seconds at search time, and run a ZREMRANGEBYSCORE cleanup every minute. Track the rate of matches the driver never answers to catch it early.
Dùng thời điểm broker nhận để so mới cũ thì lô điểm gửi lại sau hầm sẽ trông như mới nhất và kéo xe lùi 2 phút. So bằng ts từ GPS kèm seq tăng dần do app sinh, và từ chối ts lệch quá 5 phút so với giờ máy chủ. Đếm số điểm stale mỗi phút theo phiên bản app để bắt máy có đồng hồ sai.Comparing by broker receive time makes the batch replayed after a tunnel look newest and pulls the car back 2 minutes. Compare by GPS ts plus an app-generated increasing seq, and reject ts more than 5 minutes off server time. Count stale points per minute by app version to catch phones with bad clocks.
Gửi toàn bộ 50.000 điểm mỗi giây tới mọi WS gateway làm băng thông và CPU tăng theo số gateway nhân số điểm. Mỗi gateway chỉ SUBSCRIBE kênh trip:{id} của các kết nối nó đang giữ và UNSUBSCRIBE khi khách rời màn hình. Với hàng trăm nghìn kênh, cân nhắc Redis Cluster sharded pub/sub hoặc Kafka theo vùng.Sending all 50,000 points a second to every WS gateway makes bandwidth and CPU grow with gateways times points. Each gateway SUBSCRIBEs only to trip:{id} channels for connections it holds and UNSUBSCRIBEs when the rider leaves the screen. With hundreds of thousands of channels, consider Redis Cluster sharded pub/sub or regional Kafka.
Đặt mọi tài xế vào một sorted set khiến một shard Redis gánh toàn bộ ghi và GEOSEARCH quét nhiều phần tử hơn cần thiết. Chia key theo thành phố hoặc ô geohash và dùng hash tag {hcm} để các key liên quan nằm cùng slot cho script Lua. Theo dõi ops/s của từng shard, vì giờ cao điểm Hà Nội và Sài Gòn có thể dồn vào cùng một node.Putting every driver in one sorted set makes one Redis shard take all writes and makes GEOSEARCH scan more members than needed. Split keys by city or geohash cell and use a {hcm} hash tag so related keys share a slot for the Lua script. Watch per-shard ops/s, since Hanoi and Saigon rush hours can land on the same node.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
Để mọi điểm của một tài xế đi qua cùng một partition và một consumer, giữ đúng thứ tự gửi và không cần khoá giữa các processor. 128 partition đủ cho 50k điểm/giây và cho phép tăng consumer sau này.So every point from one driver goes through one partition and one consumer, keeping send order without locks between processors. 128 partitions handle 50k points a second and leave room to add consumers.
Kafka giữ thứ tự nhận, không phải thứ tự GPS. Điện thoại gửi lại lô cũ sau khi mất mạng, MQTT QoS1 có thể giao trùng, và rebalance có thể làm đọc lại. So ts (hoặc seq) với điểm đã lưu là cách duy nhất bảo đảm vị trí không đi lùi.Kafka keeps arrival order, not GPS order. Phones resend old batches after an outage, MQTT QoS1 can deliver twice, and a rebalance can re-read records. Comparing ts (or seq) with the stored point is the only way to keep the position from going backwards.
GEOSEARCH trên sorted set geohash là O(N+log M) với N là số điểm trong vùng, thường dưới 1 ms. Tách key theo thành phố để mỗi set nhỏ, đọc từ replica, giới hạn COUNT. Khi cần lọc phức tạp hơn thì dùng chỉ mục H3 hoặc S2 theo ô.GEOSEARCH on a geohash sorted set is O(N+log M), where N is the points in the area, usually under 1 ms. Split keys by city so each set stays small, read from replicas and cap COUNT. For richer filtering, use an H3 or S2 cell index.
Tần suất thích ứng theo tốc độ và trạng thái chuyến, bỏ điểm dịch dưới 10 m, client nội suy giữa hai điểm, server giới hạn 1 cập nhật/giây cho mỗi người xem. Tài xế rảnh gửi 15–30 giây một lần là đủ cho ghép chuyến.Adapt the interval to speed and trip state, skip moves under 10 m, let the client interpolate between points, and cap updates at one a second per watcher. Idle drivers reporting every 15–30 seconds is enough for matching.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.