2

I'm actually studying about web development. I was just asking why a lot of web apps and chats(Whatsapp, Telegram, Discord, and a lot, a lot more!) are using cache. I mean, after learning cache systems like Redis and Memcached, I was asking how could I use them practically. For example, Redis can cache entire pages for limited time(that will expire). Or it can store login sessions.

But how so many chat (web, desktop and mobile) apps do use caching? I know that they store messages in cache... but wouldn't just a common database be enough? Also, how are messages "sorted" in cache? It seems too difficult to me to understand cache.

2

I know that they store messages in cache... but wouldn't just a common database be enough?

So at time of writing, Whatsapp has... north of 1.5 billion users (https://www.statista.com/topics/2018/whatsapp/ says 1.5b as of Dec 2017). Multiply that times the number of messages a given user sends and you have a lot of messages. And sure, you can shard the databases so you have lots of databases each with their own subset of your userbase.

But proper persistent storage has a few drawbacks here:

  • Latency is a big one. People using messaging apps want their messages now. They want to respond now. Going to some master db to figure out what shard to go to, then wait for the data to be read from disk - it adds up.
  • Cost is a deceptively big one. Proper redundant storage costs more than (mostly) in-memory caches. They (usually) require more power, and in turn more cooling. Persistent storage tends to fail more often than simpler cache servers, increasing maintenance costs. They tend to have increased security and retention requirements, adding yet more cost.

There's also some things that the cache is better suited for. Transient stuff like "Bob is typing..." doesn't need to go to a database. That might not even go to cache depending on the service, but "Bob read this message" doesn't often need to be kept more than a few days. Online status doesn't need to be persisted. Contact lists are probably kept in cache since they change infrequently.

And chat apps especially, any recent message is going to be accessed a lot and anything written a day or more ago will be seen infrequently at best. The frequently accessed stuff can be cached according to expected use to get big benefits on performance.

Also, how are messages "sorted" in cache?

I don't know offhand. This is something that I'd expect will vary a little between services. Each has slightly different workflows and implementations, meaning they'll use the cache a little differently to cater to their particular problems. For example, something like slack may prefer channel based organization, whereas a 1 on 1 app might organize messages by destination user.

  • Oh! Didn't think about online status, the typing message etc. But nice to know that recent messages are cached and old messages are in database. Thanks for you explication! – DanielVip3 Jan 15 at 14:00
  • 1
    @DanielVip3 - recent messages are in the database too. The cache is there to reduce the load on the database servers and improve performance. – Telastyn Jan 15 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.