Using Redis in practice

Asked

Viewed 3,768 times

4

I’m having trouble understanding in practice where to use Redis in an Ecommerce, for example.

I’m reading a book but I can not understand what I have to take into account when deciding whether to use Redis or not.

Redis should be used for static data?

In the case of Ecommerce, use Redis to record login or shopping cart data and then permanently record to a database like Mongodb or Mysql, etc...?

3 answers

10

Friend, I use Redis in an Commerce and I think I can give you a very simple and practical idea of its use. Before using Redis I used memcached so let’s try to understand at what point it helped me.

1º My store had a lot of access and I could not every time the user went in the catalog or on the detail page load the product through the database so I created a very simple and common "cache" implementation that is:

1º The user asks the id product page 14325 2º My data Adapter of my application sees if there is a key in the redis called product_id_14325 (the name I am defining), if there is I return the content of the redis, that is, I do not consult in the bank. 3º If the key does not exist in redis then I search in my database and return I pass to the user, serializo it and persist in redis. Ready any user who access this detail page it is curly.

There are other approaches such as creating a process to store all your products in the cache and leave with expiration of 1 day, and minor updates for modified products, but this depends on your cache strategy, what matters is that the redis has a utility would.

  • Thanks for the tip, I do not understand why someone gave -1, I understand little of Redis yet but the tip is valid yes, the product information does not change constantly.

  • Do you know of any scenarios where Redis is used to maintain authentication states? That is, the user logs in and something is persisted and read in Redis in the next operations? I have a scenario that I want to authenticate with JWT and use Redis but I don’t know if it applies to the case.

7

Banks Nosql, means "Not only SQL":

These arose from the need to scale relational databases with ACID properties in high-availability web projects that operate on a large scale. Its main features are high performance, scalability, easy replication and structured data support.

This break with SQL standards always causes great repercussion and many discussions loaded with feelings and emotions, but the truth is that relational databases still serve to solve many problems that not always (You see, not always) can be solved with Nosql banks, for example:

  • Need for strong data consistency, well-defined typing, etc;
  • Complex searches that require a relational data model for instructions and join operations, for example;
  • Data that exceeds the availability of server memory, however we may use swap, no one wants to impair performance in this case.

Nosql Redis:

Storage of user sessions

This is a very simple template on how to use Redis to save a user’s session information.

For each session, a key is generated that is saved to the browser cookie. With this key, the system has access to a hash with information from this session: login status, clicked products and advertisements, language preferences and other temporal settings, which expire after a few hours.

The benefit of not storing such session information directly in the cookie is evident: we have gained the data integrity security, not at the risk of some malicious user modifying them. With Redis, we use simple get/set operations to access this data directly from the server’s memory (or servers, if there is more than one), no waste of resources, thanks to efficient expiration system promoted by this Nosql.

The expiration algorithm does not monitor 100% of the keys that can expire. Just like most cache systems, keys are expired when a client attempts to access them. If the key is expired the value is not returned and the record is removed from the database.

In databases that record many data that lose validity over time, as in this example, some keys would never be accessed again consequently they would never be removed. These keys need to be removed somehow, so every second Redis tests a random set of keys that may be expired. The algorithm is simple, with each run:

  • Tests 100 keys with set expiration.
  • Delete all expired keys.
  • If more than 25 keys are invalid the algorithm resumes from 1.

This probabilistic logic continues to expire until our set of valid Keys is close to 75% of the records.

Research source: Link Nosql Redis

  • I appreciate your time but had already read this, what I wanted was in practice to know how to apply in an Ecommerce, this text above is like studying theory to ride a bike, in practice you will fall!

1

I run a website development platform and use Redis and Mysql on it.

Mysql, of course, stores all the system data, including the data that renders the websites. But why then do I have a redis database along with this Mysql? Simple: speed.

By providing sites, I always want the site to open as quickly as possible, both to improve SEO and to improve the user experience, and to achieve this brand, I use a redis database to store the data that render the sites(which in this case are all in JSON), since being a database that runs in memory, the access is much faster than if the system had to somehow query the file system in search of this data. This redis database is updated whenever the customer updates any content that is relevant to the rendering of the site or if our VPS is restarted.

In short, Redis is extremely useful, as was my case, to store data that needs to be delivered quickly.

Now imagine that same situation, for an e-commerce.

The customer goes and registers the goods, and these data are sent to a bank in memory to be consumed by your client. This, among many things, would improve the experience of site visitors as it would greatly reduce the loading time of product data, since instead of having to consult the file system or an api, the data would already be available in the server memory. This should be taken into account, since slower systems have greater chances of losing access due to time-consuming loads, which can cause timeout errors, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.