Translate

Real-Time Tracking Backend System Design in Go Like Uber and Swiggy with GPS and WebSockets

कल्पना करें कि आपके food delivery platform पर एक साथ 5 लाख customers अपने orders track कर रहे हैं। हर delivery partner हर 2 सेकंड में GPS location भेज रहा है। अब backend को location ingest करनी है, latest position store करनी है और सही customer तक update पहुँचाना है। अगर हर GPS update पर database में भारी write और सभी connected clients को broadcast किया जाए, तो latency बढ़ सकती है और infrastructure पर अनावश्यक load पड़ेगा।

इस समस्या का समाधान केवल तेज API बनाने में नहीं है। आपको ingestion, location processing, storage और real-time delivery को अलग-अलग design करना होगा। Go की Goroutines, efficient networking और concurrency primitives इस architecture में मदद कर सकते हैं, लेकिन सही partitioning, backpressure और connection management उतने ही जरूरी हैं।

Key Takeaways: Real-Time Tracking के लिए क्या जरूरी है?

  • Location Ingestion: GPS updates को validate करके स्वीकार करें।
  • Redis: Latest location को fast lookup के लिए store करें।
  • Kafka या समान event broker: Location events को asynchronously process करें।
  • WebSocket: Customer application तक live updates पहुँचाएँ।
  • Security: हर customer को केवल अपने authorized order की location दिखाएँ।

Uber या Swiggy जैसे Tracking System की Architecture कैसी होगी?

Delivery Partner Mobile App
|
v
Go Location API
|
v
Authentication + Validation
|
v
Event Broker (Kafka)
|
v
Location Processing Workers
|
+------> Redis (Latest Location)
|
+------> Database (History / Audit)
|
v
Tracking / Fan-out Service
|
v
WebSocket Gateway
|
v
Customer Mobile App

यह architecture ingestion और delivery को अलग रखता है। Kafka जैसे event broker से temporary processing delays को absorb करने में मदद मिल सकती है। Redis latest location की तेज lookup सुविधा देता है, जबकि persistent database historical records के लिए इस्तेमाल हो सकता है। WebSocket Gateway लंबे समय तक connected clients को updates भेजता है।

Go में GPS Location API कैसे Implement करें?

पहला कदम एक छोटा ingestion endpoint बनाना है। Production implementation में authentication, schema validation, rate limiting और timestamp checks भी अनिवार्य होंगे।

package main

import (
"encoding/json"
"log"
"net/http"
"time"
)

type Location struct {
OrderID string `json:"order_id"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
SentAt int64 `json:"sent_at"`
}

func locationHandler(
w http.ResponseWriter,
r *http.Request,
) {
if r.Method != http.MethodPost {
http.Error(
w, "Method not allowed",
http.StatusMethodNotAllowed,
)
return
}

r.Body = http.MaxBytesReader(w, r.Body, 4096)

var loc Location
if err := json.NewDecoder(r.Body).Decode(&loc); err != nil {
http.Error(w, "Invalid JSON",
http.StatusBadRequest)
return
}

if loc.OrderID == "" ||
loc.Lat < -90 || loc.Lat > 90 ||
loc.Lng < -180 || loc.Lng > 180 {
http.Error(w, "Invalid location",
http.StatusBadRequest)
return
}

// Production: authorize order, validate timestamp,
// then publish to a bounded processing pipeline.
_ = time.UnixMilli(loc.SentAt)

w.WriteHeader(http.StatusAccepted)
}

func main() {
http.HandleFunc("/location", locationHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

यह केवल API skeleton है, complete tracking backend नहीं। Production में order ownership verify करें, stale या out-of-order updates reject करें और event broker को publish करने के बाद ही acknowledgement policy तय करें। Coordinates संवेदनशील data हैं, इसलिए authentication, authorization और retention policy को नजरअंदाज न करें।

लाखों GPS Updates को Efficiently कैसे Handle करें?

1. क्या हर Location Update को Database में लिखना चाहिए?

हर update को synchronously persistent database में लिखना latency और write load बढ़ा सकता है। Latest position को Redis में रखें और historical tracking के लिए sampling या asynchronous batch writes पर विचार करें। Business, safety या audit requirements के कारण यदि हर point सुरक्षित रखना जरूरी है, तो data loss से बचाने के लिए durable event ingestion और retention strategy बनाएं।

2. WebSocket और Polling में क्या चुनें?

Polling में client बार-बार server से पूछता है, जिससे repeated requests बढ़ सकती हैं। WebSocket persistent connection पर server को updates push करने देता है। बड़े deployments में connection gateways को horizontally scale करें और हर client को केवल संबंधित order के updates भेजें। Reconnect, heartbeat और slow-client handling भी design का हिस्सा होने चाहिए।

3. Duplicate और Out-of-Order Events कैसे रोकेंगे?

हर event के साथ device timestamp और event identifier रखें। Processing service पुराने timestamps को पहचान सकती है, जबकि event identifier duplicate processing रोकने में मदद करता है। केवल device clock पर भरोसा न करें; server-side receipt time भी record करें।

Go, Java, Node.js और Python में कौन-सा विकल्प बेहतर है?

Factor Go Java Node.js Python
Concurrency Goroutines और Channels Threads और concurrency APIs Event loop और async I/O asyncio और multiprocessing
Memory Runtime और workload पर निर्भर JVM और heap configuration पर निर्भर V8 heap और connection load पर निर्भर Objects और runtime पर निर्भर
Real-time use Tracking APIs और gateways Enterprise event services WebSocket-heavy applications Data processing और APIs
मुख्य चुनौती Goroutine lifecycle और backpressure GC और runtime tuning Event-loop blocking Workload-specific concurrency

इनमें कोई universal performance winner नहीं है। सही चुनाव team expertise, existing infrastructure, measured latency और workload profile पर निर्भर करता है।

Production में किन Metrics को Monitor करना चाहिए?

  • Ingestion: Events per second, validation failures और broker lag।
  • Latency: GPS receipt से customer delivery तक p95 और p99 delay।
  • Connections: Active WebSockets, reconnect rate और slow clients।
  • Storage: Redis latency, database write throughput और connection saturation।
  • Reliability: Dropped events, duplicate events और stale-location rate।

Final Verdict: क्या Go Real-Time Tracking के लिए सही है?

Go एक मजबूत विकल्प है क्योंकि यह concurrent network services और efficient deployment के लिए उपयोगी primitives देता है। लेकिन लाखों concurrent users संभालने के लिए केवल Goroutines पर्याप्त नहीं हैं। Event-driven processing, controlled fan-out, durable ingestion, horizontal scaling और observability को साथ में design करना होगा।

सबसे महत्वपूर्ण principle: GPS ingestion, latest-location storage और customer delivery को अलग responsibilities दें। फिर realistic load tests से capacity मापें, failure scenarios जाँचें और architecture को bottleneck के अनुसार scale करें। यही approach tracking system को responsive, reliable और operationally manageable बनाती है।

Real-time tracking systems में हजारों location updates को efficiently process करने के लिए Go Concurrency की सही समझ जरूरी है। Goroutines और Channels का उपयोग करते समय Deadlocks, Race Conditions और Goroutine Leaks जैसी समस्याओं से बचने के practical तरीके जानने के लिए हमारी यह detailed guide पढ़ें: Goroutines & Channels के 10 खतरनाक Traps: Go में Deadlocks और Race Conditions से बचने के Exact Rules .

Post a Comment

Blogger

Your Comment Will be Show after Approval , Thanks

Personal Loan - Apply Online
Personal Loan Available
Check your loan eligibility and apply online in a few simple steps.
APPLY FOR PERSONAL LOAN
Eligibility, interest rate and loan approval are subject to lender terms and conditions.

Ads

 
↑ Top