Library Management System
Library management LLD covers books, members, loans, reservations, and fines — classic entity-relationship modeling with behavior.
Introduction
Library management LLD covers books, members, loans, reservations, and fines — classic entity-relationship modeling with behavior. Interviewers test due dates, copy availability, and concurrent checkout of last copy.
Separate Catalog (search), LoanService (checkout/return), and MemberAccount (fines, limits). Avoid one Library class doing everything.
Extensions: multiple branches, digital ebooks, waitlists.
Understanding the topic
Key concepts
- Book vs BookItem (title vs physical copy with barcode).
- Member with borrowing limit and fine balance.
- Loan links member, copy, due date.
- Reservation queue when all copies out.
- Catalog search by ISBN, author, title.
- Fine calculation on overdue return.
Step-by-step explanation
- Member searches catalog.
- LoanService finds available copy or creates reservation.
- Checkout creates Loan, marks copy unavailable.
- Return marks copy available, calculates fine if overdue.
- Reservation notified when copy returned.
- Librarian role for add/remove books.
Informative example
Library with BookCopy, Member, Loan, and LoanService:
public final class BookCopy {private final String barcode;private final String isbn;private boolean available = true;public BookCopy(String barcode, String isbn) {this.barcode = barcode;this.isbn = isbn;}public boolean isAvailable() { return available; }public String isbn() { return isbn; }void checkout() {if (!available) throw new IllegalStateException("out");available = false;}void checkin() { available = true; }}public final class Member {private final String id;private int activeLoans;private final int limit;public Member(String id, int limit) {this.id = id;this.limit = limit;}public boolean canBorrow() { return activeLoans < limit; }public String id() { return id; }public void onLoanCreated() { activeLoans++; }public void onLoanClosed() { activeLoans--; }}public record Loan(String id, String memberId, String barcode, LocalDate due) {}public final class LoanService {private final Map<String, BookCopy> copies = new HashMap<>();private final Map<String, Loan> openLoans = new HashMap<>();public Loan checkout(Member member, String isbn) {if (!member.canBorrow()) throw new IllegalStateException("limit");BookCopy copy = copies.values().stream().filter(c -> c.isbn().equals(isbn) && c.isAvailable()).findFirst().orElseThrow(() -> new IllegalStateException("none available"));copy.checkout();member.onLoanCreated();Loan loan = new Loan(UUID.randomUUID().toString(), member.id(), copy.barcode(), LocalDate.now().plusWeeks(2));openLoans.put(loan.id(), loan);return loan;}public Money returnBook(String loanId) {Loan loan = openLoans.remove(loanId);copies.get(loan.barcode()).checkin();if (LocalDate.now().isAfter(loan.due())) {return Money.of(1).multiply(ChronoUnit.DAYS.between(loan.due(), LocalDate.now()));}return Money.ZERO;}}
Split Catalog and ReservationService when interviewer adds waitlists.
Real-world use
Real-world applications
- Entity modeling interview question.
- Practice SRP across catalog and loan services.
- Reservation and notification extensions.
Best practices
- Distinguish bibliographic Book from physical copy.
- Enforce borrowing limits on Member.
- Loan as audit record with due date.
- Fine policy in dedicated calculator.
- Thread-safe copy checkout with lock per barcode.
Common mistakes
- Single Book class without copy distinction.
- No due date or fine handling.
- Search logic embedded in LoanService.
- Lost track of which copy member holds.
Advanced interview questions
Q1BeginnerBook vs BookCopy?
Q2BeginnerHow prevent over-borrowing?
Q3IntermediateAll copies checked out?
Q4IntermediateMultiple library branches?
Q5AdvancedConcurrent checkout last copy?
Summary
Separate catalog, copies, members, loans. BookCopy tracks physical availability. LoanService orchestrates checkout and return. Fines and reservations as extensions. Strong entity model beats pattern overload.