News for hackers. Stuff that matters.
Re:How not to structure database-backed web apps: performanc... by nicktrocado92 on Thursday June 28, 2018 @10:25AM (#17417098)
Why it's an antipattern ?
Re:How not to structure database-backed web apps: performanc... by mnutt on Thursday June 28, 2018 @10:34AM (#17417169)
It’s susceptible to Thundering Herd whereby more requests come in for the same cache key before the initial computation is finished, and so you end up with lots of cache misses. The fix is usually to lock the cache key and have subsequent requests wait on the original computation but it’s a bit more complex to code.
Re:How not to structure database-backed web apps: performanc... by testplzignore on Thursday June 28, 2018 @01:06PM (#17418664)
Just be careful to lock the cache key, and not the cache. I've seen the latter done often, which of course kills performance if there are cache misses for multiple keys at the same time.
Re:How not to structure database-backed web apps: performanc... by tootie on Thursday June 28, 2018 @10:39AM (#17417204)
I've heard it called Cache Stampede. Any decent framework for memoizing method calls would cover this case though.
Re:How not to structure database-backed web apps: performanc... by patmcguire on Thursday June 28, 2018 @02:07PM (#17419155)
It depends on the backing, if you're using just memcached there's no way to completely lock the key. None of the popular rails caching frameworks I've seen handle this either.
Re:How not to structure database-backed web apps: performanc... by ndrr on Thursday June 28, 2018 @11:52AM (#17417935)
Do you have an example of one? I'm curious what features they provide over ad hoc memoization mechanics.
Re:How not to structure database-backed web apps: performanc... by Jach on Thursday June 28, 2018 @02:23PM (#17419265)
If you're in the Java ecosystem, the CacheBuilder in Guava is pretty good: https://google.github.io/guava/releases/19.0/api/docs/com/go...

By default it handles the case of concurrent retrieval on the same key (the second one will just wait for the first one to finish and use that value rather than starting a duplicate computation). It also lets you configure more interesting things like eviction strategies, removal notifications, and statistics.

Last year was a lesson for me in why caches are a hard problem as I had to debug many cache issues from other people not thinking things through... (At least one of the issues was my own fault. :)) Since then whenever someone suggests we use a cache I instinctively pull out a set of questions[0] to ask. The three questions Guava has you consider can also lead you to using memcached or the like instead but my set tries to answer the question "Do you even need a cache?" and if so, generating helpful design documentation.

    Is the code path as fast as it can possibly be without the cache? Do you have numbers?
    Will the cache have a maximum size, or could it grow without bound?
    If it grows without bound, either because of unbounded entries or because of unbounded memory for any particular entry, under what conditions will it consume all system memory?
    If it has a maximum size, how does it evict things when it reaches that size?
    Are you trying to cache something that could change?
    If so, how is the cache invalidated?
    How can it be invalidated / evicted manually by another thread or signal? (Debuggability, testability, inspectability/monitorability, hit rate and other statistics?)
    Is there a race condition possibility for concurrent stores, retrieves, and evicts?
    How constrained are your cache keys, that is, what does it need to know about to create one?
    Do they need to take into account global info?
    Do they need to take into account contextual information (like organization ID if your application server runs in a multi-tenant system, or user ID, or browser type, or requested-language)?
    Or do they only depend on direct inputs to that code path?
[0] https://www.thejach.com/view/2017/6/caches_are_evil -- need to update it a bit but not much...
Re:How not to structure database-backed web apps: performanc... by dangets on Friday June 29, 2018 @11:09PM (#17429286)
A coworker introduced me to Caffeine which I think shares authors with Guava's cache. Kind of a 2.0 /lessons learned iteration.

https://github.com/ben-manes/caffeine

Re:How not to structure database-backed web apps: performanc... by tootie on Thursday June 28, 2018 @02:36PM (#17419356)
Re:How not to structure database-backed web apps: performanc... by workinfunk on Thursday June 28, 2018 @10:49PM (#17422018)
No it wouldn't, memoization can't provide responses to calls it hasn't seen yet, that's the whole point.