ganglooki.blogg.se

Using lua script in fastoredis
Using lua script in fastoredis













using lua script in fastoredis

We invoke the Lua script with the object's hash value car.hash. In the diagram below we get insights into the request flow our microservice needs to go through when we use a Lua script. Microservice request flow with Lua script

using lua script in fastoredis

For this case, having a Lua script in Redis helps to optimize our performance. This reduces performance as we have to wait for the response from Redis and possibly face network congestion if our Redis instance is serving other requests. Our assumption here is that the object is updated relatively infrequently and therefore most of the time the hash value we receive will match the one in our message, indicating we have the latest version of the object.įor the other cases, where the hash we receive does not match the one in our message, we will have to make an additional request to Redis to fetch the new version of the object. To understand which pathway will be the most frequent we have to understand how frequently the data is updated and the chances that the object we received in the message will be the latest version. In the diagram below we get insights into the request flow our microservice needs to go through without using a Lua script. Car object stored in Redis as a Hash Microservice request flow without Lua script The key is the car.Id and the value is a map with a hash field containing the hash value of the car and a model field containing a JSON serialized string of the model. In Redis, we store the car object as a Hash. To check whether the car in the message is the latest version of the object we can compare the hash property of the car in the message with the hash of the car stored in Redis. There is a case to be made around eventual consistency and whether our microservice should even care if the car it received is the latest version but that's a different discussion that is out of scope for this article. This is an application of the Data Domain Pattern and allows one service to own the data while others can read the data directly, thereby allowing the two services to scale independently while ownership of data is still governed. To do this it requests the car from a different bounded context. Our microservice wants to always operate on the latest car object and therefore needs to verify that the car received in this message is the latest version. The message contains an object car that we want to perform an operation on as shown below. In our scenario, we have a microservice that receives commands via messages on a message bus.

#Using lua script in fastoredis how to#

This will give us a better understanding of how to apply Lua scripts and contextualize them better. Message-based communication in microservicesīefore we dive into how to create a Lua script, let's first describe a scenario where using a Lua script would be useful. If you decide to use Lua scripts ensure that they are lightweight and do not cause excessive load on your Redis instance. You will have to judge if the use-case warrants higher performance or better testability. While Lua scripts provide a performance benefit there are downsides to consider such as testability of your logic and since Redis is single-threaded, complex scripts could slow down your instance and block other calls. With a Lua script you could instead do that computation within Redis, where the data is, and then only return the result. This causes network overhead as you now need to transmit all that data over the network. Usually, you would fetch all the data required for your computation from Redis and then calculate the result. This means that logic that would have been executed within your microservice could be instead be executed inside Redis.

using lua script in fastoredis

Lua scripts allow you to evaluate data within Redis and make decisions based on the parameters you pass and the stored data. Redis can use scripts written in Lua to add functionality and extend its capabilities beyond a key-value store. Its lightweight nature makes it highly applicable to embedded systems such as microcontrollers. Lua is a powerful, efficient, lightweight, embeddable scripting language. We'll walk through an example Lua script and show how it improves network overhead between a microservice and Redis. In this post, we'll learn how to leverage a Lua script in Redis to avoid unnecessary network overhead and do evaluation and transformation of data within Redis. Performance is a key characteristic of any architecture and in a microservices architecture, network overhead between services can cause performance issues.















Using lua script in fastoredis