Bird
Raised Fist0
LLDsystem_design~10 mins

Class design (Book, Member, Librarian, Loan) in LLD - Scalability & System Analysis

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Scalability Analysis - Class design (Book, Member, Librarian, Loan)
Growth Table: Class Design for Library System
ScaleUsers (Members)BooksLoans ActiveSystem Changes
100 users1001,00050Simple in-memory data structures, single server, no caching needed
10,000 users10,000100,0005,000Use database for persistence, add indexing on books and members, basic caching for frequent queries
1,000,000 users1,000,00010,000,000500,000Database sharding by member ID or book ID, read replicas, caching layer (Redis), horizontal scaling of application servers
100,000,000 users100,000,0001,000,000,00050,000,000Advanced sharding and partitioning, distributed caching, microservices for different domains (Book, Member, Loan), CDN for static content, asynchronous processing for loan updates
First Bottleneck

At small scale, the database is the first bottleneck because it handles all queries for books, members, and loans. As users grow, the database query load and storage needs increase rapidly. Without indexing and caching, response times degrade.

At medium scale, the application server CPU and memory become bottlenecks due to processing many concurrent requests and managing business logic.

At large scale, network bandwidth and data partitioning challenges arise, especially for loan transactions and book availability updates.

Scaling Solutions
  • Database: Add indexes on frequently queried fields (book ID, member ID). Use read replicas to distribute read load. Implement sharding by member or book ID to split data across servers.
  • Caching: Use Redis or Memcached to cache frequent queries like book availability and member info.
  • Application Servers: Horizontally scale by adding more servers behind a load balancer to handle more concurrent users.
  • Data Partitioning: Partition loans and books by region or category to reduce cross-server queries.
  • Asynchronous Processing: Use message queues for loan updates and notifications to reduce synchronous load.
  • Microservices: Separate Book, Member, and Loan services to isolate load and scale independently.
Back-of-Envelope Cost Analysis
  • Requests per second (QPS): For 1M users, assume 10% active daily, each making 5 requests/hour -> ~140 QPS.
  • Storage: 10M books at 1 KB metadata each -> ~10 GB; 1M members at 1 KB each -> ~1 GB; 500K active loans at 1 KB each -> ~0.5 GB.
  • Bandwidth: Assuming 1 KB per request/response, 140 QPS -> ~0.14 MB/s (~1.1 Mbps), manageable with standard network.
Interview Tip

Structure your scalability discussion by first describing the system components and their interactions. Then, analyze how load grows with users and data. Identify the first bottleneck clearly. Propose targeted solutions for each bottleneck, explaining why they fit. Use concrete numbers to justify your choices. Finally, mention trade-offs and future scaling steps.

Self Check

Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?

Answer: Add read replicas to distribute read queries and reduce load on the primary database. Also, implement caching for frequent reads to reduce database hits. Consider query optimization and indexing if needed.

Key Result
The database is the first bottleneck as users and data grow; scaling requires sharding, caching, and horizontal scaling of application servers.

Practice

(1/5)
1. Which class should be responsible for storing information about a book's title, author, and ISBN in a library system?
easy
A. Member
B. Book
C. Librarian
D. Loan

Solution

  1. Step 1: Identify the class representing a book

    The class named Book logically holds details about books such as title, author, and ISBN.
  2. Step 2: Confirm other classes' roles

    Member is for library users, Librarian manages operations, and Loan tracks borrowing, so they don't store book details.
  3. Final Answer:

    Book -> Option B
  4. Quick Check:

    Book class stores book info [OK]
Hint: Book class holds book details like title and author [OK]
Common Mistakes:
  • Confusing Member with Book class
  • Assigning book details to Loan class
  • Thinking Librarian stores book info
2. Which of the following is the correct way to define a method named borrowBook inside the Member class in Python?
easy
A. def borrowBook():
B. def borrowBook(book):
C. def borrowBook(self):
D. def borrowBook(self, book):

Solution

  1. Step 1: Understand method definition in Python classes

    Instance methods must have self as the first parameter to access object data.
  2. Step 2: Check method parameters for borrowing a book

    The method needs the book to borrow, so it should accept a book parameter after self.
  3. Final Answer:

    def borrowBook(self, book): -> Option D
  4. Quick Check:

    Instance method with self and book param [OK]
Hint: Instance methods always start with self parameter [OK]
Common Mistakes:
  • Omitting self parameter
  • Not passing book argument
  • Defining method without parameters
3. Given the following Python code snippet, what will be the output?
class Loan:
    def __init__(self, book, member):
        self.book = book
        self.member = member

loan = Loan('1984', 'Alice')
print(loan.book, loan.member)
medium
A. 1984 Alice
B. book member
C. Loan object memory address
D. Error: missing parameters

Solution

  1. Step 1: Analyze the Loan class constructor

    The constructor __init__ assigns book and member to instance variables.
  2. Step 2: Check the print statement output

    Printing loan.book and loan.member outputs the strings '1984' and 'Alice' separated by space.
  3. Final Answer:

    1984 Alice -> Option A
  4. Quick Check:

    loan.book and loan.member print values [OK]
Hint: Print instance variables to see stored values [OK]
Common Mistakes:
  • Expecting object memory address output
  • Confusing variable names with strings
  • Assuming error due to parameters
4. Identify the error in this Python class design snippet for the Librarian class:
class Librarian:
    def __init__(self, name):
        self.name = name
    def addBook(book):
        print(f"Adding {book} to library")
medium
A. Missing self parameter in addBook method
B. Incorrect print statement syntax
C. Constructor missing return statement
D. Class name should be lowercase

Solution

  1. Step 1: Check method parameters in class

    Instance methods must include self as the first parameter to access instance data.
  2. Step 2: Verify addBook method signature

    addBook lacks self, so it will cause an error when called on an instance.
  3. Final Answer:

    Missing self parameter in addBook method -> Option A
  4. Quick Check:

    Instance methods need self param [OK]
Hint: Instance methods always start with self parameter [OK]
Common Mistakes:
  • Thinking print syntax is wrong
  • Expecting constructor to return value
  • Believing class names must be lowercase
5. In designing a library system, which class should handle the logic to check if a book is currently loaned out before allowing a member to borrow it?
hard
A. Book
B. Member
C. Loan
D. Librarian

Solution

  1. Step 1: Understand responsibilities of each class

    Book stores book info, Member represents users, Loan tracks borrow records, and Librarian manages library operations.
  2. Step 2: Identify who controls borrowing rules

    The Loan class should handle checking if a book is currently loaned out before allowing borrowing, as it tracks loan records.
  3. Final Answer:

    Loan -> Option C
  4. Quick Check:

    Loan class tracks loan status [OK]
Hint: Loan class tracks if a book is loaned out [OK]
Common Mistakes:
  • Putting borrowing logic inside Book class
  • Assigning loan status check to Member
  • Expecting Librarian class to enforce borrowing rules