Low-Level Design Tutorial 0/42 lessons ~6 min read Lesson 32

    Library Management System

    Library management LLD covers books, members, loans, reservations, and fines — classic entity-relationship modeling with behavior.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Member searches catalog.
    2. LoanService finds available copy or creates reservation.
    3. Checkout creates Loan, marks copy unavailable.
    4. Return marks copy available, calculates fine if overdue.
    5. Reservation notified when copy returned.
    6. Librarian role for add/remove books.

    Informative example

    Library with BookCopy, Member, Loan, and LoanService:

    java
    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?
    Book is title metadata; BookCopy is lendable physical item with barcode.
    Q2BeginnerHow prevent over-borrowing?
    Member tracks activeLoans against limit before checkout.
    Q3IntermediateAll copies checked out?
    Reservation queue; Observer notifies when copy returned.
    Q4IntermediateMultiple library branches?
    Branch owns copies; Catalog federated search; Loan tied to branch id.
    Q5AdvancedConcurrent checkout last copy?
    Synchronize on copy or use DB unique constraint — optimistic failure path.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.