php - Twitter-clone Redis global post id

950

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 thenext_post_id variable is global.

I am wondering why not every user keeps his ownnext_post_id, and the user'snext_post_id and hisuser_id can identify a unique post. In this case, we can reduce the contention on updatingnext_post_id in simultaneous accesses.

678

Answer

Solution:

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.

Usingincr 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?

People are also looking for solutions to the problem: php - Laraver routing error

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.