Redis - A great tutorial

hatzisn

Expert
Licensed User
Longtime User
I have heard a lot of times about redis in the past but I had never had a deep look into it. This tutorial answered a lot of my questions and maybe will do the same for you to:

 

aeric

Expert
Licensed User
Longtime User
Here is a class I build years ago.
 

amorosik

Expert
Licensed User
I've carefully read Redis's features, and while I find it interesting, I haven't yet found an application that could truly benefit from it
Probably, as long as we're talking about small databases (few hundred-thousand tables and a few million rows), the difference with traditional db servers isn't noticeable
Have any of you used it?
What exactly do you use it for?
 

hatzisn

Expert
Licensed User
Longtime User
I've carefully read Redis's features, and while I find it interesting, I haven't yet found an application that could truly benefit from it
Probably, as long as we're talking about small databases (few hundred-thousand tables and a few million rows), the difference with traditional db servers isn't noticeable
Have any of you used it?
What exactly do you use it for?

I have heard a single sentence that sums up the whole basic use of Redis. This sentence is: "The fastest query is the query that will not be executed". In a nutshell: "It is a caching layer for data in a basic approach and expanding it with modules, as you saw, can be used for all types of databases". This is also a benefit: Common data interface for different kind of DBs. Further more connecting each handler of a webapp directly with the database, for a very busy application, the maximum number of connections might quickly be exausted. With several instances of redis you increase dramatically the maximum amount of concurrent users that can "query" the same data.
 

aeric

Expert
Licensed User
Longtime User
Probably
But until I have a full understanding of the possibilities it offers, it's impossible to say.
Until you think your server is becoming slow, the memory becomes a bottleneck, you are running micro-services architecture for so called "modern" application stack, you need to scale or improve the performance of the server, I don't think you need it.
 

OliverA

Expert
Licensed User
Longtime User

OliverA

Expert
Licensed User
Longtime User

aeric

Expert
Licensed User
Longtime User

amorosik

Expert
Licensed User
Until you think your server is becoming slow, the memory becomes a bottleneck, you are running micro-services architecture for so called "modern" application stack, you need to scale or improve the performance of the server, I don't think you need it.

I never wrote that my server is slowing down
And I never wrote about using a microservices-based system
Whether I need Redis or not is irrelevant
The question I asked was: for which system is Redis effective?
 

aeric

Expert
Licensed User
Longtime User
The question I asked was: for which system is Redis effective?
distributed micro services architecture
expensive database queries
cacheable data or contents

It seems the video of this thread "Redis - A great tutorial" is not great and failed to explain well.
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello everyone.
I'm currently testing this Redis client library. I have heard of Redis previously, but not really thought anything of it until I saw this thread.

To tell you the truth, just like @amorosik, I can't find any use for it for myself or for any of my clients. I don't see a good reason why I would personally use it, when I can just use KVS locally if need be in B4X solutions, and then clear KVS when I've finished using it.

Anyway, I’ve wrapped the client code and it’s now working perfectly in B4J. I thought I’d get some inspiration about using it, but it hasn’t inspired me at all.

1785296298803.png

I don’t like the idea of having an extra server running on my development laptop, so I’m going to disable the service until I continue testing this library. Let’s hope I remember to re‑enable the service next time.

Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Redis Pool Initialized: true
Command: HSET | Success: true | Result: 1
HSET completed. Fetching field 'name'...
Command: HDEL | Success: true | Result: 1
Hash field deleted successfully. Result: 1
Command: SET | Success: true | Result: OK
Command: HSET | Success: true | Result: 0
HSET completed. Fetching field 'name'...
Command: HSET | Success: true | Result: 0
HSET completed. Fetching field 'name'...
Command: EXISTS | Success: true | Result: true
Key Exists: true
Command: EXPIRE | Success: true | Result: 1
Expire set successfully. Result: 1
Command: HGETALL | Success: true | Result: {name=Peter, role=developer}
Retrieved All Hash Fields Count: 2
Command: HEXISTS | Success: true | Result: true
Hash Field Exists: true
Command: LPUSH | Success: true | Result: 21
LPUSH list length: 21
Command: RPUSH | Success: true | Result: 22
RPUSH list length: 22
Command: SADD | Success: true | Result: 0
SADD added count: 0
Command: LRANGE | Success: true | Result: [item_left, item_left, item_left, item_left, item_left, item_left, item_left, item_left, item_left, item_left, item_left, item_right, item_right, item_right, item_right, item_right, item_right, item_right, item_right, item_right, item_right, item_right]
LRANGE items count: 22
Command: SMEMBERS | Success: true | Result: [unique_member]
SMEMBERS items count: 1
Command: HGET | Success: true | Result: Peter
Retrieved Hash Field Value: Peter
Command: GET | Success: true | Result: Hello from B4XPages!
Retrieved Value: Hello from B4XPages!
Command: HGET | Success: true | Result: Peter
Retrieved Hash Field Value: Peter
Command: HGET | Success: true | Result: Peter
Retrieved Hash Field Value: Peter

SS_Jedis

Author:
Peter Simpson
Version: 1.0
  • Jedis
    • Events:
      • CommandResult (Command As String, Success As Boolean, Result As Object)
      • Log (Message As String)
      • MessageReceived (Channel As String, Message As String)
      • PoolInitialized (Success As Boolean)
      • SubscribeError (Error As String)
      • TaskRejected (Command As String, Reason As String)
    • Functions:
      • Close
        Close everything: unsubscribe, close pool, shutdown executor.
      • DelAsync (key As String)
        Async DEL
      • ExistsAsync (key As String)
        Async EXISTS
        Checks if a key exists in Redis. Returns a boolean.
      • ExpireAsync (key As String, seconds As Long)
        Async EXPIRE
        Sets a timeout on a key in seconds.
      • GetAsync (key As String)
        Async GET
      • GetRawPool As redis.clients.jedis.JedisPool
        Expose raw pool for advanced usage. Use with caution.
      • HDelAsync (key As String, field As String)
        Async HDel - Hash
      • HExistsAsync (key As String, field As String)
        Async HEXISTS - Hash
        Checks if a specific hash field exists.
      • HGetAllAsync (key As String)
        Async HGETALL - Hash
        Retrieves all fields and values in a hash.
      • HGetAsync (key As String, field As String)
        Async HGET - Hash
      • HSetAsync (key As String, field As String, value As String)
        Async HSET - Hash
      • Initialize (maxThreads As Int)
        Initialize with default queue size (100).
      • InitializeEx (maxThreads As Int, queueSize As Int)
        Initialize with custom thread and queue sizes.
      • InitializePool (EventName As String, host As String, port As Int, timeoutMs As Int, password As String, database As Int, maxTotal As Int, maxIdle As Int)
        Initialises a Jedis Redis connection pool.
        EventName: B4X event prefix used for callbacks (e.g., EventName + "_PoolInitialized").
        Host: Redis server hostname or IP ("127.0.0.1" for local, cloud endpoint for hosted Redis).
        Port: Redis TCP port (default 6379).
        TimeoutMs: Socket timeout in milliseconds (typical 2000-5000).
        Password: Redis authentication password; empty if not required.
        Database: Logical Redis database index (0-15), default is 0.
        MaxTotal: Maximum total connections allowed in the pool.
        MaxIdle: Maximum idle connections kept ready for reuse.
        Notes:
        - Fires EventName + "_PoolInitialized" with success/failure.
        - Failure indicates DNS, socket, or authentication issues.
        - Call Redis.Close() on shutdown to release pool resources.
      • IsInitialized As Boolean
        Returns true if the object has been initialized and not yet closed.
      • LPushAsync (key As String, value As String)
        Async LPUSH - List
        Inserts a value at the head of a list.
      • LRangeAsync (key As String, start As Long, stop As Long)
        Async LRANGE - List
        Retrieves a range of elements from a list.
        So see all the elements, set stop to -1.
      • PublishAsync (channel As String, message As String)
        Async PUBLISH
      • RPushAsync (key As String, value As String)
        Async RPUSH - List
        Inserts a value at the tail of a list.
      • SAddAsync (key As String, member As String)
        Async SADD - Set
        Adds a unique member to a set.
      • SetAsync (key As String, value As String)
        Async SET
      • SetBlockingSubmit (blocking As Boolean)
        Configure whether submissions should block when the queue is full (CallerRunsPolicy) or use the default rejection handler.
        Must be called after Initialize/InitializeEx.
      • SetDebugMode (debug As Boolean)
        Enable or disable debug mode. When enabled, error results include stack traces.
      • SetSubscriberJoinTimeout (timeoutMs As Long)
        Set subscriber join timeout in milliseconds used when waiting for subscriber thread termination during Unsubscribe.
      • SMembersAsync (key As String)
        Async SMEMBERS - Set
        Returns all members of a set as a list.
      • Subscribe (channel As String)
        Subscribe to a channel. Uses a dedicated subscriber thread and Jedis instance.
      • Unsubscribe
        Unsubscribe and attempt to deterministically stop the subscriber thread.

To run in memory you need to installed the following server locally or use an online server. Apparently, there are only a few real online Redis servers in the world that you can connect to externally over TCP.

I've tested this library using the Developer Edition (RC) server in the link below

When will I release this library, how long is a piece is string???


Enjoy...
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Yes, the simplest way we can use is KVS library

Same to you, I wrapped Lettuce to test out Redis but found it has no advantage but adding complexity.

Don't forget we can also use SQLite in-memory for same purpose but with SQL queries instead of key-value pairs like NoSQL

Basically jetty server has cache built-in
Post in thread '[Server] Handling Cache' https://www.b4x.com/android/forum/threads/server-handling-cache.141502/post-896890

Erel also wrapped the Caffeine library for server cache

In my Pakai Server template, I just use B4X Maps with MiniHTML3 (Cache.bas)
Post in thread '[B4X] MiniHTML3' https://www.b4x.com/android/forum/threads/b4x-minihtml3.171326/post-1047990

There is also B4XCache

There are so many alternative ways to use cache besides using Redis.

However, Redis may be help to improve the loading performance compared to uncached method but same do other alternatives I mentioned above could provide.

If you read the comments for the video on first post, I think you will agree that the video is very much an advertisement for Redis. It's very confusing.

Here I found a better greater tutorial. It shows the real use case of Redis, which is to generate JSON API responses.
 
Top