e-Book - Redis in Action

This book covers the use of Redis, an in-memory database/data structure server.
  • Foreword
  • Preface
  • Acknowledgments
  • About this Book
  • About the Cover Illustration
  • Part 1: Getting Started
  • Part 2: Core concepts
  • Part 3: Next steps
  • Appendix A
  • Appendix B
  • Buy the paperback

    9.3 Packing bits and bytes

    When we discussed sharding HASHes, I briefly mentioned that if we’re storing short
    sharded HASHes as a way of reducing memory use. But let’s say that we wanted to store
    a short fixed amount of information for sequential IDs. Can we use even less memory
    than sharded HASHes?

    In this section, we’ll use sharded Redis STRINGs to store location information for
    large numbers of users with sequential IDs, and discuss how to perform aggregate
    counts over this stored data. This example shows how we can use sharded Redis
    STRINGs to store, for example, location information for users on Twitter.

    Before we start storing our data, we need to revisit four commands that’ll let us
    efficiently pack and update STRINGs in Redis: GETRANGE, SETRANGE, GETBIT, and SETBIT.
    The GETRANGE command lets us read a substring from a stored STRING. SETRANGE
    will let us set the data stored at a substring of the larger STRING. Similarly, GETBIT will
    fetch the value of a single bit in a STRING, and SETBIT will set an individual bit. With
    these four commands, we can use Redis STRINGs to store counters, fixed-length
    strings, Booleans, and more in as compact a format as is possible without compression.
    With our brief review out of the way, let’s talk about what information we’ll store.