Design a library management system
medium 85 minModel a lending library: titles and copies, members with plans, loans, a waiting list and fines.
Video lesson · Chapter 1 of 6
Make a prediction
Theory
Read or listen first. About 3 min.
What it is
A library management system in a low-level design round is the software behind the counter: what the library owns, who may borrow, who has what until when, who waits for it, and what a late day costs.
Underneath it is a lending system: things that go out and come back, with a limit for each person and a queue for each kind of thing. Cars, tools, costumes, meeting equipment and e-book licences have the same shape.
The answer is one coordinator with five verbs, two small owners (catalog and waiting list), a few records, and one interface for fines. Its centre is a method of eight lines that decides where a free copy goes.
Why it exists
It is asked because everybody knows libraries, and because the word book hides a modelling question that experience answers at once: is it the text or the thing in your hand?
It mixes three kinds of rules: numbers that change (limits), behaviour that changes (fines) and rules that must never break (one copy, one reader). Good designs treat the three differently.
It is also a small lesson in time. Due days, grace, expiry and reminders are all about days passing while no code runs. Much of the design is choosing what to store and what to calculate when asked.
A short history
1876
Melvil Dewey publishes the decimal classification, first as a pamphlet of 44 pages: a key for where a book stands, not for which copy it is.
1960s
The Library of Congress begins to use computers and devises the MARC format, so that catalog records can be read by machines.
1970
The ISBN becomes an international standard. It numbers an edition. Copies need a number of their own.
1998
The final report of FRBR describes four levels: work, expression, manifestation and item. Our title and copy are the last two.
2000
Koha, made in New Zealand for the Horowhenua Library Trust, goes live in January. Its project calls it the first free library software.
2002
NISO publishes the first version of NCIP, a protocol for circulation: how systems of different makers tell each other about loans.
2007
From 1 January all ISBNs have 13 digits. Systems that had stored them as 10 characters found out.
Words to know
- Title
- The description of a book: one for each ISBN.
- Copy or item
- One physical thing, with a barcode.
- Loan
- One copy with one member until a day.
- Hold
- A wish for any copy of a title. Also called reservation or request.
- Hold shelf
- Where a kept copy waits for its person for some days.
- Lapse
- The days to fetch are over. The copy moves on.
- Plan
- What a member signed for: limit, days, renewals, fines.
- Grace
- Late days that cost nothing. Ask whether they are forgiven or only delayed.
- Facade
- One simple door in front of several parts, with the order of steps inside.
- Outbox
- Messages stored with the change they tell about, and sent later.
- Invariant
- A sentence that is true after every call, such as: nobody waits while a copy is on the shelf.
The solution, twice
Reach the regular design first. Then go where a FAANG interviewer digs.
How it works
Step through the idea, or press Play.
A member borrows a copy. Rules of the member, rules of the copy, then three writes in one step
Understand it
Start here
One card for each toy
A van lends toys in a village. It has two boats and one drum. There is no card for "boat". There is a card for each boat, and a card is always in exactly one place: under the toy, in a child's pocket on the board, or in the basket. Watch where the cards are.
Two kinds of things, one card for each copy, three places a card can be. A library is this van with more shelves.
What an interviewer looks for here
Title and copy
Is there a difference between Dracula and the Dracula with barcode D1? Everything else hangs on it.
The loan as an object
Not a field in the book and a list in the member. One record that both sides read.
Rules that change
Days, limits, fines. Which are numbers and which are behaviour?
What happens on return
A returned copy may already belong to somebody. Most answers put it on the shelf.
Where a returned copy goes
Words
Five nouns that people mix up
| Word | It is | Identity | Changes? | How many for two Draculas |
|---|---|---|---|---|
| Title | The description: name, author | ISBN | Hardly ever | 1 |
| Copy | The thing with dust on it | Barcode | Status changes daily | 2 |
| Loan | One copy with one member for a time | The copy, while it runs | Due date, when renewed | 0, 1 or 2 |
| Hold request | A wish for any copy of a title | Member and title | Moves up | As many as wish |
| Kept copy | A copy that waits on the hold shelf | The copy | Lapses after 3 days | 0, 1 or 2 |
Questions
Six questions, and what each answer changes
| Ask | If yes | If no |
|---|---|---|
| More than one copy of a title? | Title and Copy are two classes | One class would do. Nobody says no |
| Different rules for different members? | A Plan with numbers and a fine policy | Constants |
| Can people wait for a title? | A queue, a third state, an expiry | A returned copy goes to the shelf |
| Must we know who had it last year? | Loans are closed and kept | They may be removed. Ask about privacy |
| Several counters at once? | One lock, or a database | Still say what would break |
| E-books? | Copies become licences: a count, not barcodes | Leave it out |
Design, first try
The design most people draw first
It follows the nouns of the prompt: a book, a member, a library. It works for one copy of each title, one kind of member and one counter. The trouble begins with the second copy.
The design to avoid: one class that does it all, and a book that is two things
1/5One class for everything
Catalog, lending, fines and e-mail in one place. Four reasons to change, and no way to test fines without a library.
usesowns (dies together)extends (is a)
book = library.find("111") # Dracula: but which of the two?member.books.append(book)book.borrowed_by = member # the same fact, a second timebook.due_date = today + timedelta(days=14)# the second copy of Dracula goes out:book.borrowed_by = other_member # and the first loan is forgottenDesign, better
The design to reach
The design to reach: title and copy apart, the loan as an object
1/6One door
Library is what a counter, a kiosk or a web page talks to. Five verbs, one lock, no printing and no e-mail.
owns (dies together)usesimplements
loan = library.checkout("sam", "D1") # a Loan, or Refused with a reasonplace = library.hold("noor", "111") # the place in the queue for a titlefine = library.give_back("D1") # cents, and the copy is passed onA return that somebody waits for
Roles
What each class owns
| Class | Knows | Does | Does not |
|---|---|---|---|
| Library | Members, running loans, the lock | Checks rules, opens and closes loans, passes copies on | Send mail, print, take cards |
| Catalog | Titles, copies, the word index | Finds | Decide who may borrow |
| WaitingList | Who waits for which title | Joins, leaves, takes the first | Know about copies |
| Copy | Its status, and for whom it is kept | Nothing. It is a record | Know the borrower |
| Loan | Copy, member, plan, due day | Nothing. It is a record | Calculate its fine |
| Plan | Limit, days, renewals | Nothing | Know its members |
| FinePolicy | Rate, grace, cap | Days to cents | Know dates |
The life of a copy
The life of a membership
The hard part
Three facts that must agree
The status of the copy, the set in the member and the table of loans tell one story in three places. We keep all three because each answers a question in O(1): is it here, may they take more, who has it. That is allowed under two conditions: one method writes all three, under one lock, and a test compares them.
| Rule that is always true | Broken by |
|---|---|
| A copy is ON_LOAN exactly if there is a loan for its barcode | Closing a loan without passing the copy on |
member.loans is the set of barcodes of their loans | A list in the member that another class changes |
A copy has kept_for exactly if it is KEPT | Clearing the status and not the name |
| If anyone waits for a title, none of its copies is on the shelf | Putting a returned or new copy on the shelf without asking |
| Nobody waits for a title they hold or have kept | Letting a borrower join the queue |
add_copy asks the waiting list: a new copy is a return that nobody made.Where do you write down who has a copy?
BreaksIn the book
- How
book.borrowed_by = member.- Limit
- One loan for each title, and no history.
member.booksrepeats it. - Choose when
- A shelf at home.
WorksIn the copy
- How
copy.borrowed_byandcopy.due.- Limit
- Correct while it runs. Renewals, plan and history have no place, and the copy knows people.
- Choose when
- If nobody asks about history.
StrongIn a loan
- How
- One record: copy, member, plan, due, renewals. Found by barcode.
- Limit
- One more class.
- Choose when
- Here. A relation with its own data is a class.
Money
Fines: days to cents
The fine is the part people expect to be a Strategy, and it is. Look at what varies. Standard and student differ by a number. A grace of three days is another shape, though one formula still covers both. No fine at all is a third.
| Days late | Regular, 50 a day | Student, 25, cap 1000 | Premium, 50, 3 days grace | Staff |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 3 | 150 | 75 | 0 | 0 |
| 4 | 200 | 100 | 50 | 0 |
| 5 | 250 | 125 | 100 | 0 |
| 50 | 2500 | 1000 | 2350 | 0 |
| 60 | 2500 (cap) | 1000 (cap) | 2500 (cap) | 0 |
Holds up
- Cents as integers
- Calendar days from
date, not hours fromdatetime - The plan is stored in the loan when it starts
- The policy gets a number of days and knows nothing else
Breaks
doublefor moneynow - duein seconds, divided by 86,400- Reading the plan of today for a loan of last month
- A
setStrategyon one shared calculator: two counters, one strategy
Is it a number or a behaviour?
Two counters
The same copy, the same moment
Asha and Lee want one copy. At a desk this is impossible: the book is in one hand. Online it is normal, because both see *available* on a screen. So the check and the change must be one step. For one process a lock does it: the numbers are in the FAANG interview pages of the Solution.
Why the limit needs the lock too
Summary
The whole topic in one minute.
A title is a description and a copy is a thing. Members borrow copies and wait for titles. The loan is an object that knows the copy, the member, the plan and the due day. Kinds of members are plans with numbers; only the fine is a strategy.
A copy that becomes free goes to the first who waits, under the same lock that closed the loan, and only then to the shelf. A kept copy lapses to the next person. Messages are written with the change and sent after it.
- A title is a description, a copy is a thing. Lend copies, wait for titles.
- The loan is an object: copy, member, plan, due day.
- Kinds of member are a plan with numbers. Only the fine is behaviour.
- A copy that comes back goes to the first who waits, and only then to the shelf.
- Kept is a state with a name and a last day. It lapses to the next, not to the shelf.
- One lock covers copy, member and queue. Messages go out after it.
- If three places say the same thing, one method writes them and a test compares them.
Sources
- Library of Congress: overview of the BIBFRAME 2.0 model
- Library of Congress: understanding MARC
- International ISBN Agency: what is an ISBN?
- Koha manual: circulation and fine rules
- Evergreen documentation: placing holds
- Evergreen documentation: item statuses
- FOLIO documentation: circulation settings
- NISO: the circulation interchange protocol NCIP
- PostgreSQL: partial indexes
- Debezium: outbox event router
- Internet Archive: borrowing from the lending library