1

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.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
HuangJie
  • 1,488
  • 1
  • 16
  • 33

1 Answers1

0

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.

Using incr command each process connecting to Redis will get an unique post identifier, thus, there's no problem here with this strategy.

In this case, we can reduce the contention on updating next_post_id in simultaneous accesses

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?

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206