[NineChap Sys] System Design Class 2 - Shuatiblog.com
This class covers database design:
design account (login/out) system for our radio.
This class covers database design:
- design data with class and inheritance
- design a user system (Netflix 2015)
- design a payment system (Yelp, BigCommerce 2015)
design account (login/out) system for our radio.
Step one, scenario
- register, update, remove account
- login/out
- user balance, VIP services
Step Two, necessary
- Ask
- total user: 100 million
- daily user: 1 million
- predict
- daily active user in 3 month: 10 million
- register percentage: 1%
- daily new register: 100 thousand
- more predict
- login percentage: 15%
- average login frequency: 1.2 (ppl may input wrong password 20% of time)
- daily login attempts: 10 million * 15% * 1.2 = 1.8 million
- average login frequency: 1.8 million / 24hr = 21 login/sec
- normal login frequency: 21 * 2 = 42 login/sec
- peak login frequency: 42 * 3 = 126 login/sec
Step Four, Kilobit
Data – User table should contain name and password. What else?
class User {
int userId; (primary key)
String name;
String password;
}
Data – User Table
class UserTable {
list<User> table;
public insert(){}
public delete(User){}
public update(User){}
public User select(){}
}
CRUD, (Sometimes called SCRUD with an “S” for Search) are the four basic functions of persistent storage.
Step Five, Evolve
advanced 2: verification and forbidden accounts
We have to know the concept of Account State Lifecycle Graph:
- ban: fake user, advertising users… bannned by the management
- inactive: user choose to suspend his own account, voluntarily.
- delete account: normally we won’t remove all related data (just make userId as “deleted”). Otherwise a lot of data can be violated. All your chatting history actually remains.
redesign User Table
Old User table:
class User {
int userId; (primary key)
String name;
String password;
}
Modified User table:
class User {
int userId;
char name[10];
char hiddenPassword[10];
int state;
}
- We added state, to support Account life cycle.
- We changed username to fixed size, for better performance on searching and storing. Can prevent certain attacks, too.
- save encrypted password.
advanced 3: login/out process
- User account auto logged out after a certain period of time.
- multiple account logged in at same time.
Session
Session is a conversation between user and server.
- User can have >1 session, if he log in from different devices.
- Session must be verified, thus we have to keep sessionId.
Session status: “iPad online”, “PC online”…
Modify User table:
class User {
int userId;
char name[10];
char hiddenPassword[10];
int state;
List<session> sessionList;
}
Important in Session table:
- device ID
- time-out periodclass Session { private sessionId; int userId;
int deviceCode; long timeOut;
}
User table would include a session list.
further improvement: session
- we update sessionList very frequently.
- size of sessionList is dynamic.
Solution: seperate the table.
Question: When to clean up the session data (considering huge amount of data and frequent calculation)?
Answer: every end of day. Or store sessions in-memory, so it lose all the data when machine restarts (it is used in Gaming platforms). Or we can clean up one user’s session list whenever the user did a new log-in.We do not remove session whenever it expires. It’s too much calculation.
further improvement: inheritance
Apply inheritance to UserTable and SessionTable:
class Table {
list<Row*> table;
public insert(){}
public delete(){}
public update(){}
public User select(){}
}
class UserTable extends Table {
}
class SessionTable extends Table {
}
As for the Row class:
class Row {
List<Attributes> row;
}
class User extends Row {
}
class Session extends Row {
}
advanced 4: search in table
- find my userId
- find my userId range
Solution 1: add HashMap in the table. Can do search in O(1), but can’t find range.
Solution 2: BST data structure. Can do search range and search in O(log2 n) time.
Best solution: B+ tree
B+ tree – everything in O(logb n) which is close to constant time.
Plus, B+ tree is hard disk friendly. Read more on a future post.
advanced 5: VIP services
User could buy VIP services using his acccount balance.
class ProService {
int userId;
double money;
long endTime;
public addMoney(){}
public buyPro(){}
}
Q5.1: System crashes before adding time
Solution: transaction with log
WriteLOG
Transaction_1123: BEGIN
money=20; endTime=16_07_2016
If system crash happens here, system will read the log, recover and roll back all original data. Try not to complete the transaction – just fail it.
WriteLOG
Transaction_1123: BEGIN
money=20; endTime=16_07_2016
WriteLOG
Transaction_1123: END
money=10; endTime=16_08_2016
What happens if system crash during writing the log? or during the rollback?
Q5.2: check list
- one user id have 2 corresponding pro-services information.
- Shallow user: a pro-services info does not have corresponding user.
Solution: have a checker class.
Q5.3: 2 simutaneous purchase.
Solution: lock.
- first process lock money & endTime.
- Read money = 20
- another process try to lock, but end up waiting (sleeping).
- first process do the job, and release the lock.
- another process wakes up.
lock have time-out settings. It can be applied in distributed system as well.
Question: does lock make your execution slow?
- If another process is sleeping, CPU will be fully consumed by other process. So it won’t impact.
- You can do some async processing, too.
- When you lock, try to lock only a small piece of code, not the entire method. In DB, lock only a row, not a table.
- Java CAS (Compare and swap )
Q5.4: machine crash
Solution: duplication.
- How many copies?Google did 3. Normally 2 in same data center, and 1 in another location.Backup data normally is on another machine. But there’s also RAID (redundant array of independent disks) which:combines multiple physical disk drive components into a single logical unit for the purposes of data redundancy, performance improvement, or both.
- When does the copy happen?Option 1 is doing everyday nightly. This is called a ‘check point’.Option 2 is use another server to support Shadow Redundancy. All data from Machine 0 will be copied to Machine 1 WHILE it happens. The result is, Machine 1 is identical to Machine 0. If Machine 0 crashes, Machine 1 may be behind less than 1 second.The way to duplicate is either re-play all the actions, or to read Machine 0’s log and apply the new data.
- How to copy?User send data to 1 server, and from that server, pipeline. This ensures good usage of server bandwith, and serial transfer of data ensures low latency (several ms).It’s also possible to do tree-like transfer, but the above method is preferred cuz all machine consume same bandwidth.
- What is log?It is actually ‘checkpoint’ + log. It allows you to rollback.
Summary:
Final note: Data consistency
Main sources of inconsistency comes from:
- network fault
- disk error
The disk eror is solved by checksum (compare during disk writing).
Summary
ACID is a set of properties that guarantee that database transactions are processed reliably.
- Atomicity: all or nothingQ5.1: System crashes before adding time
- Consistency: validate according to defined rulesQ5.2: check list
- Isolation: independency between transactions (lock)Q5.3: 2 simutaneous purchase.
- Durability: stored permanentlyQ5.4: machine crash
Question:
- design a user system (Netflix 2015)
Hint: table design, register, login/out, password check, find password.
- Design payment system (Yelp, BigCommerce 2015)
Hint: the question does not ask about table/ds design itself, but rather the problems associated with payment. Read about ACID principle.
Read full article from [NineChap Sys] System Design Class 2 - Shuatiblog.com