1. What is Caching?
Caching refers to the practice of storing frequently accessed data in a temporary storage location, so future requests can be served more quickly without having to access the underlying database or compute results again.
Types of Caching
In-Memory Caching:
- Definition: Frequently used data is stored in memory (RAM) for fast access.
- Tools: Popular caching systems like Redis or Memcached are often used in such scenarios. These tools can store data in-memory for quick retrieval.
- Example in BookMyShow: Caching movie details, showtimes, or available seat information for specific theaters that are frequently accessed.
Distributed Caching:
- Definition: In a large-scale system like BookMyShow, data is cached across multiple servers, allowing for redundancy and high availability. A distributed cache is more robust than a single in-memory cache, especially in a distributed environment.
- Tools: Redis Cluster or Amazon ElastiCache provide a distributed caching solution.
- Example in BookMyShow: Movie showtimes or ticket availability data for high-demand movies or theaters can be cached in a distributed manner across various locations.
HTTP Caching:
- Definition: Web pages or API responses are cached at the HTTP level, either by the browser, a CDN, or at the web server.
- Example in BookMyShow: The details of a specific movie or a particular theater’s schedule could be cached on a CDN, allowing users to access these details quickly without querying the backend.
2. Why is Caching Important for BookMyShow?
Benefits of Caching for BookMyShow
Reduced Latency:
- Serving data from a cache is significantly faster than querying a database, leading to lower response times for users. For example, showing available tickets for a specific movie can be retrieved instantly from the cache.
Offloading Database Load:
- Caching reduces the load on the underlying databases, freeing up resources to handle more complex queries or updates. This helps BookMyShow manage the huge number of concurrent requests without overloading the system.
Improved Scalability:
- Caching allows the system to scale horizontally by reducing the demand on database and backend services. This ensures that BookMyShow can handle millions of users simultaneously without performance degradation.
Cost Efficiency:
- Accessing cached data consumes fewer resources than querying the database, leading to better resource utilization and lower operational costs.
3. Cache Strategies in BookMyShow
Time-based Expiration:
- Data in the cache is valid for a certain period. After this period, the data is refreshed from the primary source (like the database). This is useful for time-sensitive data like showtimes or availability that may change frequently.
- Example: Cache movie showtimes for 15 minutes. If a user requests showtimes after 15 minutes, the system will fetch the latest data from the database and cache it again.
Cache Invalidation:
- When the underlying data changes (for example, a new booking or a movie schedule update), the cache must be invalidated to ensure that outdated data is not served to users.
- Example: If a user books a seat for a movie, the seat availability cache must be updated to reflect the new availability status.
Lazy Loading:
- Data is cached when it is first requested. The system fetches data from the database only when it is needed and then stores it in the cache for future requests.
- Example: When a user searches for a movie, the details are retrieved from the database and cached. If another user searches for the same movie, it is served from the cache.
Write-Through Caching:
- Data is written to both the cache and the database at the same time. This ensures that the cache is always up-to-date with the underlying data.
- Example: When a user updates their profile information, the change is written to both the database and the cache simultaneously.
4. What is Load Balancing?
Load balancing refers to distributing incoming network traffic across multiple servers or resources to ensure that no single server becomes overwhelmed with requests. It helps ensure high availability, reliability, and fault tolerance for services, especially in systems like BookMyShow where the number of requests can spike dramatically.
Types of Load Balancing
DNS Load Balancing:
- Definition: DNS load balancing uses DNS servers to distribute traffic among multiple IP addresses.
- Example in BookMyShow: The domain name for BookMyShow can resolve to different data centers or servers based on location or server health.
HTTP(S) Load Balancing:
- Definition: A load balancer intercepts HTTP/HTTPS requests and forwards them to the appropriate backend server based on routing policies.
- Example in BookMyShow: If a user accesses the BookMyShow website, the load balancer decides which server will handle the request based on the server’s current load, geographic location, or other criteria.
TCP/UDP Load Balancing:
- Definition: This type of load balancing deals with lower layers (network and transport layers). It distributes traffic for applications that use TCP or UDP.
- Example in BookMyShow: If BookMyShow uses real-time communication (such as WebSockets) for user interactions, TCP load balancing would be used to ensure smooth communication.
5. Why is Load Balancing Important for BookMyShow?
Benefits of Load Balancing for BookMyShow
Improved Reliability and Availability:
- Distributing requests across multiple servers ensures that if one server goes down, the load balancer can reroute traffic to other servers. This minimizes downtime and enhances the availability of services.
Optimized Resource Utilization:
- Load balancing helps in utilizing the full capacity of all servers, ensuring that no single server is overwhelmed while others remain underutilized.
Scalability:
- As BookMyShow grows and receives more traffic, additional servers or resources can be added to the pool, and the load balancer will automatically distribute the load. This horizontal scaling ensures that the system can handle increased demand.
Improved User Experience:
- By directing users to the server with the least load or the closest geographical location, load balancing reduces latency and improves response times for users.
6. Load Balancing Strategies in BookMyShow
Round-Robin:
- Traffic is distributed evenly across servers in a circular order. This is the most common and simplest method of load balancing.
- Example: In BookMyShow, user requests for booking tickets can be forwarded to different servers based on this technique.
Least Connections:
- Requests are sent to the server that currently has the least active connections. This ensures that the server with the least load processes new requests first.
- Example: For users trying to book tickets during peak hours, the server with the least number of active users will handle the new request.
Weighted Load Balancing:
- Servers are assigned different weights based on their computing power or capacity. More powerful servers handle more traffic.
- Example: In BookMyShow, if some servers are more powerful, the load balancer can assign them a higher weight, meaning they will receive more requests.
Geo-Location Based Load Balancing:
- Requests are routed to the closest data center based on the geographical location of the user. This helps in reducing latency and improving the user experience.
- Example: A user in India will be routed to a BookMyShow server in India, while a user in the US would be routed to a server in the US.
7. Challenges in Cache & Load Balancing for BookMyShow
Cache Invalidation:
- Ensuring that the cache is updated when there is a change in the underlying data is a challenge. For example, when a user books a ticket, the system must invalidate the cache for that movie’s available seats.
Cache Coherence:
- In a distributed caching system, ensuring that all cache nodes have consistent data can be difficult, especially when some data updates are infrequent or large in size.
Load Balancer Bottlenecks:
- The load balancer itself can become a bottleneck if not scaled properly. It’s important to ensure that the load balancing system can handle the volume of traffic.
Session Persistence:
- In a load-balanced environment, maintaining session persistence (i.e., ensuring that user sessions are consistent across requests) can be challenging. For example, if a user is in the middle of booking tickets, the load balancer must send all requests from that user to the same server.