Categories
Technology

Redis

What is Redis?

Redis is an open-source, in-memory data structure store that may be used as a database, cache, or message broker. It supports several data structures including strings, hashes, lists, sets, sorted sets, bitmaps, hyper loglogs, and geospatial indexes. Redis is characterized by its performance, simplicity, and versatility.

Key Features of Redis

  • In-Memory Data Store: Held in memory, with ultra-fast read and write operations.
  • Persistence: Offers methods such as snapshotting and append-only file (AOF).
  • Data Structures: Supports strings, hashes, lists, sets, sorted sets, bitmaps, and more.
  • Pub/Sub Messaging: Allows for real-time messaging and notifications.
  • High Availability: Implements Redis Sentinel for monitoring and fail-over.
  • Cluster Support: Goes all the way towards data distribution across multiple nodes with Redis Cluster.
  • Extensibility: Provides for modules of custom commands and functionality.
  • Lua Scripting: Can execute Lua scripts natively on the server. Atomic Operations: Commands like increment, decrement, and list operations are atomic in nature.
  • Replication: Provides for master-slave replication, thus making sure data is not lost in case of failure, which can easily scale to higher data.

Uses of Redis

  • Caching: Stores frequently accessed data to decrease database load and latency.
  • Session Management: User sessions in web applications can be managed.
  • Real-time Analytics: It is capable of handling real-time data streams like user activity or stock prices tracking.
  • Leaderboards: This helps to build a real-time ranking system by making use of sorted sets.
  • Message Queues: This can act as a lightweight message broker.
  • Geospatial Indexing: It stores and queries geospatial data in an efficient way.
  • Pub/Sub Systems: One can use this to build a chat application, notification, or live feeds.
  • Machine Learning: Serve pre-computed ML models and features.
  • Gaming Applications: Manage game states, leaderboards, and matchmaking.

How to Implement Redis

1. Installation

  • Linux: Use apt-get install redis or yum install redis.
  • macOS: Install via Homebrew: brew install redis.
  • Windows: Use WSL or download from third-party Redis for Windows projects.

2. Basic Commands

  • Start Redis server: redis-server
  • Connect using the CLI: redis-cli
  • Common commands:
    a) SET key value: Set a value.
    b) GET key: Retrieve a value.
    c) DEL key: Delete a key.
    d) EXPIRE key seconds: Set expiration time.
    e) INCR key: Increment value

3. Config

Modify the redis.conf configuration for parameters, such as Persistence: Persist to AOF by append only yes. Security: Add a password using the requirepass. Max Memory: Specify limitations with max memory and eviction strategies. Log: Configure levels and logfiles.

4. Client libraries Redis supports many client libraries in multiple languages such as Python (redis-py), Node.js (ioredis), Java (Jedis), and PHP (Predis or phpredis).

Redis in Laravel Framework

Redis is neatly supported in Laravel because Laravel supports Redis out of the box.

  1. Prerequisites

Install Redis on your system and ensure that Redis is running.

2. Installation of PHP Redis Extension

You can install the PHP Redis extension by using the following command: Linux/macOS: pecl install redis Windows: Copy the Redis DLL to the PHP extensions folder and uncomment it in php.ini.

3. Laravel Configuration

Add Redis to the config/database.php file:

'redis' => [

 'client' => env('REDIS_CLIENT', 'phpredis'),

 'default' => [

'host' => env('REDIS_HOST', '127.0.0.1'),

'password' => env('REDIS_PASSWORD', null),

'port' => env('REDIS_PORT', 6379),

'database' => env('REDIS_DB', 0),

 ],

'cache' => [

 'host' => env('REDIS_HOST', '127.0.0.1'),

 'password' => env('REDIS_PASSWORD', null),

 'port' => env('REDIS_PORT', 6379),

 'database' => env('REDIS_CACHE_DB', 1),

 ],

 ],

Update the .env file:

  • REDIS_CLIENT=phpredis
  • REDIS_HOST=127.0.0.1
  • REDIS_PASSWORD=null
  • REDIS_PORT=6379
  • REDIS_DB=0
  • REDIS_CACHE_DB=1

Usage in Laravel

Caching:

  • Cache::store(‘redis’)->put(‘key’, ‘value’, 3600); // Store in cache
  • Cache::store(‘redis’)->get(‘key’); // Retrieve from cache
  • Session Storage: Update SESSION_DRIVER in.env to redis.
  • Queues: Set QUEUE_CONNECTION to redis in.env.
  • Broadcasting: Configure Redis as a broadcasting driver to implement real-time notifications.

Best Practices with Redis

  1. Monitor Usage: Use redis-cli or tools like RedisInsight.
  2. Set Expiration: Employ EXPIRE or TTL to stop uncontrolled growth.
  3. Utilize Namespaces: Use the keys with prefix to better arrange.
  4. Use Redis Cluster: Scale up to handle large-sized applications.

Secure Redis:

  • Make use of secure passwords
  • Redis bind only on specific IP.
  • Activate TLS.
  • Back up the server: Snapshotting or AOF should be turned on.
  • Minimize abuse: Redis can only be utilized when data stored needs in-memory speeds.
  • Code Efficiency: Implement Lua script codes for extensive computations.
  • Optimized Eviction: Select an appropriate eviction policy for eviction such as volatile-lru, allkeys-lru etc.

Tools and Libraries

  1. RedisInsight- GUI management for Redis
  2. Predis: a PHP extension allowing interaction with Redis
  3. Redis Modules:
    a) RedisJSON : Json support
    b) RediSearch : Full text Search
    c) RedisTimeSeries: manage time-series data
    d) RedisBloom : Probabilistic Data Structures
    e) Redis Sentinel- provide high availability for Redis applications automatically
    f) RedisGraph: Redis’ graph database.

Connect with Redis client API libraries:

Use the Redis client libraries to connect to Redis servers from your own code. Following client libraries for six main languages:

LanguageClient nameDocs
Pythonredis-pyredis-py guide
PythonRedisVLRedisVL guide
C#/.NETNRedisStackNRedisStack guide
JavaScriptnode-redisnode-redis guide
JavaJedisJedis guide
JavaLettuceLettuce guide
Gogo-redisgo-redis guide
PHPPredisPredis guide

Community-supported clients 

The table below shows the recommended third-party client libraries for languages that Redis does not document directly:

LanguageClient nameGithubDocs
Chiredishttps://github.com/redis/hiredishttps://github.com/redis/hiredis
C++Boost.Redishttps://github.com/boostorg/redishttps://www.boost.org/doc/libs/develop/libs/redis/doc/html/index.html
Dartredis_dart_linkhttps://github.com/toolsetlink/redis_dart_linkhttps://github.com/toolsetlink/redis_dart_link
PHPPhpRedis extensionhttps://github.com/phpredis/phpredishttps://github.com/phpredis/phpredis/blob/develop/README.md
Rubyredis-rbhttps://github.com/redis/redis-rbhttps://rubydoc.info/gems/redis
Rustredis-rshttps://github.com/redis-rs/redis-rshttps://docs.rs/redis/latest/redis/

redis-py guide (Python)

Connect your Python application to a Redis database

redis-py is the Python client for Redis. The sections below explain how to install redis-py and connect your application to a Redis database.

redis-py requires a running Redis or Redis Stack server. See Getting started for Redis installation instructions. You can also access Redis with an object-mapping client interface.

Install

To install redis-py, enter:

pip install redis

For faster performance, install Redis with hiredis support. This provides a compiled response parser, and for most cases requires zero code changes. By default, if hiredis >= 1.0 is available, redis-py attempts to use it for response parsing.

pip install redis[hiredis]

Connect and test

Connect to localhost on port 6379, set a value in Redis, and retrieve it. All responses are returned as bytes in Python. To receive decoded strings, set decode_responses=True. For more connection options, see these examples.

r = redis.Redis(host='localhost', port=6379, decode_responses=True)

Store and retrieve a simple string.

r.set('foo', 'bar')
# True
r.get('foo')

# bar

Store and retrieve a dict.

r.hset('user-session:123', mapping={
    'name': 'John',
    "surname": 'Smith',
    "company": 'Redis',
    "age": 29
})
# True

r.hgetall('user-session:123')
# {'surname': 'Smith', 'name': 'John', 'company': 'Redis', 'age': '29'}

Redis is a powerful and versatile tool for all kinds of use cases, from caching to real-time analytics. With its increasing feature set and community support, Redis remains a critical component of modern application architecture. Its flexibility and performance make it an essential technology for developers who want to build scalable, high-performance applications.

References:
https://dev.to/woovi/simple-cache-with-redis-5g3a
https://laravel.com/docs/11.x/redis
https://redis.io/docs/latest/develop/clients/
https://redis.io/ebook/part-1-getting-started/chapter-1-getting-to-kn…

Categories
Technology

Augmented Reality (AR) Vs Virtual Reality (VR) 

Augmented Reality (AR) Vs Virtual Reality (VR) 

Augmented reality is an interactive experience that combines the real world and computer-generated content. Augmented reality is made up of the word “augment” which means to make something great by adding something to it. So basically, augmented reality is a method by which we can alter our real world by adding some digital elements to it. This is done by superimposing a digital image on the person’s current view thus it enhances the experience of reality.  

Augmented reality (AR) is the integration of digital information with the user’s environment in Real Time. Unlike virtual reality (VR), which creates a totally artificial environment, AR users experience a real-world environment with generated perceptual information overlaid on top of it. 

Virtual Reality (VR) 
Virtual reality is a simulated experience that employs pose tracking and 3D near-eye displays to give the user an immersive feel of a virtual world. Applications of virtual reality include entertainment (particularly video games), education (such as medical or military training) and business. 

Examples 

 

    • AR can be used in mobile apps to display information about landmarks when you point your phone’s camera at them. 

    • VR is often used in gaming, simulations, or virtual tours where users can explore and interact with a digital world. 

    • Snapchat uses AR technology to apply interactive filters to users’ faces in real-time. These filters can include animations, masks, and effects that augment the user’s appearance, making for fun and engaging social interactions. 

    • Google Maps integrates AR to provide users with real-time navigation guidance through their smartphone cameras. Users can see directional arrows and labels overlaid onto the real world, helping them navigate unfamiliar environments more easily. 

    • In Pokémon GO, AR is used to overlay Pokémon onto the real-world environment captured by the player’s smartphone camera. This allows players to see Pokémon appear as if they are actually present in the physical world around them. It adds an extra layer of immersion to the game, making the Pokémon-catching experience more lifelike and engaging. 

Some Differences  

 

    • Virtual Reality creates a fully immersive digital environment or experience that simulates the real world or imaginary world. 

    • Augmented Reality overlays digital information into the real world. 

    • It generally requires a headset or a similar kind of device to immerse the user into the digital world. 

    • It can be accomplished through smartphones or tablets with the help of AR apps. 

Different Industries where we can see Usages of AR and VR technologies 
Manufacturing: 

AR enhances manufacturing processes by providing real-time data visualization, facilitating data-driven decisions, and promoting collaboration. 

VR is utilized by automotive companies like Honda, BMW, and Jaguar Land Rover for design and engineering reviews, reducing the need for physical prototypes and saving time and resources. 

Healthcare: 

AR glasses aid surgeons by projecting medical images during surgeries, improving precision. 

VR in healthcare includes FDA-approved systems like EaseVRx for pain reduction and therapeutic applications for mental health issues, such as Virtual Reality Exposure Therapy for PTSD and anxiety. 

Gaming: 

AR gaming, exemplified by Pokémon GO, combines virtual and real-world environments for an immersive experience. 

VR gaming offers a fully immersive experience, enhancing traditional gaming and bringing entertainment beyond smartphones. 

Education: 

AR tools like Third Eye X2 smart glasses enhance traditional teaching by providing immersive experiences and facilitating remote learning. 

VR revolutionizes education by offering immersive and experiential learning opportunities, democratizing access to education worldwide. 

These technologies have diverse applications across manufacturing, healthcare, gaming, and education, contributing to enhanced experiences and improved processes. 

Some Videos About Real time usage of AR and VR technologies in medical fields