php - Twitter-clone Redis global post id
I am looking at the doc of design and implementation of a simple Twitter clone using PHP and the Redis key-value store. I found that the
next_post_id variable is global.
I am wondering why not every user keeps his own
next_post_id, and the user's
next_post_id and his
user_id can identify a unique post. In this case, we can reduce the contention on updating
next_post_id in simultaneous accesses.
Redis demands keeping things as simple as possible.
Post identifier is increased globally because it's conceptually global too. Each user doesn't own a collection of posts, but posts are a global collection and they're assigned to users.
incrcommand each process connecting to Redis will get an unique post identifier, thus, there's no problem here with this strategy.
This isn't true, because Redis isn't multi-threaded when reading and writing to a database. It's single-threaded. That is, there're no simultaneous accesses, but sequential accesses. You should realize, then, that there would be no benefit on doing what you said.
Maybe you want to take a look at this other Q&A: Redis is single-threaded, then how does it do concurrent I/O?