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.