What is the best way to invalidate cache in Redis? - caching

I can only think of Redis messaging. But isn't that costly? I do not want to use template.expire() either because it is static. What I need is to re-populate cache whenever there is a new information in the DB. Thanks!

Related

Is it possible to enable persistance for some part of the Redis data?

I want to use Redis RDB persistence, but I need persistency for only small part of the data. How can I configure Redis to take snapshot for some part of data. Is it possible or I need 2 different Redis DB?
No, you can't do it. You'll need 2 different Redis instances.

Using Redis Pub/Sub to persist data into an RDBMS

I was experimenting with using Redis for caching and got a bit confused with persistence. I would like to create a service that automatically writes into a PostgreSQL database every time the Redis cache is updated.
My initial thought was to use the Pub/Sub feature to listen to events and then trigger the appropriate queries that write into my RDBMS. Though, after looking some more into the Pub/Sub feature it appears that it is probably not be meant to be used this way.
It seems that the standard practice is to keep that logic inside of the REST API that queries Redis. I would however like to have a service that automatically does this in the background. Is there a simpler way of doing this that I am missing ?
Thank you in advance.
I guess the common way is to Save data to MongoDB and Redis manually.
for example when a user post new message:
newPost = new Post("Hello world!");
// save to db and get model ID
newPost->save();
// save model to Redis
newPost->saveToCache();
This approach will guarantee that all new Models will store in database even when Redis is down.
An other approach is to save data in Redis first, and add a flag to a Redis List, Then(anytime later) read flags from list and save related data to database.

what are the best approaches (practices) to create stateful microservices?

I need to create a food ordering service, using microservices, scalable , cluster, several steps to order. Need to store user data between steps / requests.
What is an approach to keep state and user data? Store it in DB? Cache? Shared memory?
Are there any tutorials for the best practice of it?
(I gonna use spring / springboot and modules)
Anything that you cannot afford to lose (usually the business data) will go in DB and can be parallelly cached in an in-memory DB like Redis that has a cache eviction algorithm inbuilt.
Anything that, if lost, is not a big deal (usually the technical things that are not directly linked with the business data) can go only in an in-memory DB.
Since you are using Spring, you could probably use something like Redis with Spring Data Redis. There are already known Spring solutions (such as this) to fall back on api calls to fetch data from DB if the Redis server goes down. You can also run multiple Redis instances behind Redis Sentinel to provide failover. Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes. Also, you can configure Redis to persist the data in file system once daily or so to backup the cache data for disaster recovery.
If you are looking for a fully managed service, AWS provides "Step Functions" to satisfy your stateful requirements: https://stackoverflow.com/questions/tagged/aws-step-functions

Application data in Sinatra

Say I have some objects that need to be created only once in the application yet accessed from within multiple requests. The objects are immutable. What is the best way to do this?
Store them in the session.
If you don't want to lose them after a server's restart, then use a database (SQLite, which is a single file, for example).
You want to persist your objects. Normally you'd do it with some ORM like Active Record or Datamapper. Depending on what is available to you. If you want something dead simple without migrations and you have access to a MongoDB use mongomapper.
If that object is used only for some time, then it is discarded (and if needed again, then recreated), use some caching mechanism, like memcached or redis.
If setting up such services is heavy and you want to avoid it, and - say - you are using Debian/Ubuntu then save your objects into files (with Marshal-ing) in the /shm device which is practically memory.
If the structure of the data is complex, then go with SQLite as suggested above.

Best way to cache persistent data (e.g. code books) in Spring?

I have a series of code books in my database, and I am using plain JDBC calls to fetch them and store them in a collection. I would like to put these in some kind of a cache at application startup time in order to save time later.
I don't need any fancy stuff like automatic object invalidation, TTL etc - the code books change rarely, so I'll trigger the update myself and just reload the whole cache when the need arises.
The project where I need this uses Spring, and this is my first project using it. Is there a standard/elegant way to do this in Spring?
Thanks.
Check out Spring-cache.
Supports EHCache, OSCache and a memory cache, but allows pluggable cache providers too.

Resources