Redis key 에 특수문자 및 공백이 들어가도 될까 ?

mallin·2022년 3월 31일
0

Redis

목록 보기
1/1
post-thumbnail

평화롭게 개발을 하던 그때 ... Redis 키에 공백이 들어가 있는 걸 확인했다.
보자마자 내 눈이 잘 못된 줄 알았지만 .. 실제 운영에도 나가 있는 Redis 키이고, Redis GUI 프로그램인 AnotherRedisDesktopManager 으로 봤을 때에도 값이 정상적으로 들어가 있는 걸 확인했다.

💡 Another Redis Desktop Manager 란 ?
Redis 에 있는 키 및 값을 쉽게 보기 위한 GUI 프로그램으로, redis GUI 프로그램을 medis 도 써봤는데 무료 프로그램 중에서는 Another Redis Desktop Manager 가 사용하기 가장 편리 한 것 같다. 많이 안써보긴 했지만
다운로드 링크 👉 https://goanother.com/

그래서 갑자기 궁금증이 생겼다. Redis key 에는 모든 문자 및 특수 문자 & 공백이 들어가도 되는건가 ??

먼저, 공식 문서를 살펴보자.
redis docs -> https://redis.io/docs/manual/data-types/data-types-tutorial/

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like “foo” to the content of a JPEG file. The empty string is also a valid key.

A few other rules about keys:

Very long keys are not a good idea. For instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, hashing it (for example with SHA1) is a better idea, especially from the perspective of memory and bandwidth.
Very short keys are often not a good idea. There is little point in writing “u1000flw” as a key if you can instead write “user:1000:followers”. The latter is more readable and the added space is minor compared to the space used by the key object itself and the value object. While short keys will obviously consume a bit less memory, your job is to find the right balance.
Try to stick with a schema. For instance “object-type:id” is a good idea, as in “user:1000”. Dots or dashes are often used for multi-word fields, as in “comment:1234:reply.to” or “comment:1234:reply-to”.
The maximum allowed key size is 512 MB.

... 난 영어를 잘 못하기 때문에 파파고로 번역해서 요약해보자면

  1. Redis 키는 바이너리 세이프이다. 문자열에서 JPEG 파일의 내용에 이르기까지 모든 바이너리 시퀀스를 키로 사용할 수 있으며, 빈 문자열도 유효한 키이다.
  2. 매우 긴 키는 메모리 측면뿐만 아니라 데이터 집합에서 키를 조회하는 데 비용이 많이 들 수 있기 때문에 좋은 생각이 아니다.
  3. 뜻을 알아 볼 수 없을 정도로 너무 짧게 키를 만드는 것도 좋은 생각이 아니다.
  4. 허용되는 최대 키 크기는 512MB 이다.

여기서 바이너리 세이프란 무엇일까 ?
일단 바이너리가 뭔지 알아보자. 바이너리는 2진법을 의미하며, '1'과 '0'만을 사용하여 수를 나타내는 진법이다. 결국, 바이너리 세이프라는 건 Byte Sequence(기계가 처리하는 문자의 형태) 로 되어 있다면 모두 키로 활용 가능 하다는 것이다.

모두 가능하다고 해도 다 같이 사용하는 Redis key 라고 하면 키에 공백이랑 특수문자 넣는 일은 자제하자 ... 😇

0개의 댓글