Skip to content
VIRUCHITH.COM
Back to articles
Systems Design Systems DesignInventory ManagementBitmaskingIRCTCredBus

The "Bitmask" Secret: How Trains and Buses Manage Intermediate Bookings Without Chaos

Discover how IRCTC and redBus solve segment-based inventory challenges using Bitmasking. Learn the bitwise logic behind intermediate bookings, ACID-compliant architectures, and how virtual bucketing optimizes revenue for long-distance journeys.

Author

Viruchith Ganesan

Mar 3, 2026
3 min read
Illustration of a train and a bus with a digital inventory system overlay

If you’ve ever booked a ticket on redBus or IRCTC, you’ve likely wondered: How does the system know seat 24 is available for me from Bangalore to Salem, but already booked for someone else from Salem to Kochi on the same bus?

This isn’t a simple “Available/Booked” boolean. It’s a complex problem of Segment-Based Inventory Control.

1. The Core Data Structure: The Bitmask Strategy

The most efficient way to track a seat’s journey is through Bitmasking.

Imagine a train journey from Chennai (MAS) to New Delhi (NDLS) with 10 intermediate stops. That journey has 9 “legs” or segments.

  • We represent each seat as a 9-bit integer.
  • 0 means the leg is free; 1 means it is booked.

Example: The “Bitwise Check”

Suppose a seat has a mask: 000001100. This tells us the seat is booked for legs 6 and 7 (e.g., Nagpur to Itarsi).

If you want to book from Chennai to Vijayawada (Legs 1 and 2), the system creates a Request Mask: 110000000.

It then performs a Bitwise AND operation:

Current_Mask & Request_Mask) == 0? (000001100 & 110000000) == 0 → True!

The seat is available for your partial journey. If you book it, the new mask becomes 110001100 via a Bitwise OR.

2. The High-Level Architecture

To handle millions of hits during a “Tatkal” window, the architecture must be split between Static Discovery and Atomic Transactions.

A. The Search Service (Read-Heavy)

Systems like redBus use an Aggregated Search Architecture. Since they work with thousands of private operators, they don’t own all the data.

  • Cache-Aside Pattern: Search results (bus lists, routes) are cached in Redis.
  • Eventually Consistent: Availability shown in the search list might be 30 seconds old to save database IOPS.

B. The Booking Service (Write-Heavy & ACID)

This is where the bitmask is updated. It requires strict ACID compliance (Atomicity, Consistency, Isolation, Durability) to ensure no two people book the same segment.

3. Database Schema Design

A simplified schema for a system like IRCTC looks like this:

Table: Train_Schedule Train Schedule Schema

Table: Seat_Inventory Seat Inventory Schema

Table: Booking_History Booking History Schema

4. The “End-to-End” Priority Logic

Why is it sometimes harder to book a “Short Distance” ticket than a “Long Distance” one? This is due to Pooled Quotas.

IRCTC uses a strategy called Virtual Bucketing:

  1. General Quota (GN): Reserved for people going from the Origin to the Destination.
  2. Remote Location Quota (RL): Specific seats “unlocked” only for intermediate major stations.
  3. Pooled Quota (PQ): Shared between small intermediate stations.

The Logic: If the system sold every seat to “Short Distance” travelers (Leg 1 only), the train would run empty for the remaining 8 legs, losing massive revenue. The algorithm “protects” long-distance segments by limiting how many bits can be flipped for short journeys.

5. Handling Concurrency (The Tatkal Challenge)

When 100,000 people click “Book” at 10:00 AM, how do you prevent double-booking?

A. Distributed Locking

Systems use Redis Redlock or Zookeeper to lock a specific train_id + date + seat_no for a few minutes while the payment is processed.

B. Database Sharding

Data is sharded by Train_ID. Requests for the “Rajdhani” go to Shard A, while “Shatabdi” goes to Shard B. This prevents a single database from becoming a bottleneck.

Summary

Building a booking engine for India’s scale requires more than just a SELECT query. It requires:

  • Bit manipulation for speed.
  • Segmented inventory for revenue optimization.
  • Distributed locks for concurrency.

Next time you see “Seat 24” on your ticket, remember—it might have had three different owners before the bus even reached its final stop!

Continue reading

Related articles