Deep understanding of Redis performance _redis

Source: Internet
Author: User
Tags redis value store

Brief introduction

How many times have you found yourself stuck in a Web application without performance after months of development and countless efforts? How many times have you wondered if you can't communicate fast and fast to ordinary users, your clients should also treat you as an expert? How much do you hear about the bad contrast between Google and Facebook? Let me tell you what my clients think about this:

I have developed a Web application with complex processing and filtering because of many business rules and UI requirements. Plus some third-party providers of outdated technology, for them, speed means 15 years of work thrown into the trash, and then start again. My application is not so fast, sometimes processing a request costs 6~8s will be processed, business rules validation, filtering and formatting responses. And customers do not accept this. And said Google would do well if it did. Facebook, too, is fine. I can't explain to the customer how large the hardware behind the speed is and how much pressure the server is under. I just reply that can do better, I remember redis after the guarantee can do.


So, before we go into Redis, I'll introduce NoSQL.

Most developers are accustomed to using relational databases. Data-driven development or domain-driven development (as a monolithic framework encoding or NHibernate with the fluent API), the concept of relational databases. NoSQL, on the other hand, ushered in a new fashion for data storage. The most popular among the. NET communities are mongodb,ravendb and Redis. I have written an article on how to use MONGDB, which you can find here. I use it to log logs of important activities, errors, and exceptions.

In the NoSQL world, concepts are basically the same as all popular databases. Basically it's a JSON document that exists somewhere on your machine, or some kind of. NET client or driver that you can manipulate. The great thing about NoSQL is that it solves one of the major drawbacks of relational databases and OO languages such as C # or Java: Mismatch impedance.


Redis is actually the same as any NoSQL database. But it's a memory database and it's a great performance.

Simply put, Redis can give you an incredible ability to access your object's state to make your application faster. Sometimes, your application can be optimized 8 to 10 times times faster. It's not a joke and you don't have to have a Google or Facebook infrastructure behind you. If you want to know more about redis behind the idea and its historical content, you can find more online. There are a lot of articles about these.

Redis represents the remote Dictionary service. It is a key-value store like C # 's Dictionary object. So let's see how to use:


First, follow the steps below to install and run Redis:

    • Open Redis.io and download the Win64 version of Redis (it does not specify the appropriate version according to your system)
    • Copy the downloaded content to the Redis folder (you can also name other names you like)
    • Click Redis-server.exe to start the service side
    • Click Redis-client.exe to run the client command line tool
    • Now let's try to run some basic commands to detect if the installation is correct. In the client command line interface:
    • Enter set Azul "Hello World" to add a record, you will receive OK feedback. This means that you have added a key to the Azul value "Hello World" (by the way, Azul is greeting in the Kabair language)
    • Input get Azul gets the corresponding value you will receive feedback Hello World
    • Entering Delete Azul deletes this entry

Because Redis is a dictionary, you can save key values in the following ways:

Copy Code code as follows:
Set schedule:1 "{' origin ': ' Montreal ', ' Destination ': ' Toronto '}"

The value in this example is a JSON object. This means that you can add complex objects to the Redis. But there is no need to do this because Redis supports 5 types of data to meet your needs.

You can also specify key in the following ways:

Copy Code code as follows:
Set Schedule:id 1

For executing multiple sets and get, you can use Mset and mget instead. I will no longer discuss these commands. You can view the Redis document and try it out.

Behind

Everything we've done so far has broken the routine. But behind the scenes is the Redis client sends instructions to the server through the Redis protocol. The server executes these instructions on the data in memory and returns the result of the response.
Redis as a service

You can use Redis on physical machines, virtual machines, or Redis as a cloud service. Many suppliers such as digital ocean and widnows Azure offer the service. We'll start with digital ocean (mainly considering the price, and I think Azure's Redis service is a bit expensive)


We use the Putty SSH (secure connection mode) to connect to digital Ocean. Download putty and configure the IP address to your do droplet address (droplet is the Linux host you created on Do). Then start putty and log in with the root account and password provided by do. Run the following command to keep your Linux host updated: Apt-get update (Linux users are excited now).

Now you are or have kept the update. Then run apt-get install build-essential Install all the missing tools for your Linux server. Oh, yes. In addition, there are many more detailed posts for these settings. I have to admit, because I am not familiar with the Linux world, so in the configuration when it really cost a lot of hands.

Using in Visual Studio

On the Start page, add an MVC project and add Servicestack.redis using the NuGet Package Manager. So you can connect to Redis and do some interesting things.

You need to follow these steps to connect Redis and get a cached list of objects.

using (iredisclient client = redisclient)
 {
  var scheduleclient= client. Gettypedclient<schedule> ();
  var schedules = Scheduleclient.getall ();
 }

Even if you cache a lot of objects this is a very fast operation. Caching objects in Redis is a good idea, eliminating the possibility of business processing (applying rules or other filtering and formatting operations).

The objects that are repeatedly extracted in the database (often the same) are best placed in the Redis. Usually we want to cache the data in the warehouse (for more details please browse the cached Warehouse mode cached repository pattern) It's also best to put it in Redis. This allows you to quickly get the results of your operation and dramatically improve the overall performance of your Web application.


Note that scheduleclient will expose a lot of functions for adding, adding, removing, and using lists, hashes, and so on. Try to explore more ...

You can also set up your Redis client to look at (not with glimpse-;)) What happened in this situation where you added the monitor command (the same place we added Azul when we started).

In the complete anti-HelloWorld application style (I admit I was one of them!) The warrior jumps to the sky excitedly, I should also remind one thing: Connect to your database from your controller (that is, redis! It's not a good idea, if it's strange to you, I think you need to read something about design and architecture. I don't understand Microsoft's guidance to allow users to add an MVC project, and you can start building a great commercial web application on the Slap. Because it's not like that.


However, here is an example of adding an object, in this case a scheduler:


Domain Object public
 class Schedule 
 {public
  int Id {get; set;}
  public string Origin {get; set;}
  public string Destination {get; set;}
 } 
 
 using (iredisclient client = redisclient)
 {
  var scheduleclient= client. Gettypedclient<schedule> ();
  var schedule= new Schedule
  {
    id= scheduleclient.getnextsequence (),
    Origin = "Montreal",
    Destination = "Quebec"
 }
  var schedules = scheduleclient.store (schedule);
 }

Because Redis is a caching service, it is possible to expire the input by setting a time-out. You can also reverse the process by removing that timeout.

This is just an introduction to Redis and gives you a way to improve the performance of Web references. In some cases, Redis may give you an endorsement of the passion that your client or boss has not given you about building reliable software.

I hope this will help some people!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.