Cache layer to json api

Hi everyone!

I have a small social website on rails that use react on view and backend is just a JSON API,

with more o less 3000 users online at any moment. It runs with postgres/memcached

When user, for example, visits its feed page, I do:

  1. Select activities from database (20 per page)

  2. Select last 4 comments from each activity from database (justo 1 select)

  3. Select all users referenced by activity or comment from database (select users.* from users where id in (1,3,4,5,…100) )

I have a cache layer (memcached) that when I will load users, first I try load from memcached, if it not exists I read from database and put it on cache.

BUT I also have some “listenners” on users model (and over others referenced models like address and profile) to invalidate cache if any field change.

The problem:

  • This cache demand a lot of code.

  • Sometimes cache run out of sync.

  • I hate to have this listeners and they are “side effects”

My question is: Any one is doing something like that??

I search A LOT over all google about cache layer to json api and looks like that everyone is just using database directly.

I know that Rails has it own solution, but it always end up using update_at column, that means, I have to fetch users from database any way.

alternative:

  • Live with date, life is not pretty

  • Buy a more powerful postgres instance… any one is using memcached like that.

  • Remover listeners, put some expires_in (1 or 2 minutos… or more) and let app show out of sync data for a couple of minutes.

thanks for any help!