### Logistics * Magic code:
Scalability Pattern: Database Caching
Using caching (e.g. redis) to reduce db access
Example: Social Graph
Schema (like all of you have)
User(id, name)
Follow(id, follower_id, following_id)
Content(id, author_id)
Nicely normalized
First, Second and Third Normal form
Origins of the relational database
Queries like:
How many people are following user X?
Who is following user Y?
What are the most recent “n” posts (i.e. content) for user “u”?
What are the most recent “n” posts for users that “u” is following?
But to display each and every user, a join is needed!
Measurement
Ask database system to analyze SQL queries that are slow
Discover that the social graph access was very slow
Discussion
Have you started using redis yet in your projects?
What do your redis keys look like?
How do you compute your cache key?
DB: Caching
Use Network scale caching (Redis) to store and share across servers
count:followers:u = number
count:following:u = number
How to maintain this number?
How important is it that it is correct?
What might make it incorrect?
Add APIs
get_follower_count(user), get_following_count(user), incr_follower_count(user), decr_follower_count(user), incr_following_count(user), decr_following_count(user)
What class has those methods?
Where are they invoked?
Result of queries?
Result of search?
Creating the cache key
What do you store in the cache?
Teams
Discuss and design how you would incorporate this idea in your specific nanoTwitter
Thank you. Questions?
(random Image from picsum.photos)