DEV Community

Dev Cookies
Dev Cookies

Posted on

๐Ÿ• How a Food Delivery App Works Behind the Scenes: Microservices, Communication, and Flow Explained

Have you ever wondered what happens behind the scenes when you order food on apps like Zomato or Swiggy?

Several microservices work together โ€” some talk synchronously, others asynchronously โ€” to ensure your biryani gets delivered hot and fast! ๐Ÿ›

Let's dive into the entire journey. ๐Ÿš€


๐Ÿ›’ Step 1: User Browses Restaurants and Selects Food

๐Ÿ”ฅ Services involved:

  • Restaurant Service
  • Menu Service
  • Search Service
  • User Profile Service

๐Ÿ“ž Communication:

  • User App โ†’ Search Service: (Sync, REST API) to search restaurants.
  • User App โ†’ Menu Service: (Sync, REST API) to fetch restaurant menus.
  • User App โ†’ User Profile Service: (Sync, REST API) to fetch saved addresses.

โœ… Why synchronous?

  • User expects immediate search results and menu listing.
  • UX needs to be fast and smooth.

๐Ÿงบ Step 2: Add Food Items to Cart

๐Ÿ”ฅ Services involved:

  • Cart Service

๐Ÿ“ž Communication:

  • User App โ†’ Cart Service: (Sync, REST API) to add items or view cart.

โœ… Why synchronous?

  • Real-time feedback needed when user adds/removes dishes.

๐Ÿ’ณ Step 3: Place Order and Make Payment

๐Ÿ”ฅ Services involved:

  • Order Service
  • Payment Service
  • Coupon/Discount Service

๐Ÿ“ž Communication:

  • User App โ†’ Coupon Service: (Sync, REST API) to validate discounts.
  • User App โ†’ Payment Service: (Sync, REST API) to process payment.
  • User App โ†’ Order Service: (Sync, REST API) to place the order.

โœ… Why synchronous?

  • Payments and coupons must be validated instantly to complete order.

๐Ÿณ Step 4: Restaurant Accepts the Order

๐Ÿ”ฅ Services involved:

  • Restaurant Management Service
  • Order Service
  • Notification Service

๐Ÿ“ฉ Communication:

  • Order Service โ†’ Restaurant Management Service: (Async, Kafka event NEW_ORDER_PLACED) to notify restaurant.

  • Restaurant Management Service โ†’ Notification Service: (Async, Kafka event ORDER_ACCEPTED/REJECTED) to send push notifications.

โœ… Why asynchronous?

  • Restaurant may take time (few seconds) to accept.
  • System should not block the user; instead show "Waiting for confirmation".

๐Ÿšด Step 5: Assign Delivery Partner

๐Ÿ”ฅ Services involved:

  • Delivery Assignment Service
  • Delivery Partner Service

๐Ÿ“ฉ Communication:

  • Restaurant Service โ†’ Delivery Assignment Service: (Async, Kafka event ORDER_READY) to request pickup.
  • Delivery Assignment Service โ†’ Delivery Partner Service: (Async, Kafka event ASSIGN_RIDER) to allocate a delivery person.

โœ… Why asynchronous?

  • Finding the nearest available rider can take a few seconds.
  • Needs retries if rider not available.

๐Ÿ“ Step 6: Live Tracking of Order

๐Ÿ”ฅ Services involved:

  • Tracking Service
  • User Notification Service

๐Ÿ“ž Communication:

  • User App โ†’ Tracking Service: (Sync, REST API or WebSocket) to get live order location.
  • Delivery Partner App โ†’ Tracking Service: (Async, updates every few seconds) to send GPS location.

โœ… Why mixed?

  • Delivery boy's app sends async updates in the background.
  • User app polls or uses WebSocket for real-time experience.

๐Ÿด Step 7: Order Delivered and Feedback

๐Ÿ”ฅ Services involved:

  • Order Service
  • Rating and Review Service

๐Ÿ“ฉ Communication:

  • Delivery Partner App โ†’ Order Service: (Async, Kafka event ORDER_DELIVERED) to mark order delivered.
  • User App โ†’ Rating Service: (Sync, REST API) to submit rating/review.

โœ… Why mixed?

  • Delivery marking can be async.
  • User rating needs quick feedback after submitting.

๐Ÿงฉ Full Service Map (with communication type)

Service Talks to Communication Why
User App โ†’ Search Service Sync (REST) Instant restaurant search
User App โ†’ Menu Service Sync (REST) Instant menu loading
User App โ†’ Cart Service Sync (REST) Real-time cart update
User App โ†’ Coupon/Payment Service Sync (REST) Real-time payment validation
Order Service โ†’ Restaurant Service Async (Kafka) Restaurant confirmation
Restaurant Service โ†’ Delivery Assignment Async (Kafka) Schedule delivery
Delivery Partner App โ†’ Tracking Service Async Background GPS updates
User App โ†’ Tracking Service Sync (REST/WebSocket) Live order tracking
Delivery Partner App โ†’ Order Service Async (Kafka) Mark order delivered
User App โ†’ Rating Service Sync (REST) Instant rating feedback

๐ŸŽฏ Why Sync vs Async in Food Delivery?

Synchronous (REST/WebSocket) Asynchronous (Kafka/Event Bus)
Searching restaurants Order confirmation from restaurant
Adding to cart Assigning delivery partner
Making payment Updating live delivery status
Submitting rating Marking delivery completed

๐Ÿ”ฅ Key Observations:

  • User-facing actions = mostly synchronous.
  • Internal system-to-system actions = mostly asynchronous.
  • Kafka, RabbitMQ, or Pub/Sub used for decoupled communication.
  • REST APIs or WebSockets used for real-time interactions.
  • Scalability, resiliency, and user experience are balanced by picking right communication strategy.

๐Ÿ“š Real-World Example:

  • Swiggy uses gRPC between internal services, Kafka for events, and REST/WebSockets for user interactions.
  • Zomato heavily uses Kafka for order and delivery workflows.

โœ๏ธ Final Thought

Building a food delivery platform is not just about delivering food.

It's about designing a resilient, fast, and fault-tolerant system using the right combination of synchronous and asynchronous communication patterns! ๐Ÿš€๐Ÿ”

Top comments (0)