Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Databases»From caching to real-time analytics: Essential use cases for Amazon ElastiCache for Valkey

    From caching to real-time analytics: Essential use cases for Amazon ElastiCache for Valkey

    December 7, 2024

    Valkey is an open-source, distributed, in-memory key-value data store that offers high-performance data retrieval and storage capabilities, making it an ideal choice for scalable, low-latency modern application development. Originating as a fork of Redis OSS following recent licensing changes, Valkey maintains full compatibility with its predecessor while providing high performance alternative for its developers. Valkey provides a rich set of data structures, that can be used for wide range of use cases, including caching, session management, real-time analytics, and messaging systems. Valkey’s ability to process millions of requests per second with extremely low latency makes it as an ideal choice for high-performance applications.

    Amazon ElastiCache is a fully managed, Valkey-compatible, in-memory caching service that delivers real-time, cost-optimized performance for modern applications. It allows developers to easily deploy and manage in-memory caches without manual administration, supporting millions of operations per second with microsecond response times. ElastiCache is designed to enhance the performance of modern applications while providing enterprise-grade security and reliability.

    For applications with unpredictable traffic patterns, ElastiCache provides a serverless caching option that can be deployed in under a minute and instantly scales to match workload demand, removing the need for capacity planning and manual infrastructure management. This service follows a pay-as-you-go pricing model based on actual usage, making it cost-effective for variable workloads.

    In this post, we explore some of the most common use cases of ElastiCache for Valkey, demonstrating how its unique capabilities enable faster, more scalable, and resilient applications across various industries. We focus on the flexible data structures of Valkey and how to use these data structures for implementing solutions for common use cases.

    Common use cases implemented using ElastiCache for Valkey

    The following are common use cases for ElastiCache for Valkey:

    • Caching – ElastiCache for Valkey is extensively used as a caching layer to improve application performance by storing frequently accessed data in memory. This reduces the load on databases and improves response times. Popular examples include:
      • Web application caching – ElastiCache for Valkey can cache user sessions, HTML fragments, or API responses to speed up web applications across retail, media, gaming, healthcare, and financial services.
      • Database query caching – ElastiCache for Valkey can cache the results of expensive database queries, reducing the load on the database and improving response times. For example, Valkey can be used in e-commerce platforms for caching product catalogs and search results.
    • Session management – ElastiCache for Valkey is commonly used for storing and managing user session data in web applications. Its fast read/write operations and built-in data expiration make it ideal for this use case. Popular examples include:
      • Shopping cart data – e-commerce platforms can use ElastiCache for Valkey to store shopping cart data, providing a seamless user experience across multiple requests.
      • User preferences and authentication – Social media platforms and online forums can use ElastiCache for Valkey to store user preferences, authentication tokens, and other session-related data.
    • Real-time analytics – ElastiCache for Valkey’s ability to handle high-speed data ingestion and real-time processing makes it suitable for real-time analytics applications. Popular use cases include:
      • Real-time leaderboards – Online gaming platforms can use ElastiCache for Valkey to maintain real-time leaderboards and rankings for their games.
      • Real-time recommendations – e-commerce and media streaming platforms can use ElastiCache for Valkey to store user activity data and provide real-time recommendations based on user behavior.
    • Messaging and queuing – ElastiCache for Valkey built-in data structures like lists, streams, and sorted sets make it suitable for implementing messaging and queuing systems. Popular examples include:
      • Task queues – Web applications and backend services use ElastiCache for Valkey to manage task queues for asynchronous job processing, such as sending emails, processing uploads, or handling background tasks.
      • Pub/Sub messaging – ElastiCache for Valkey’s built-in publish/subscribe functionality can be used to implement real-time messaging systems for applications like chat servers, real-time collaboration tools, and notification systems.
    • Rate limiting applications – Rate limiting is a common use case for ElastiCache for Valkey, particularly in the context of web applications and APIs. Rate limiting is a technique used to control the rate at which requests are processed by a system, preventing it from being overwhelmed by excessive traffic or abuse. ElastiCache for Valkey can be integrated with load balancers, API gateways, or service meshes to enforce rate limiting at different levels (such as per client, per API endpoint, or per service).
    • Media streaming – Media streaming platforms require efficient handling of high-throughput, real-time data for delivering content, tracking user interactions, and ensuring seamless playback experiences. Valkey Streams, with its ability to manage ordered logs of events and real-time high-throughput data processing, provides a powerful solution for these requirements.
      • Live Event Streaming Analytics: Collect and process real-time metrics like viewer count, engagement (such as likes, comments), and playback quality.
      • Live Chat and Audience Interaction: Manage real-time messaging for live-streamed events (such as Q&A, audience chat during sports events).
      • Real-Time Recommendations: Provide personalized recommendations during live streams, such as suggesting related content or advertisements.

    Flexible data structures

    Valkey is not just a simple key-value store—it’s a versatile datastore that offers a wide array of powerful, in-memory data structures. Valkey’s rich set of data structures and its ability to handle large volumes of data with minimal latency make it a powerful tool for modern application development. These built-in data types, such as strings, hashes, lists, sets, geospatial index, and more, provide developers with the flexibility to model complex data relationships and build efficient, high-performance applications. Each data structure in Valkey is optimized for specific use cases, allowing developers to choose the best tool for the job without compromising performance.

    For example, lists are ideal for implementing queues, sets support fast membership checks, and streams are perfect for handling real-time event data. This flexibility allows Valkey to serve as a backbone for a variety of applications, from social media feeds and leaderboards to financial transaction processing and Internet of Things (IoT) data collection.

    The following table provides a summary of Valkey data structures and their common use cases.

    Data StructureDescriptionCommon Use Cases
    StringSimple key-value storageCaching, session tokens, counters, configuration values, user authentication, and basic data storage
    HashKey-value pairs within a single keyUser profiles, metadata storage, product information, and storing structured data like JSON objects
    ListOrdered collection of stringsTask queues, message queues, latest news feeds, comments on posts, and storing recent activities
    SetUnordered collection of unique valuesTags, tracking unique views or actions (such as upvotes and followers), and managing user permissions
    Sorted SetOrdered collection of unique values with a score for eachLeaderboards, ranking systems, time-based event sorting, and priority queues for messaging
    StreamLog-like data structure for ordered sequences of entries.Real-time data processing, event sourcing, live chat, IoT data ingestion, and activity tracking
    BitmapCompact storage of bits (0s and 1s)User activity tracking (such as daily check-ins), feature usage toggles, and tracking online statuses
    HyperLogLogProbabilistic data structure for cardinality estimation.Counting unique users, tracking unique page views, and estimating large sets without high memory use
    GeospatialStore and query geographic coordinatesLocation-based services, proximity search, ride-hailing services, and social media check-ins

    Here, we focus on the string data structure and how to use it for a caching use case.

    Strings are the simplest and most commonly used data type in Valkey. A string in Valkey can store any type of data, including text, numbers, or binary data such as serialized objects, images, or videos. Strings support basic operations like setting, getting, incrementing, and decrementing numeric values, which are performed with extremely low latency due to Valkey’s in-memory architecture.

    One of the most popular use cases for Valkey is caching frequently accessed data to improve the performance and responsiveness of web applications. By storing data in-memory, Valkey can serve requests much faster than querying a traditional database, significantly reducing latency and server load.

    Imagine an e-commerce website that displays product details like images, prices, descriptions, and reviews. When a user browses different product pages, the website needs to fetch this information to render the page. If this data were retrieved from a traditional database each time a user requests it, the process could become slow and resource-intensive, especially during peak traffic. If the same data can be cached using an in-memory data store like Valkey, this can significantly improve the latencies of the application and thereby improve customer experience.

    ElastiCache for Valkey as a database cache

    ElastiCache for Valkey can be used as a caching layer to reduce database load and speed up response times. By storing frequently accessed data in Valkey, web applications can quickly retrieve cached content without having to query the database. This reduces the time required to serve a request, increases scalability, and decreases the load on the underlying database.

    ElastiCache for Valkey is a great choice for implementing a highly available, distributed, and secure in-memory cache because it can serve frequently requested items at sub-millisecond response times, and by doing so, can ease the load off your relational or NoSQL databases and applications. Valkey stores all data in memory, allowing for extremely fast data access handling millions of operations per second with microsecond responses. Apart from high performance, Valkey uses asynchronous I/O threading system allowing for parallel processing of commands and I/O operations, maximizing throughput and minimizing bottlenecks. Valkey supports asynchronous replication that allows you to build highly available solutions providing consistent performance and reliability.

    The following diagram illustrates an example architecture and workflow.

    Lazy loading is a popular caching strategy where data is loaded into a cache only when it is requested for the first time (on a cache miss). When an application or system tries to access a particular piece of data that is not yet in the cache, the data is fetched from the underlying data source (such as a database), stored in the cache, and then returned to the requester. Subsequent requests for the same data can then be served directly from the cache, avoiding the need to query the data source again.

    A real-world example of this caching strategy is employed by e-commerce platforms that cache product catalog data in an in-memory data store like ElastiCache for Valkey to provide a seamless experience with minimal latency.

    By using Valkey as a caching layer, the website can improve its performance as follows:

    • Initial request – When a user visits a product page for the first time, the application queries the database to retrieve the product information. The retrieved data is then stored in Valkey with a unique key (for example, product:12345) corresponding to the product ID, along with an expiration time (TTL) to make sure the data remains fresh.
    • Subsequent requests – For future requests to the same product page, the application first checks Valkey for the cached data using the key product:12345. Depending on if the data is present, it takes the following actions:
      • If the data is present in the cache (a cache hit), Valkey returns the data almost instantly, eliminating the need for a database query and significantly reducing load times.
      • If the data is not found in Valkey (a cache miss), the application retrieves it from the database, stores it in Valkey, and serves it to the user.
    • Cache expiration and invalidation – Cached product data can be set to expire after a certain period (for example, 10 minutes) to make sure users see the latest information, or it can be invalidated when updates occur (for example, price changes or inventory updates).

    The following is a sample Python function that first checks if the user data is in the cache. If not, it queries the database and then stores the result in the cache for future use.

    import os, valkey, pymysql, boto3, json, hashlib
    import cachetools.func
    
    class DB:
        def __init__(self, **params):
            params.setdefault("charset", "utf8mb4")
            params.setdefault("cursorclass", pymysql.cursors.DictCursor)
    
            self.mysql = pymysql.connect(**params)
    
    class SecretsManagerProvider(valkey.CredentialProvider):
        def __init__(self, secret_id, version_id=None, version_stage='AWSCURRENT'):
            self.sm_client = boto3.client('secretsmanager')
            self.secret_id = secret_id
            self.version_id = version_id
            self.version_stage = version_stage
    
        def get_credentials(self) -> Union[Tuple[str], Tuple[str, str]]:
            @cachetools.func.ttl_cache(maxsize=128, ttl=24 * 60 * 60) #24h
            def get_sm_user_credentials(secret_id, version_id, version_stage):
                secret = self.sm_client.get_secret_value(secret_id, version_id)
                return json.loads(secret['SecretString'])
            creds = get_sm_user_credentials(self.secret_id, self.version_id, self.version_stage)
            return creds['username'], creds['password']
    
    
    # Time to live for cached data
    TTL = 600
    
    # Connect to the Valkey cluster
    my_secret_id = os.environ.get('EC_SECRETS_ID')
    creds_provider = SecretsManagerProvider(secret_id=my_secret_id)
    cache = valkey.Valkey(host="localhost", port=6379, credential_provider=creds_provider)
    cache.ping()
    # Connect to the MySQL database
    database = DB(host=<hostname>, user=<username>, password=<password>, db=<database>)
    cursor = database.cursor()
    
    def get_user_from_db(user_id):
        """Query the database for user information based on user_id."""
        query = "SELECT name, email FROM users WHERE id = %s"
        cursor.execute(query, (user_id,))
        result = cursor.fetchone()
        return result
    
    def get_user(user_id):
        """Retrieve user data, using Valkey as a cache."""
        cache_key = f"user:{user_id}"
        
        # Check if the data is in the cache
        user_data = cache.get(cache_key)
        
        if user_data:
            print("Cache hit!")
            return eval(user_data)  # Convert string back to a dictionary
        
        print("Cache miss! Querying the database...")
        
       # Query the database
        user_data = get_user_from_db(user_id)
        
        if user_data:
            # Convert the tuple from the database to a dictionary
            user_data = {"name": user_data[0], "email": user_data[1]}
            
            # Store the result in the cache with a TTL (e.g., 10 minutes)
            cache.setex(cache_key, TTL, str(user_data))
        
        return user_data
        
    if __name__ == "__main__":
        user_id = 12345  # Example user ID
        
    # First call: Cache Miss, should query the database and cache the result
        user = get_user(user_id)
        print(f"User Data: {user}")
        
    # Subsequent calls - should retrieve the result from the cache
        user = get_user(user_id)
        print(f"User Data: {user}")
    

    Adding a cache using ElastiCache for Valkey offers numerous advantages that can significantly improve the performance, scalability, and reliability of an application:

    • Improved application performance – Valkey is an in-memory datastore, meaning it can retrieve data much faster than traditional disk-based databases. This dramatically reduces the time it takes to access frequently used data, leading to lower latency for end-users.
    • Handling high throughput – Valkey can handle a high number of read and write operations per second, making it suitable for applications with high traffic and large volumes of data requests.
    • Reduced database load – By caching frequently accessed data, Valkey reduces the number of direct queries to the primary database, which can prevent the database from becoming a bottleneck and reduce the overall load on backend systems.
    • Reduced load times – By caching content that needs to be delivered to users quickly, such as user sessions, API responses, or frequently accessed webpages, Valkey can help reduce load times and improve the overall user experience.
    • Reduced infrastructure costs – Valkey can generate high throughout with minimal hardware footprint when compared to disk-based systems. Serving data from a Valkey cache can be much cheaper than repeatedly querying a primary database, especially for read-heavy workloads.

    Summary

    ElastiCache for Valkey is an in-memory data store designed for managing data in real-time applications. It supports a wide range of data structures, including strings, hashes, lists, sets, sorted sets and geospatial index types, making it suitable for diverse use cases such as caching, session management, task queues, real-time analytics, and location-based services. As a high-performance database, message broker, and real-time analytics engine, ElastiCache for Valkey remains an ideal choice for building reliable and low-latency solutions across various industries. Its ability to handle high-throughput scenarios makes it the preferred choice for developers seeking speed, scalability, and simplicity in their applications.

    With ElastiCache Serverless for Valkey, you can create a cache in under a minute and get started as low as $6/month. Try out ElastiCache for Valkey for your own use case, and share your feedback in the comments.


    About the Author

    Siva Karuturi is a Sr. Specialist Solutions Architect for In-Memory Databases. Siva specializes in various database technologies (both Relational & NoSQL) and has been helping customers implement complex architectures and providing leadership for In-Memory Database & analytics solutions including cloud computing, governance, security, architecture, high availability, disaster recovery and performance enhancements. Off work, he likes traveling and tasting various cuisines Anthony Bourdain style!

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticlePrevent transaction ID wraparound by using postgres_get_av_diag() for monitoring autovacuum
    Next Article Automate pre-checks for your Amazon RDS for MySQL major version upgrade

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 15, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-30419 – NI Circuit Design Suite SymbolEditor Out-of-Bounds Read Vulnerability

    May 15, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    India’s RBI Introduces Exclusive “bank.in” Domain to Combat Digital Banking Fraud

    Development

    Unable to locate button via Selenium WebDriver

    Development

    CVE-2025-3647 – Moodle Information Disclosure

    Common Vulnerabilities and Exposures (CVEs)

    Revealing the UTG-Q-010 Campaign: A Deep Dive into Cryptocurrency Lures and Pupy RAT

    Development
    Hostinger

    Highlights

    Development

    Indonesia National Data Center Hack Disrupts Government Services, Affecting Over 200 Agencies

    June 24, 2024

    A ransomware attack on Indonesia’s national data center has disrupted official government services. The attack…

    How Salesforce’s 5-level framework for AI agents finally cuts through the hype

    April 10, 2025

    Kickpad – experimental kick drum audio sample generator

    February 22, 2025

    Growth as a junior UX designer

    August 13, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.