Design API endpoint to fetch 'coming soon' show content.

Hard
4 years ago

Design an API endpoint to fetch 'coming soon' show content. Consider factors like data structure, filtering, and sorting.

Sample Answer

API Design for 'Coming Soon' Show Content

This document outlines the design for an API endpoint that fetches 'coming soon' show content. This will cover requirements, high-level design, data model, endpoints, tradeoffs, alternative approaches, edge cases, and future considerations.

1. Requirements

  • Use Cases:
    • Users can view a list of shows that are scheduled to be released soon.
    • Users can filter the list by genre.
    • Users can sort the list by release date or popularity.
    • The API should be performant and scalable.
    • The API should be easy to maintain and extend.
  • User Stories:
    • "As a user, I want to see a list of all shows coming soon so I can plan what to watch."
    • "As a user, I want to filter coming soon shows by genre so I can find shows that I like."
    • "As a user, I want to sort coming soon shows by release date so I can see the newest shows first."

2. High-Level Design

The system will consist of the following components:

  • Client: The application (web, mobile, etc.) that consumes the API.
  • API Gateway: A single entry point for all API requests. It handles authentication, authorization, rate limiting, and routing.
  • Coming Soon Service: This service is responsible for fetching and processing 'coming soon' show data. It will interact with the database to retrieve the data and apply any necessary filtering or sorting.
  • Database: Stores the 'coming soon' show data. A relational database like PostgreSQL or MySQL is suitable for structured data.
  • Cache: A caching layer (e.g., Redis or Memcached) to improve performance by storing frequently accessed data.

Workflow:

  1. The client sends a request to the API Gateway.
  2. The API Gateway authenticates and authorizes the request.
  3. The API Gateway routes the request to the Coming Soon Service.
  4. The Coming Soon Service checks the cache for the requested data.
  5. If the data is in the cache, the Coming Soon Service returns the data to the API Gateway.
  6. If the data is not in the cache, the Coming Soon Service retrieves the data from the database, applies any filtering or sorting, and stores the data in the cache.
  7. The Coming Soon Service returns the data to the API Gateway.
  8. The API Gateway returns the data to the client.

3. Data Model

We'll use a relational database to store the show information. Here's the schema for the coming_soon_shows table:

FieldTypeDescription
idINTPrimary key, unique identifier for the show
titleVARCHAR(255)Title of the show
descriptionTEXTShort description of the show
genreVARCHAR(255)Genre of the show (e.g., Action, Comedy, Drama)
release_dateDATEScheduled release date of the show
poster_urlVARCHAR(255)URL of the show's poster image
popularityINTA score representing the show's expected popularity
created_atTIMESTAMPTimestamp of when the record was created
updated_atTIMESTAMPTimestamp of when the record was last updated

4. Endpoints

Endpoint: /coming-soon

  • Method: GET
  • Description: Fetches a list of 'coming soon' shows.

Request Parameters:

  • genre (optional): Filters the shows by genre. Example: genre=Action
  • sort_by (optional): Sorts the shows by a specific field. Possible values: release_date, popularity. Default: release_date.
  • sort_order (optional): Sort order. Possible values: asc, desc. Default: asc for release_date, desc for popularity.
  • page (optional): Page number for pagination. Default: 1.
  • page_size (optional): Number of shows per page. Default: 20.

Request Example:

/coming-soon?genre=Action&sort_by=popularity&sort_order=desc&page=2&page_size=10

Response:

{
  "page": 2,
  "page_size": 10,
  "total_results": 50,
  "total_pages": 5,
  "results": [
    {
      "id": 21,
      "title": "Action Movie 3",
      "description": "Another action-packed adventure.",
      "genre": "Action",
      "release_date": "2024-01-25",
      "poster_url": "https://example.com/action3.jpg",
      "popularity": 95
    },
    {
      "id": 22,
      "title": "Action Flick 4",
      "description": "Explosions galore!",
      "genre": "Action",
      "release_date": "2024-02-01",
      "poster_url": "https://example.com/action4.jpg",
      "popularity": 90
    }
  ]
}

Response Fields:

  • page: The current page number.
  • page_size: The number of shows per page.
  • total_results: The total number of shows matching the query.
  • total_pages: The total number of pages.
  • results: An array of show objects.

5. Tradeoffs

FeatureApproachProsCons
DatabaseRelational Database (e.g., PostgreSQL)Structured data, strong consistency, easy to query and filter.Can be more complex to scale horizontally than NoSQL databases.
CachingRedis or MemcachedImproves performance by reducing database load, faster response times.Adds complexity to the system, requires cache invalidation strategies.
API GatewayUsing an API Gateway (e.g., Kong, AWS API Gateway)Centralized authentication, authorization, rate limiting, and routing.Adds another layer of complexity, potential single point of failure.
PaginationUsing page and page_size parametersPrevents overwhelming the client with large datasets, improves performance.Adds complexity to the API implementation.
Sorting & FilteringImplementing sorting and filtering in the Coming Soon ServiceFlexible, allows for complex queries, reduces data transfer.Can increase the load on the Coming Soon Service, requires careful optimization of database queries.

6. Other Approaches

  • NoSQL Database (e.g., MongoDB): Could be used if the data is less structured or if horizontal scalability is a primary concern. Pros: More flexible schema, easier to scale horizontally. Cons: Less structured, weaker consistency.
  • GraphQL API: Could be used to allow clients to request only the data they need. Pros: Reduces data transfer, more flexible for clients. Cons: More complex to implement, can be harder to cache.
  • Pre-computed Data: For very popular queries, pre-compute the results and store them in the cache. Pros: Extremely fast response times. Cons: Requires more storage, harder to keep data up-to-date.

7. Edge Cases

  • No Shows Coming Soon: The API should return an empty list with appropriate metadata (e.g., total_results: 0).
  • Invalid Genre: The API should return an error or ignore the invalid genre and return all shows.
  • Invalid Sort Field: The API should return an error or use the default sort field (release date).
  • Large Number of Shows: The API should handle pagination efficiently to prevent performance issues.
  • Database Downtime: Implement circuit breaker pattern to handle database downtime gracefully and return a cached response or an error message to the client.
  • Cache Misses: Optimize cache hit rate by choosing appropriate cache eviction policies (e.g., Least Recently Used - LRU).

8. Future Considerations

  • Personalized Recommendations: Implement personalized recommendations based on user preferences and viewing history.
  • Integration with Other Services: Integrate with other services, such as a show details service or a user review service.
  • Real-time Updates: Implement real-time updates using WebSockets or Server-Sent Events (SSE) to notify clients of new shows as they are added.
  • Support for Different Content Types: Extend the API to support different content types, such as movies, documentaries, and live events.
  • Advanced Filtering: Add support for filtering by multiple genres, release date ranges, and other criteria.